For simulated Practice problems visit KillerCoda.
-
create a deployment
nginx
withnginx:1.18.0
image with4
replicas, in the default namespace, with deployment strategy ofrecreate
expose port80
on the containers.Solution
#generate yaml file k create deploy nginx --image=nginx:1.18.0 --replicas=4 --dry-run=client -o yaml > deploy.yaml #update pod.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: nginx spec: replicas: 4 selector: matchLabels: app: nginx strategy: type: Recreate template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.18.0 name: nginx ports: - containerPort: 80 resources: {} status: {} # create deployment k create -f deploy.yaml
-
update existing deployment
nginx
in the default namespace, to change it's deployment strategy toRollingUpdate
fromrecreate
expose port80
on the containers. And set maxSurge to50%
while rolling update on the deploymentSolution
#generate yaml file k create deploy nginx --image=nginx:1.18.0 --replicas=4 --dry-run=client -o yaml > deploy.yaml #update pod.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: nginx spec: replicas: 4 selector: matchLabels: app: nginx strategy: type: RollingUpdate rollingUpdate: maxSurge: 50% template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.18.0 name: nginx ports: - containerPort: 80 resources: {} status: {} # create deployment k create -f deploy.yaml
-
update
nginx
deployment to use imagenginx:1.19.8
. check deployment status runningk rollout status deploy nginx
. check no. of pods surge up to 6 while updation(check this by runningk get deploy nginx
)Solution
#update the deployment k edit deploy nginx # then edit the container image OR # set new image on the deployment k set image deploy nginx nginx=nginx:1.19.8
-
There exist a namespace
blue-green
, a deployment namedblue
. Also a service namednginx-svc
exposing thenginx
deployment on port80
. If you request the service it will returnThis is blue deployment
. Create another deployment namedgreen
with labelmark=green
&image=nginx:1.19.8
and make changes such that it should returnThis is green deployment
while callingnginx-svc
service. Deleteblue
deployment.Setup
# copy contents to a file 'script.sh' #!/bin/bash # Create the namespace kubectl create namespace blue-green # Create the deployment kubectl apply -f - <<EOF apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: blue namespace: blue-green spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx mark: blue spec: initContainers: - image: busybox name: busybox command: ["sh","-c","echo 'This is blue deployment' > /sd/index.html"] volumeMounts: - name: store mountPath: /sd containers: - image: nginx:1.18.0 name: nginx ports: - containerPort: 80 volumeMounts: - name: store mountPath: /usr/share/nginx/html resources: {} volumes: - name: store emptyDir: {} EOF kubectl expose deployment nginx --type=ClusterIP --name=nginx-svc --port=80 --target-port=80 --selector=mark=blue --namespace=blue-green echo "Namespace, service and deployment created successfully."
# make the file executable chmod +x script.sh # run script ./script.sh # make request to the svc # check service ip k get svc -n blue-green wget -qO- <IP>
Solution
# update the deployment ... ... metadata: creationTimestamp: null labels: app: nginx name: blue ... ... labels: app: nginx mark: green spec: initContainers: - image: busybox name: busybox command: ["sh","-c","echo 'This is green deployment' > /sd/index.html"] ... ... containers: - image: nginx:1.19.8 name: nginx ... ... # edit nginx-svc k edit svc nginx-svc # and update selector to mark: green ... selector: mark: green ...
-
There exist a namespace
canary
with deploymentapp-v1
and serviceapp-svc
.Make request to the service and it returnsApp Version V1
. Create another deploymentapp-v2
such that50%
of traffic directed to it, after deployingapp-v2
. Making request to the service returnsApp Version V1
if request goes toapp-v1
& andApp Version V2
if request goes toapp-v2
. use labelapp=frontend
.Setup
# copy contents to a file 'script.sh' #!/bin/bash # Create the namespace kubectl create namespace canary # Create the deployment kubectl apply -f - <<EOF apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: app-v1 namespace: canary spec: replicas: 2 selector: matchLabels: app: frontend strategy: {} template: metadata: creationTimestamp: null labels: app: frontend spec: initContainers: - image: busybox name: busybox command: ["sh","-c","echo 'App Version V1' > /sd/index.html"] volumeMounts: - name: store mountPath: /sd containers: - image: nginx:1.18.0 name: nginx ports: - containerPort: 80 volumeMounts: - name: store mountPath: /usr/share/nginx/html resources: {} volumes: - name: store emptyDir: {} EOF kubectl expose deployment app-v1 --type=ClusterIP --name=app-svc --port=80 --target-port=80 --selector=app=frontend --namespace=canary echo "Namespace, service and deployment created successfully."
# make the file executable chmod +x script.sh # run script ./script.sh # make request to the svc # check service ip k get svc -n canary wget -qO- <IP>
Solution
# create deployment app-v2 apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: app-v2 namespace: canary spec: replicas: 2 selector: matchLabels: app: frontend strategy: {} template: metadata: creationTimestamp: null labels: app: frontend spec: initContainers: - image: busybox name: busybox command: ["sh","-c","echo 'App Version V2' > /sd/index.html"] volumeMounts: - name: store mountPath: /sd containers: - image: nginx:1.18.0 name: nginx ports: - containerPort: 80 volumeMounts: - name: store mountPath: /usr/share/nginx/html resources: {} volumes: - name: store emptyDir: {} # make request to the svc # check service ip k get svc -n canary wget -qO- <IP>