App Lifecycle and Management
Some of the scenario questions here are based on Kodekloud's CKA course labs.
CKAD and CKA can have similar scenario questions. It is recommended to go through the CKAD practice tests.
Shortcuts
First run the two commands below for shortcuts.
export do="--dry-run=client -o yaml"
export now="--force --grace-period=0"
Questions
-
Inspect the current deployment and determine the strategy used.
controlplane ~ ➜ k get deployments.appsNAME READY UP-TO-DATE AVAILABLE AGEfrontend 4/4 4 4 84sAnswer
controlplane ~ ➜ k describe deployments.apps frontend | grep -i strategyStrategyType: RollingUpdateRollingUpdateStrategy: 25% max unavailable, 25% max surge -
Upgrade the application by setting the image on the deployment to kodekloud/webapp-color:v2. Do not delete and re-create the deployment. Only set the new image name for the existing deployment.
Answer
controlplane ~ ➜ k edit deployments.apps frontenddeployment.apps/frontend editedapiVersion: apps/v1kind: Deploymentmetadata:annotations:deployment.kubernetes.io/revision: "1"creationTimestamp: "2023-12-29T17:01:17Z"generation: 1name: frontendnamespace: defaultresourceVersion: "855"uid: 05c0c824-dd8a-4f28-9c88-0f45cfd1702dspec:minReadySeconds: 20progressDeadlineSeconds: 600replicas: 4revisionHistoryLimit: 10selector:matchLabels:name: webappstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:creationTimestamp: nulllabels:name: webappspec:containers:- image: kodekloud/webapp-color:v2controlplane ~ ➜ k get deployments.appsNAME READY UP-TO-DATE AVAILABLE AGEfrontend 5/4 4 3 5m18s -
Up to how many PODs can be down for upgrade at a time. Consider the current strategy settings and number of PODs - 4
controlplane ~ ✦ ➜ k get deployments.appsNAME READY UP-TO-DATE AVAILABLE AGEfrontend 4/4 4 4 6m30sAnswer
controlplane ~ ✦ ➜ k describe deployments.apps frontend | grep -i unavailableReplicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailableRollingUpdateStrategy: 25% max unavailable, 25% max surge -
Change the deployment strategy to Recreate. Delete and re-create the deployment if necessary. Only update the strategy type for the existing deployment.
controlplane ~ ✦ ➜ k get deployments.appsNAME READY UP-TO-DATE AVAILABLE AGEfrontend 4/4 4 4 6m30sAnswer
controlplane ~ ✦ ➜ k get deployments.appsNAME READY UP-TO-DATE AVAILABLE AGEfrontend 4/4 4 4 6m30scontrolplane ~ ✦ ➜ k describe deployments.apps frontend | grep -i unavailableReplicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailableRollingUpdateStrategy: 25% max unavailable, 25% max surgecontrolplane ~ ✦ ➜ k edit deployments.apps frontendapiVersion: apps/v1kind: Deploymentmetadata:annotations:deployment.kubernetes.io/revision: "2"creationTimestamp: "2023-12-29T17:01:17Z"generation: 2name: frontendnamespace: defaultresourceVersion: "1060"uid: 05c0c824-dd8a-4f28-9c88-0f45cfd1702dspec:minReadySeconds: 20progressDeadlineSeconds: 600replicas: 4revisionHistoryLimit: 10selector:matchLabels:name: webappstrategy:type: Recreatecontrolplane ~ ✦ ➜ k get deployments.appsNAME READY UP-TO-DATE AVAILABLE AGEfrontend 4/4 4 4 10m -
In the current pod, what is the command used to run the pod ubuntu-sleeper?
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEubuntu-sleeper 1/1 Running 0 22sAnswer
controlplane ~ ➜ k get po ubuntu-sleeper -o yaml | grep -i command -A 5- command:- sleep- "4800"image: ubuntuimagePullPolicy: Alwaysname: ubuntu -
Create a pod with the ubuntu image to run a container to sleep for 5000 seconds.
Answer
## ubuntu-sleeper-2.yamlapiVersion: v1kind: Podmetadata:name: ubuntu-sleeper-2spec:containers:- name: ubuntuimage: ubuntucommand:- "sleep"- "5000"controlplane ~ ➜ k apply -f ubuntu-sleeper-2.yamlpod/ubuntu-sleeper-2 createdcontrolplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEubuntu-sleeper 1/1 Running 0 4m5subuntu-sleeper-2 1/1 Running 0 4s -
Create a pod using the file named ubuntu-sleeper-3.yaml. There is something wrong with it. Try to fix it
## ubuntu-sleeper-3.yamlapiVersion: v1kind: Podmetadata:name: ubuntu-sleeper-3spec:containers:- name: ubuntuimage: ubuntucommand:- "sleep"- 1200Answer
Commands should be enclosed in quotes.
## ubuntu-sleeper-3.yamlapiVersion: v1kind: Podmetadata:name: ubuntu-sleeper-3spec:containers:- name: ubuntuimage: ubuntucommand:- "sleep"- "1200"controlplane ~ ✦2 ➜ k apply -f ubuntu-sleeper-3.yamlpod/ubuntu-sleeper-3 createdcontrolplane ~ ✦2 ➜ k get poNAME READY STATUS RESTARTS AGEubuntu-sleeper 1/1 Running 0 11mubuntu-sleeper-2 1/1 Running 0 3m19subuntu-sleeper-3 1/1 Running 0 25s -
Create a pod with the given specifications. By default it displays a blue background. Set the given command line arguments to change it to green.
- Pod Name: webapp-green
- Image: kodekloud/webapp-color
- Command line arguments: --color=green
Answer
controlplane ~ ✦2 ➜ k run webapp-green --image kodekloud/webapp-color $do -o yaml > green.yml## green.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: webapp-greenname: webapp-greenspec:containers:- image: kodekloud/webapp-colorname: webapp-greenresources: {}args:- "--color"- "green"dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}~controlplane ~ ✦2 ➜ k apply -f green.ymlpod/webapp-green createdcontrolplane ~ ✦2 ➜ k get poNAME READY STATUS RESTARTS AGEubuntu-sleeper 1/1 Running 0 21mubuntu-sleeper-2 1/1 Running 0 13mubuntu-sleeper-3 1/1 Running 0 8m50swebapp-green 1/1 Running 0 3s -
What is the environment variable name set on the container in the pod?
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEwebapp-color 1/1 Running 0 19sAnswer
controlplane ~ ➜ k describe po webapp-color | grep -i env -A 5Environment:APP_COLOR: pink -
Identify the database host from the config map db-config.
controlplane ~ ✦ ➜ k get cmNAME DATA AGEkube-root-ca.crt 1 13mdb-config 3 9sAnswer
controlplane ~ ✦ ➜ k describe cm db-configName: db-configNamespace: defaultLabels: <none>Annotations: <none>Data====DB_HOST:----SQL01.example.comDB_NAME:----SQL01DB_PORT:----3306BinaryData====Events: <none> -
Create a new ConfigMap for the webapp-color POD. Use the spec given below.
-
ConfigMap Name: webapp-config-map
-
Data: APP_COLOR=darkblue
-
Data: APP_OTHER=disregard
Answer
controlplane ~ ✦ ➜ k create cm webapp-config-map $do > webapp-color.yml## webapp-color.ymlapiVersion: v1kind: ConfigMapmetadata:creationTimestamp: nullname: webapp-config-mapdata:APP_COLOR: "darkblue"APP_OTHER: "disregard"~controlplane ~ ✦ ➜ k apply -f webapp-color.ymlconfigmap/webapp-config-map createdcontrolplane ~ ✦ ➜ k get cmNAME DATA AGEkube-root-ca.crt 1 17mdb-config 3 4m56swebapp-config-map 2 3scontrolplane ~ ✦ ➜ k describe cm webapp-config-mapName: webapp-config-mapNamespace: defaultLabels: <none>Annotations: <none>Data====APP_OTHER:----disregardAPP_COLOR:----darkblueBinaryData====Events: <none> -
-
Update the environment variable on the POD to use only the APP_COLOR key from the newly created ConfigMap.
controlplane ~ ✦ ➜ k get poNAME READY STATUS RESTARTS AGEwebapp-color 1/1 Running 0 8mAnswer
controlplane ~ ✦ ➜ k get cmNAME DATA AGEkube-root-ca.crt 1 20mdb-config 3 7m53swebapp-config-map 2 3mcontrolplane ~ ✦ ➜ k create cm webapp-config-map $do > webapp-color.ymlcontrolplane ~ ✦ ➜ k get po webapp-color -o yaml > color.ymlcontrolplane ~ ✦ ➜ k delete -f color.yml $nowWarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "webapp-color" force deletedcontrolplane ~ ✦ ➜ k get poNo resources found in default namespace.Follow the K8s docs on how to configure pods to use configmaps.
## color.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: "2023-12-29T17:36:54Z"labels:name: webapp-colorname: webapp-colornamespace: defaultresourceVersion: "781"uid: 1e5515fa-9479-4ddd-a463-1df7723c1f8cspec:containers:- env:- name: APP_COLORvalueFrom:configMapKeyRef:name: webapp-config-mapkey: APP_COLORcontrolplane ~ ✦ ➜ k apply -f color.ymlpod/webapp-color createdcontrolplane ~ ✦ ➜ k get poNAME READY STATUS RESTARTS AGEwebapp-color 1/1 Running 0 5s -
How many secrets are defined in the dashboard-token secret?
controlplane ~ ➜ k get secretsNAME TYPE DATA AGEdashboard-token kubernetes.io/service-account-token 3 40sAnswer
controlplane ~ ➜ k describe secrets dashboard-tokenName: dashboard-tokenNamespace: defaultLabels: <none>Annotations: kubernetes.io/service-account.name: dashboard-sakubernetes.io/service-account.uid: dec16229-5c52-414f-928d-28380a3fb4b3Type: kubernetes.io/service-account-tokenData====ca.crt: 570 bytesnamespace: 7 bytestoken: eyJhbGciOiJSUzI1NiIsImtpZCI6IjlEc25IMUFwZ1pfZlRVcmVla3pvbS0yTXlsa1ZzT1RyZktaWmJCR1dYVHcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNl -
Create a new secret named db-secret with the data given below.
-
Secret Name: db-secret
-
Secret 1: DB_Host=sql01
-
Secret 2: DB_User=root
-
Secret 3: DB_Password=password123
Answer
controlplane ~ ➜ kubectl create secret generic db-secret \--from-literal='DB_Host=sql01' \--from-literal='DB_User=root' \--from-literal='DB_Password=password123'secret/db-secret createdcontrolplane ~ ➜ k get secretNAME TYPE DATA AGEdashboard-token kubernetes.io/service-account-token 3 8m18sdb-secret Opaque 3 23scontrolplane ~ ➜ k describe secrets db-secretName: db-secretNamespace: defaultLabels: <none>Annotations: <none>Type: OpaqueData====DB_Host: 5 bytesDB_Password: 11 bytesDB_User: 4 bytes -
-
Configure webapp-pod to load environment variables from the newly created secret (from the previous question).
-
Pod name: webapp-pod
-
Image name: kodekloud/simple-webapp-mysql
-
Env From: Secret=db-secret
Answer
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEwebapp-pod 1/1 Running 0 6m55smysql 1/1 Running 0 6m55scontrolplane ~ ➜ k get po webapp-pod -o yaml > webapp-pod.ymlcontrolplane ~ ✦2 ➜ k delete -f webapp-pod.yml $nowWarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "webapp-pod" force deletedcontrolplane ~ ✦2 ➜ k get poNAME READY STATUS RESTARTS AGEmysql 1/1 Running 0 8m1sFollow steps on how to configure all key-value pairs in a Secret as container environment variables from K8S docs.
## webapp-pod.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: "2023-12-29T17:57:07Z"labels:name: webapp-podname: webapp-podnamespace: defaultresourceVersion: "802"uid: b134a439-e86d-45d2-af52-717d17717da1spec:containers:- image: kodekloud/simple-webapp-mysqlimagePullPolicy: Alwaysname: webappenvFrom:- secretRef:name: db-secretcontrolplane ~ ✦2 ➜ k apply -f webapp-pod.ymlpod/webapp-pod createdcontrolplane ~ ✦2 ➜ k get poNAME READY STATUS RESTARTS AGEmysql 1/1 Running 0 13mwebapp-pod 1/1 Running 0 2s -
-
Identify the name of the containers running in the blue pod.
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEapp 1/1 Running 0 36sblue 0/2 ContainerCreating 0 7sfluent-ui 1/1 Running 0 37sred 0/3 ContainerCreating 0 23sAnswer
controlplane ~ ➜ k describe po blueName: blueNamespace: defaultPriority: 0Service Account: defaultNode: controlplane/192.36.217.6Start Time: Fri, 29 Dec 2023 13:12:32 -0500Labels: <none>Annotations: <none>Status: PendingIP:IPs: <none>Containers:teal:Container ID:Image: busyboxImage ID:Port: <none>Host Port: <none>Command:sleep4500State: WaitingReason: ContainerCreatingReady: FalseRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-vrvfc (ro)navy:Container ID:Image: busyboxImage ID:Port: <none>Host Port: <none>Command:sleep4500State: WaitingReason: ContainerCreatingReady: FalseRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-vrvfc (ro) -
Create a multi-container pod with 2 containers. If the pod goes into the crashloopbackoff then add the command sleep 1000 in the lemon container. Use the spec given below:
-
Name: yellow
-
Container 1 Name: lemon
-
Container 1 Image: busybox
-
Container 2 Name: gold
-
Container 2 Image: redis
Answer
ontrolplane ~ ➜ k run yellow --image busybox $doapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: yellowname: yellowspec:containers:- image: busyboxname: yellowresources: {}dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}controlplane ~ ➜ k run yellow --image busybox $do > yellow.yml## yellow.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: yellowname: yellowspec:containers:- image: busyboxname: lemonresources: {}- image: redisname: golddnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}controlplane ~ ➜ k apply -f yellow.ymlpod/yellow createdcontrolplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEapp 1/1 Running 0 8m39sblue 2/2 Running 0 7m34sfluent-ui 1/1 Running 0 8m39sred 3/3 Running 0 8m26syellow 1/2 CrashLoopBackOff 1 (4s ago) 7s## yellow.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: yellowname: yellowspec:containers:- image: busyboxname: lemonresources: {}command:- "sleep"- "1000"- image: redisname: golddnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}controlplane ~ ➜ k delete -f yellow.ymlpod "yellow" deletedcontrolplane ~ ➜ k apply -f yellow.ymlpod/yellow createdcontrolplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEapp 1/1 Running 0 9m44sblue 2/2 Running 0 8m39sfluent-ui 1/1 Running 0 9m44sred 3/3 Running 0 9m31syellow 2/2 Running 0 3scontrolplane ~ ➜ k describe po yellowName: yellowNamespace: defaultPriority: 0Service Account: defaultNode: controlplane/192.11.203.9Start Time: Sat, 30 Dec 2023 03:10:46 -0500Labels: run=yellowAnnotations: <none>Status: RunningIP: 10.244.0.13IPs:IP: 10.244.0.13Containers:lemon:Container ID: containerd://eba9ba778ac377f05a4061170c3229c0361103ab3dbad6710d28f32a95fc8f77Image: busyboxImage ID: docker.io/library/busybox@sha256:ba76950ac9eaa407512c9d859cea48114eeff8a6f12ebaa5d32ce79d4a017dd8Port: <none>Host Port: <none>Command:sleep1000State: RunningStarted: Sat, 30 Dec 2023 03:10:48 -0500Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-m8gtm (ro)gold:Container ID: containerd://f0ab41cb48bb10d4006833ecb452800b58974d578a869cfac5e6e7dbe2052029Image: redisImage ID: docker.io/library/redis@sha256:a7cee7c8178ff9b5297cb109e6240f5072cdaaafd775ce6b586c3c704b06458ePort: <none>Host Port: <none>State: RunningStarted: Sat, 30 Dec 2023 03:10:48 -0500Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-m8gtm (ro) -
-
Inspect the app pod and identify the number of containers in it. It is deployed in the elastic-stack namespace.
Answer
controlplane ~ ➜ k get po -n elastic-stackNAME READY STATUS RESTARTS AGEapp 1/1 Running 0 13melastic-search 1/1 Running 0 13mkibana 1/1 Running 0 13mcontrolplane ~ ➜ k describe po appName: appNamespace: defaultPriority: 0Service Account: defaultNode: controlplane/192.11.203.9Start Time: Sat, 30 Dec 2023 03:01:05 -0500Labels: name=appAnnotations: <none>Status: RunningIP: 10.244.0.8IPs:IP: 10.244.0.8Containers:app:Container ID: containerd://80c1ffb344a3f209d2014335e046bf7eef23c4d319274f047c7b8972ff578694Image: kodekloud/event-simulatorImage ID: docker.io/kodekloud/event-simulator@sha256:1e3e9c72136bbc76c96dd98f29c04f298c3ae241c7d44e2bf70bcc209b030bf9Port: <none>Host Port: <none>State: RunningStarted: Sat, 30 Dec 2023 03:02:20 -0500Ready: TrueRestart Count: 0Environment: <none>Mounts:/log from log-volume (rw)/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-s4djx (ro) -
The application in the elastic-stack namespace outputs logs to the file /log/app.log. View the logs and try to identify the user having issues with Login.
controlplane ~ ➜ k get po -n elastic-stackNAMESPACE NAME READY STATUS RESTARTS AGEelastic-stack app 1/1 Running 0 18melastic-stack elastic-search 1/1 Running 0 18melastic-stack kibana 1/1 Running 0 18mAnswer
controlplane ~ ➜ k -n elastic-stack exec -it app -- tail -10 /log/app.log[2023-12-30 08:18:12,459] INFO in event-simulator: USER3 is viewing page3[2023-12-30 08:18:12,725] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.[2023-12-30 08:18:12,725] INFO in event-simulator: USER4 is viewing page2[2023-12-30 08:18:13,460] INFO in event-simulator: USER3 is viewing page3[2023-12-30 08:18:13,726] INFO in event-simulator: USER3 logged in[2023-12-30 08:18:14,461] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.[2023-12-30 08:18:14,461] INFO in event-simulator: USER1 logged in[2023-12-30 08:18:14,727] INFO in event-simulator: USER4 logged in[2023-12-30 08:18:15,462] INFO in event-simulator: USER1 logged in[2023-12-30 08:18:15,729] INFO in event-simulator: USER3 logged in -
Edit the pod to add a sidecar container to send logs to Elastic Search. Mount the log volume to the sidecar container. Only add a new container. Do not modify anything else. Use the spec provided below.
-
Name: app
-
Container Name: sidecar
-
Container Image: kodekloud/filebeat-configured
-
Volume Mount: log-volume
-
Mount Path: /var/log/event-simulator/
-
Existing Container Name: app
-
Existing Container Image: kodekloud/event-simulator
controlplane ~ ➜ k get po -n elastic-stackNAMESPACE NAME READY STATUS RESTARTS AGEelastic-stack app 1/1 Running 0 18melastic-stack elastic-search 1/1 Running 0 18melastic-stack kibana 1/1 Running 0 18mAnswer
controlplane ~ ➜ k get po app -n elastic-stack -o yaml > elastic-app.ymlAdd a sidecar container to the YAML file. Follow K8S docs for steps.
## elastic-app.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: "2023-12-30T08:01:04Z"labels:name: appname: appnamespace: elastic-stackresourceVersion: "1071"uid: ee0727bd-ef91-4817-b717-1582c890cae7spec:containers:- image: kodekloud/event-simulatorimagePullPolicy: Alwaysname: appresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /logname: log-volume- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-78bvzreadOnly: true- name: sidecarimage: kodekloud/filebeat-configuredvolumeMounts:- name: log-volumemountPath: /var/log/event-simulator/controlplane ~ ✦ ✖ k delete -f elastic-app.yml $nowWarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "app" force deletedcontrolplane ~ ✦ ✖ vi elastic-app.ymlcontrolplane ~ ✦ ➜ k apply -f elastic-app.ymlpod/app createdcontrolplane ~ ✦ ➜ k get po -n elastic-stackNAME READY STATUS RESTARTS AGEapp 2/2 Running 0 21selastic-search 1/1 Running 0 43mkibana 1/1 Running 0 43m -
-
What is the image used by the initContainer in the blue pod? What is its state?
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEred 1/1 Running 0 32sgreen 2/2 Running 0 32sblue 1/1 Running 0 32sAnswer
controlplane ~ ➜ k describe po blueName: blueNamespace: defaultPriority: 0Service Account: defaultNode: controlplane/192.12.137.9Start Time: Sat, 30 Dec 2023 08:47:05 +0000Labels: <none>Annotations: <none>Status: RunningIP: 10.42.0.10IPs:IP: 10.42.0.10Init Containers:init-myservice:Container ID: containerd://a2eb2f74006ea5f3645334bdb11b774f1399e440e53a0a3105717eb260554740Image: busyboxImage ID: docker.io/library/busybox@sha256:ba76950ac9eaa407512c9d859cea48114eeff8a6f12ebaa5d32ce79d4a017dd8Port: <none>Host Port: <none>Command:sh-csleep 5State: TerminatedReason: CompletedExit Code: 0Started: Sat, 30 Dec 2023 08:47:08 +0000Finished: Sat, 30 Dec 2023 08:47:13 +0000Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7djbh (ro) -
What is the state of the purple POD?
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEred 1/1 Running 0 3m1sgreen 2/2 Running 0 3m1sblue 1/1 Running 0 3m1spurple 0/1 Init:0/2 0 40sAnswer
controlplane ~ ➜ k describe po purple | grep -i statusStatus: PendingType Status -
Update the pod red to use an initContainer that uses the busybox image and sleeps for 20 seconds
controlplane ~ ➜ k get poNAME READY STATUS RESTARTS AGEred 1/1 Running 0 5m27sgreen 2/2 Running 0 5m27sblue 1/1 Running 0 5m27spurple 0/1 Init:0/2 0 3m6sAnswer
controlplane ~ ➜ k get po red -o yaml > red.ymlAdd the initContainer in the YAML file. Follow K8s docs for steps.
## red.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: "2023-12-30T08:47:05Z"name: rednamespace: defaultresourceVersion: "909"uid: 2a18cd56-4b47-49f5-8a9b-a294020d2773spec:initContainers:- name: initcontainerimage: busyboxcommand: ['sh', '-c', 'sleep 20']containers:- command:- sh- -c- echo The app is running! && sleep 3600image: busybox:1.28imagePullPolicy: IfNotPresentname: red-containerresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-r4zhpreadOnly: truecontrolplane ~ ✦ ➜ k delete -f red.yml $nowWarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "red" force deletedcontrolplane ~ ✦ ➜ k get poNAME READY STATUS RESTARTS AGEgreen 2/2 Running 0 10mblue 1/1 Running 0 10mpurple 0/1 Init:0/2 0 7m53scontrolplane ~ ✦ ➜ k apply -f red.ymlpod/red createdcontrolplane ~ ✦ ➜ k get poNAME READY STATUS RESTARTS AGEgreen 2/2 Running 0 10mblue 1/1 Running 0 10mpurple 0/1 Init:0/2 0 8m2sred 0/1 Init:0/1 0 2scontrolplane ~ ✦ ➜ k get poNAME READY STATUS RESTARTS AGEgreen 2/2 Running 0 10mblue 1/1 Running 0 10mpurple 0/1 Init:0/2 0 8m33sred 1/1 Running 0 33s -
A new application orange is deployed. There is something wrong with it. Identify and fix the issue.
controlplane ~ ✦ ➜ k get poNAME READY STATUS RESTARTS AGEgreen 2/2 Running 0 11mblue 1/1 Running 0 11mpurple 0/1 Init:0/2 0 9m31sred 1/1 Running 0 91sorange 0/1 Init:Error 1 (6s ago) 7sAnswer
controlplane ~ ✦2 ➜ k logs orangeDefaulted container "orange-container" out of: orange-container, init-myservice (init)Error from server (BadRequest): container "orange-container" in pod "orange" is waiting to start: PodInitializingcontrolplane ~ ✦2 ➜ k get po orange -o yaml > orange.ymlInspect the YAML file. We can see that the command is wrong.
## orange.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: "2023-12-30T08:58:50Z"name: orangenamespace: defaultresourceVersion: "1228"uid: 79f7f539-c1e2-4f74-9ebe-cb6065cc49d2spec:containers:- command:- sh- -c- echo The app is running! && sleep 3600image: busybox:1.28imagePullPolicy: IfNotPresentname: orange-containerresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-9d8threadOnly: truednsPolicy: ClusterFirstenableServiceLinks: trueinitContainers:- command:- sh- -c- sleeeep 2;image: busyboximagePullPolicy: Alwaysname: init-myserviceresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-9d8threadOnly: trueFix the command in initcontainer.
## orange.ymlapiVersion: v1kind: Podmetadata:creationTimestamp: "2023-12-30T08:58:50Z"name: orangenamespace: defaultresourceVersion: "1228"uid: 79f7f539-c1e2-4f74-9ebe-cb6065cc49d2spec:containers:- command:- sh- -c- echo The app is running! && sleep 3600image: busybox:1.28imagePullPolicy: IfNotPresentname: orange-containerresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-9d8threadOnly: truednsPolicy: ClusterFirstenableServiceLinks: trueinitContainers:- command:- sh- -c- sleep 2;image: busyboximagePullPolicy: Alwaysname: init-myserviceresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-9d8threadOnly: truecontrolplane ~ ✦2 ➜ k delete -f orange.yml $nowWarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "orange" force deletedcontrolplane ~ ✦2 ➜ k apply -f orange.ymlpod/orange createdcontrolplane ~ ✦2 ➜ k get poNAME READY STATUS RESTARTS AGEgreen 2/2 Running 0 20mblue 1/1 Running 0 20mred 1/1 Running 0 10mpurple 0/1 Init:1/2 0 18morange 0/1 Init:0/1 0 2scontrolplane ~ ✦2 ➜ k get poNAME READY STATUS RESTARTS AGEgreen 2/2 Running 0 20mblue 1/1 Running 0 20mred 1/1 Running 0 10mpurple 0/1 Init:1/2 0 18morange 1/1 Running 0 5s