Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions deployment-strategies/blue-green/deploy-blue.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
#!/bin/bash
#primary=$1
#blue_app=$1
#blue_svc=$2
#rs2.yaml
#service-blue.yaml
kubectl apply -f $blue_app
kubectl apply -f $blue_svc
echo "perform testing of your blue version --curl my-app-blue"
kubectl apply -f rs2.yaml
kubectl apply -f service-blue.yaml
echo "perform testing of your blue version --curl my-app-blue"
4 changes: 2 additions & 2 deletions deployment-strategies/blue-green/rs1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ spec:
replicas: 5
selector:
matchLabels:
app: my-app
app: my-app-team-y
version: v1.0.0
template:
metadata:
labels:
app: my-app
app: my-app-team-y
version: v1.0.0
annotations:
prometheus.io/scrape: "true"
Expand Down
4 changes: 2 additions & 2 deletions deployment-strategies/blue-green/rs2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ spec:
replicas: 5
selector:
matchLabels:
app: my-app
app: my-app-team-y
version: v2.0.0
template:
metadata:
labels:
app: my-app
app: my-app-team-y
version: v2.0.0
annotations:
prometheus.io/scrape: "true"
Expand Down
4 changes: 2 additions & 2 deletions deployment-strategies/blue-green/service-blue.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ kind: Service
metadata:
name: my-app-blue
labels:
app: my-app
app: my-app-team-y
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: http
selector:
app: my-app
app: my-app-team-y
version: v2.0.0
4 changes: 2 additions & 2 deletions deployment-strategies/blue-green/service-green.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ kind: Service
metadata:
name: my-app-green
labels:
app: my-app
app: my-app-team-y
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: http
selector:
app: my-app
app: my-app-team-y
version: v1.0.0
8 changes: 8 additions & 0 deletions deployment-strategies/blue-green/tear_down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

teardown (){

kubectl delete -f rs1.yaml -f rs2.yaml
kubectl delete -f service-green.yaml -f service-blue.yaml
}
teardown
44 changes: 44 additions & 0 deletions deployment-strategies/k8s_helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function getReplicaCount() {
RS_NAME=$1
kubectl get rs ${RS_NAME} -o json | jq '.status.readyReplicas'
}

function apply_manifest() {
MANIFEST_FILE=$1
kubectl apply -f ${MANIFEST_FILE}
}

function setReplicaForRS() {
RS_NAME=$1
RS_DESIRED_COUNT=$2
kubectl scale rs --replicas="${RS_DESIRED_COUNT}" ${RS_NAME}
}

function deleteManifest() {
MANIFEST_FILE=$1
kubectl delete -f ${MANIFEST_FILE}
}

function replicaSetStatus() {
echo "Status of replicasets"
kubectl get rs
}

function waitForRS() {
RS_NAME=$1
RS_DESIRED_COUNT=$2

replica_count=`getReplicaCount ${RS_NAME}`
if [ ${RS_DESIRED_COUNT} = "0" ]
then
#Just handling a boundary
return
fi
while [ $replica_count != ${RS_DESIRED_COUNT} ]
do
echo "Ready replicas of ${RS_NAME}:$replica_count"
echo "Wating to reach to ${RS_DESIRED_COUNT}"
sleep 1s
replica_count=`getReplicaCount ${RS_NAME}`
done
}
20 changes: 20 additions & 0 deletions deployment-strategies/nginx-deployment/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

11 changes: 11 additions & 0 deletions deployment-strategies/nginx-deployment/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: my-nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
26 changes: 26 additions & 0 deletions deployment-strategies/nginx-deployment/test-ms.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
test-pod.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ms
spec:
selector:
matchLabels:
app: ms
replicas: 1
template:
metadata:
labels:
app: ms
spec:
containers:
- name: ms
image: rachitsrivastava007/sleep:8
ports:
- containerPort: 80
args:
- -c
- while true; do echo hello; sleep 10;done
command:
- /bin/sh

14 changes: 14 additions & 0 deletions deployment-strategies/recreate/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SHELL := /bin/bash

initialSetup:
source recreate.sh;startDeployment

deployment:
source recreate.sh;recreateDeployment

rollback:
source recreate.sh;rollback

tearDown:
source recreate.sh;cleanUp

59 changes: 59 additions & 0 deletions deployment-strategies/recreate/app-v1-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-v1
labels:
app: my-app
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: my-app-team-z
template:
metadata:
labels:
app: my-app-team-z
version: v1.0.0
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9101"
spec:
containers:
- name: my-app
image: containersol/k8s-deployment-strategies
ports:
- name: http
containerPort: 8080
- name: probe
containerPort: 8086
env:
- name: VERSION
value: v1.0.0
livenessProbe:
httpGet:
path: /live
port: probe
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: probe
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: my-app
labels:
app: my-app-team-z
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: http
selector:
app: my-app-team-z
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
apiVersion: apps/v1
kind: ReplicaSet
kind: Deployment
metadata:
name: my-app-v2
labels:
app: my-app
spec:
# modify replicas according to your case
replicas: 0
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: my-app
app: my-app-team-z
template:
metadata:
labels:
app: my-app
app: my-app-team-z
version: v2.0.0
annotations:
prometheus.io/scrape: "true"
Expand Down Expand Up @@ -41,4 +42,3 @@ spec:
path: /ready
port: probe
periodSeconds: 5

58 changes: 26 additions & 32 deletions deployment-strategies/recreate/recreate.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
#!/bin/bash
source ../k8s_helper.sh

source recreate_strategy.sh
function startDeployment() {
apply_manifest ../rs1.yaml
waitForRS my-app-v1 5
apply_manifest ../service.yaml
replicaSetStatus
}

# Creating ReplicaSet and its Service
kubectl apply -f rs1.yaml
kubectl apply -f service.yaml
function recreateDeployment() {
setReplicaForRS my-app-v1 0
apply_manifest ../rs2.yaml
setReplicaForRS my-app-v2 5
waitForRS my-app-v2 5
replicaSetStatus
}

function rollback() {
setReplicaForRS my-app-v2 0
setReplicaForRS my-app-v1 5
waitForRS my-app-v1 5
replicaSetStatus
}

replica_count=`kubectl get rs my-app -o json | jq '.status.readyReplicas'`
while [ $replica_count != 5 ]
do
echo "Reday replicas of Version 1 : $replica_count"
sleep 1s
replica_count=`kubectl get rs my-app -o json | jq '.status.readyReplicas'`
done

# Current ReplicaSet Host and Version Details
service=$(minikube service my-app --url)
version=$(curl "$service")
echo "Current ReplicaSet host and version -------- $version"

# Scaling ReplicaSet
echo -n " Do you want to scale Replica set select yes(y) or no(n) --> "
read option
case $option in
y)
rc=0
scaleReplicas "$rc"
;;
n)
exit;
;;
*)
echo "Invalid option select y to yes and n to exit"
;;
esac
function cleanUp() {
deleteManifest ../rs1.yaml
deleteManifest ../rs2.yaml
deleteManifest ../service.yaml
replicaSetStatus
}
10 changes: 5 additions & 5 deletions deployment-strategies/recreate/recreate_strategy.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/bin/bash

scaleReplicas(){
kubectl scale rs --replicas="$1" my-app
kubectl scale rs --replicas="$1" my-app-v1
# jq needs to be installed through sudo apt install jq
replica_count=`kubectl get rs my-app -o json | jq '.status.readyReplicas'`
replica_count=`kubectl get rs my-app-v1 -o json | jq '.status.readyReplicas'`
while [ $replica_count != 'null' ]
do
sleep 1s
replica_count=`kubectl get rs my-app -o json | jq '.status.readyReplicas'`
replica_count=`kubectl get rs my-app-v1 -o json | jq '.status.readyReplicas'`
done

kubectl apply -f rs2.yaml

replica_count=`kubectl get rs my-app -o json | jq '.status.readyReplicas'`
replica_count=`kubectl get rs my-app-v2 -o json | jq '.status.readyReplicas'`
while [ $replica_count != 5 ]
do
echo "Ready Replicas of Version 2 : $replica_count"
sleep 1s
replica_count=`kubectl get rs my-app -o json | jq '.status.readyReplicas'`
replica_count=`kubectl get rs my-app-v2 -o json | jq '.status.readyReplicas'`
done

echo "Scaling of replicaset v1 to v2 completed"
Expand Down
Loading