diff --git a/deployment-strategies/blue-green/deploy-blue.sh b/deployment-strategies/blue-green/deploy-blue.sh index 129f1be..115af82 100755 --- a/deployment-strategies/blue-green/deploy-blue.sh +++ b/deployment-strategies/blue-green/deploy-blue.sh @@ -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" \ No newline at end of file +kubectl apply -f rs2.yaml +kubectl apply -f service-blue.yaml +echo "perform testing of your blue version --curl my-app-blue" diff --git a/deployment-strategies/blue-green/rs1.yaml b/deployment-strategies/blue-green/rs1.yaml index 8e4c68d..bda3e3b 100644 --- a/deployment-strategies/blue-green/rs1.yaml +++ b/deployment-strategies/blue-green/rs1.yaml @@ -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" diff --git a/deployment-strategies/blue-green/rs2.yaml b/deployment-strategies/blue-green/rs2.yaml index 345065c..9b38535 100644 --- a/deployment-strategies/blue-green/rs2.yaml +++ b/deployment-strategies/blue-green/rs2.yaml @@ -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" diff --git a/deployment-strategies/blue-green/service-blue.yaml b/deployment-strategies/blue-green/service-blue.yaml index 7cec494..dfa211b 100644 --- a/deployment-strategies/blue-green/service-blue.yaml +++ b/deployment-strategies/blue-green/service-blue.yaml @@ -3,7 +3,7 @@ kind: Service metadata: name: my-app-blue labels: - app: my-app + app: my-app-team-y spec: type: NodePort ports: @@ -11,5 +11,5 @@ spec: port: 80 targetPort: http selector: - app: my-app + app: my-app-team-y version: v2.0.0 diff --git a/deployment-strategies/blue-green/service-green.yaml b/deployment-strategies/blue-green/service-green.yaml index 3226d6b..fa46d8f 100644 --- a/deployment-strategies/blue-green/service-green.yaml +++ b/deployment-strategies/blue-green/service-green.yaml @@ -3,7 +3,7 @@ kind: Service metadata: name: my-app-green labels: - app: my-app + app: my-app-team-y spec: type: NodePort ports: @@ -11,5 +11,5 @@ spec: port: 80 targetPort: http selector: - app: my-app + app: my-app-team-y version: v1.0.0 diff --git a/deployment-strategies/blue-green/tear_down.sh b/deployment-strategies/blue-green/tear_down.sh new file mode 100755 index 0000000..40f81e5 --- /dev/null +++ b/deployment-strategies/blue-green/tear_down.sh @@ -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 diff --git a/deployment-strategies/k8s_helper.sh b/deployment-strategies/k8s_helper.sh new file mode 100644 index 0000000..b242ce2 --- /dev/null +++ b/deployment-strategies/k8s_helper.sh @@ -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 +} \ No newline at end of file diff --git a/deployment-strategies/nginx-deployment/deployment.yaml b/deployment-strategies/nginx-deployment/deployment.yaml new file mode 100644 index 0000000..b5f8f37 --- /dev/null +++ b/deployment-strategies/nginx-deployment/deployment.yaml @@ -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 + diff --git a/deployment-strategies/nginx-deployment/service.yaml b/deployment-strategies/nginx-deployment/service.yaml new file mode 100644 index 0000000..84ddb02 --- /dev/null +++ b/deployment-strategies/nginx-deployment/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: my-nginx +spec: + selector: + app: nginx + ports: + - protocol: TCP + port: 80 + targetPort: 80 diff --git a/deployment-strategies/nginx-deployment/test-ms.yaml b/deployment-strategies/nginx-deployment/test-ms.yaml new file mode 100644 index 0000000..3ef22fa --- /dev/null +++ b/deployment-strategies/nginx-deployment/test-ms.yaml @@ -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 + diff --git a/deployment-strategies/recreate/Makefile b/deployment-strategies/recreate/Makefile new file mode 100644 index 0000000..5646fad --- /dev/null +++ b/deployment-strategies/recreate/Makefile @@ -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 + diff --git a/deployment-strategies/recreate/app-v1-deploy.yaml b/deployment-strategies/recreate/app-v1-deploy.yaml new file mode 100644 index 0000000..52978eb --- /dev/null +++ b/deployment-strategies/recreate/app-v1-deploy.yaml @@ -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 diff --git a/deployment-strategies/rolling-update/rs2.yaml b/deployment-strategies/recreate/app-v2-deploy.yaml similarity index 87% rename from deployment-strategies/rolling-update/rs2.yaml rename to deployment-strategies/recreate/app-v2-deploy.yaml index 5d5f4f0..a25bf73 100644 --- a/deployment-strategies/rolling-update/rs2.yaml +++ b/deployment-strategies/recreate/app-v2-deploy.yaml @@ -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" @@ -41,4 +42,3 @@ spec: path: /ready port: probe periodSeconds: 5 - diff --git a/deployment-strategies/recreate/recreate.sh b/deployment-strategies/recreate/recreate.sh index 8bb469e..3259173 100755 --- a/deployment-strategies/recreate/recreate.sh +++ b/deployment-strategies/recreate/recreate.sh @@ -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 \ No newline at end of file +function cleanUp() { + deleteManifest ../rs1.yaml + deleteManifest ../rs2.yaml + deleteManifest ../service.yaml + replicaSetStatus +} diff --git a/deployment-strategies/recreate/recreate_strategy.sh b/deployment-strategies/recreate/recreate_strategy.sh index 1f09cc1..54e5cf5 100755 --- a/deployment-strategies/recreate/recreate_strategy.sh +++ b/deployment-strategies/recreate/recreate_strategy.sh @@ -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" diff --git a/deployment-strategies/recreate/service.yaml b/deployment-strategies/recreate/service.yaml deleted file mode 100644 index b84c8f1..0000000 --- a/deployment-strategies/recreate/service.yaml +++ /dev/null @@ -1,14 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: my-app - labels: - app: my-app -spec: - type: NodePort - ports: - - name: http - port: 80 - targetPort: http - selector: - app: my-app diff --git a/deployment-strategies/rolling-update/Makefile b/deployment-strategies/rolling-update/Makefile new file mode 100644 index 0000000..65077e2 --- /dev/null +++ b/deployment-strategies/rolling-update/Makefile @@ -0,0 +1,13 @@ +SHELL := /bin/bash + +initialSetup: + source rolling.sh;startDeployment + +deployment: + source rolling.sh;rollingDeployment + +rollback: + source rolling.sh;rollback + +tearDown: + source rolling.sh;cleanUp diff --git a/deployment-strategies/rolling-update/app-v1-deploy.yaml b/deployment-strategies/rolling-update/app-v1-deploy.yaml new file mode 100644 index 0000000..9f5719a --- /dev/null +++ b/deployment-strategies/rolling-update/app-v1-deploy.yaml @@ -0,0 +1,57 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-app + labels: + app: my-app +spec: + replicas: 10 + 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 diff --git a/deployment-strategies/recreate/rs2.yaml b/deployment-strategies/rolling-update/app-v2-deploy.yaml similarity index 79% rename from deployment-strategies/recreate/rs2.yaml rename to deployment-strategies/rolling-update/app-v2-deploy.yaml index 8458bf7..72971fc 100644 --- a/deployment-strategies/recreate/rs2.yaml +++ b/deployment-strategies/rolling-update/app-v2-deploy.yaml @@ -1,19 +1,23 @@ apiVersion: apps/v1 -kind: ReplicaSet +kind: Deployment metadata: name: my-app labels: app: my-app spec: - # modify replicas according to your case - replicas: 5 + replicas: 10 + strategy: + type: RollingUpdate + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 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" @@ -40,5 +44,4 @@ spec: httpGet: path: /ready port: probe - periodSeconds: 5 - + periodSeconds: 5 \ No newline at end of file diff --git a/deployment-strategies/rolling-update/rolling.sh b/deployment-strategies/rolling-update/rolling.sh index 1d963d6..ad3678b 100755 --- a/deployment-strategies/rolling-update/rolling.sh +++ b/deployment-strategies/rolling-update/rolling.sh @@ -1,35 +1,48 @@ #!/bin/bash -source rolling_strategy.sh +source ../k8s_helper.sh -kubectl apply -f rs1.yaml -kubectl apply -f rs2.yaml -kubectl apply -f service.yaml +function startDeployment() { + apply_manifest ../rs1.yaml + apply_manifest ../rs2.yaml + waitForRS my-app-v1 5 + apply_manifest ../service.yaml + replicaSetStatus +} -replica_count_v1=`kubectl get rs my-app-v1 -o json | jq '.status.readyReplicas'` -while [ $replica_count_v1 != 5 ] +function rollingDeployment() { + rs1_replica_count=5 + rs2_replica_count=0 + while [[ $rs2_replica_count != "5" ]] do - echo "Ready replicas of Version 1 : $replica_count_v1" - sleep 1s - replica_count_v1=`kubectl get rs my-app-v1 -o json | jq '.status.readyReplicas'` + rs1_replica_count=$((rs1_replica_count-1)) + rs2_replica_count=$((rs2_replica_count+1)) + + setReplicaForRS my-app-v1 ${rs1_replica_count} + setReplicaForRS my-app-v2 ${rs2_replica_count} + waitForRS my-app-v2 ${rs2_replica_count} + replicaSetStatus done +} + +function rollback() { + rs1_replica_count=0 + rs2_replica_count=5 + while [[ $rs1_replica_count != "5" ]] + do + rs1_replica_count=$((rs1_replica_count+1)) + rs2_replica_count=$((rs2_replica_count-1)) -# Current ReplicaSet Host and Version Details -service=$(minikube service my-app --url) -version=$(curl "$service") -echo "Current ReplicaSet host and version -------- $version" + setReplicaForRS my-app-v1 ${rs1_replica_count} + setReplicaForRS my-app-v2 ${rs2_replica_count} + waitForRS my-app-v2 ${rs2_replica_count} + replicaSetStatus + done +} -# Scaling ReplicaSet -echo -n " Do you want to scale Replica set select yes(y) or no(n) --> " -read option -case $option in -y) - scaleReplicas -;; -n) - exit; -;; -*) - echo "Invalid option select y to yes and n to exit" -;; -esac \ No newline at end of file +function cleanUp() { + deleteManifest ../rs1.yaml + deleteManifest ../rs2.yaml + deleteManifest ../service.yaml + replicaSetStatus +} diff --git a/deployment-strategies/rolling-update/rolling_strategy.sh b/deployment-strategies/rolling-update/rolling_strategy.sh deleted file mode 100755 index d2e4203..0000000 --- a/deployment-strategies/rolling-update/rolling_strategy.sh +++ /dev/null @@ -1,31 +0,0 @@ -scaleReplicas(){ - replica_count_current_v1=`kubectl get rs my-app-v1 -o json | jq '.status.readyReplicas'` - replica_count_current_v2=0 - echo $replica_count_current_v1 - echo $replica_count_current_v2 - while [[ $replica_count_current_v1 != 'null' && $replica_count_current_v2 != 5 ]] - do - ##V1 - replica_count_desired_v1=$((replica_count_current_v1-1)) - echo $replica_count_desired_v1 - kubectl scale rs --replicas="$replica_count_desired_v1" my-app-v1 - sleep 1s - replica_count_current_v1="$replica_count_desired_v1" - - ## V2 - replica_count_desired_v2=$((replica_count_current_v2+1)) - kubectl scale rs --replicas="$replica_count_desired_v2" my-app-v2 - sleep 1s - replica_count_current_v2=`kubectl get rs my-app-v2 -o json | jq '.status.readyReplicas'` - while [ $replica_count_current_v2 != $replica_count_desired_v2 ] - do - echo "Ready replicas of Version 2 : $replica_count_current_v2" - sleep 1s - replica_count_current_v2=`kubectl get rs my-app-v2 -o json | jq '.status.readyReplicas'` - done - done - echo "Scaling of replicaset v1 to v2 completed" - service=$(minikube service my-app --url) - version=$(curl "$service") - echo "Current ReplicaSet host and version -------- $version" -} \ No newline at end of file diff --git a/deployment-strategies/rolling-update/rs1.yaml b/deployment-strategies/rs1.yaml similarity index 81% rename from deployment-strategies/rolling-update/rs1.yaml rename to deployment-strategies/rs1.yaml index daeff56..d0d350a 100644 --- a/deployment-strategies/rolling-update/rs1.yaml +++ b/deployment-strategies/rs1.yaml @@ -9,11 +9,11 @@ spec: replicas: 5 selector: matchLabels: - app: my-app + app: my-app-team-z template: metadata: labels: - app: my-app + app: my-app-team-z version: v1.0.0 annotations: prometheus.io/scrape: "true" @@ -22,6 +22,13 @@ spec: containers: - name: my-app image: containersol/k8s-deployment-strategies + resources: + limits: + cpu: 100m + memory: 100Mi + requests: + cpu: 50m + memory: 50Mi ports: - name: http containerPort: 8080 diff --git a/deployment-strategies/recreate/rs1.yaml b/deployment-strategies/rs2.yaml similarity index 74% rename from deployment-strategies/recreate/rs1.yaml rename to deployment-strategies/rs2.yaml index e14f817..da9d7ba 100644 --- a/deployment-strategies/recreate/rs1.yaml +++ b/deployment-strategies/rs2.yaml @@ -1,20 +1,20 @@ apiVersion: apps/v1 kind: ReplicaSet metadata: - name: my-app + name: my-app-v2 labels: app: my-app spec: # modify replicas according to your case - replicas: 5 + replicas: 0 selector: matchLabels: - app: my-app + app: my-app-team-z template: metadata: labels: - app: my-app - version: v1.0.0 + app: my-app-team-z + version: v2.0.0 annotations: prometheus.io/scrape: "true" prometheus.io/port: "9101" @@ -22,6 +22,13 @@ spec: containers: - name: my-app image: containersol/k8s-deployment-strategies + resources: + limits: + cpu: 100m + memory: 100Mi + requests: + cpu: 50m + memory: 50Mi ports: - name: http containerPort: 8080 @@ -29,7 +36,7 @@ spec: containerPort: 8086 env: - name: VERSION - value: v1.0.0 + value: v2.0.0 livenessProbe: httpGet: path: /live diff --git a/deployment-strategies/rolling-update/service.yaml b/deployment-strategies/service.yaml similarity index 77% rename from deployment-strategies/rolling-update/service.yaml rename to deployment-strategies/service.yaml index b84c8f1..b1802cc 100644 --- a/deployment-strategies/rolling-update/service.yaml +++ b/deployment-strategies/service.yaml @@ -3,7 +3,7 @@ kind: Service metadata: name: my-app labels: - app: my-app + app: my-app-team-z spec: type: NodePort ports: @@ -11,4 +11,4 @@ spec: port: 80 targetPort: http selector: - app: my-app + app: my-app-team-z