CKAD-Exercises

CKAD Practice Questions: Application Deployment

Overview

This section focuses on the Application Deployment domain of the Certified Kubernetes Application Developer (CKAD) exam. It covers strategies, tools, and techniques for deploying and managing applications in Kubernetes clusters. Understanding deployment strategies, rolling updates, and tools like Helm and Kustomize is crucial for success.


Topics Covered


Questions

1. Implement a blue/green deployment strategy

Scenario:

Solution:

Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: app-blue spec: replicas: 3 selector: matchLabels: app: my-app version: blue template: metadata: labels: app: my-app version: blue spec: containers: - name: app-container image: my-app:1.0 --- apiVersion: apps/v1 kind: Deployment metadata: name: app-green spec: replicas: 3 selector: matchLabels: app: my-app version: green template: metadata: labels: app: my-app version: green spec: containers: - name: app-container image: my-app:2.0 --- apiVersion: v1 kind: Service metadata: name: my-app-service spec: selector: app: my-app version: blue ports: - protocol: TCP port: 80 targetPort: 8080 ``` #### Steps to Apply: 1. Save the YAML file and apply it: ```bash kubectl apply -f blue-green-deployment.yaml ``` 2. Switch traffic to the `app-green` deployment by updating the Service selector: ```bash kubectl patch service my-app-service -p '{"spec":{"selector":{"app":"my-app","version":"green"}}}' ```

2. Implement a canary deployment strategy

Scenario:

Solution:

Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: app-stable spec: replicas: 9 selector: matchLabels: app: my-app version: stable template: metadata: labels: app: my-app version: stable spec: containers: - name: app-container image: my-app:1.0 --- apiVersion: apps/v1 kind: Deployment metadata: name: app-canary spec: replicas: 1 selector: matchLabels: app: my-app version: canary template: metadata: labels: app: my-app version: canary spec: containers: - name: app-container image: my-app:2.0 --- apiVersion: v1 kind: Service metadata: name: my-app-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 ``` #### Steps to Apply: 1. Deploy the stable and canary versions: ```bash kubectl apply -f canary-deployment.yaml ``` 2. Gradually scale up the canary deployment and scale down the stable deployment: ```bash kubectl scale deployment app-canary --replicas=5 kubectl scale deployment app-stable --replicas=5 ```

3. Configure a rolling update for an application

Scenario:

Solution:

Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: rolling-update-app spec: replicas: 3 selector: matchLabels: app: rolling-app template: metadata: labels: app: rolling-app spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80 strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1 ``` #### Steps to Apply: 1. Save and apply the YAML file: ```bash kubectl apply -f rolling-update.yaml ``` 2. Verify the rollout: ```bash kubectl rollout status deployment/rolling-update-app ```

4. Deploy an application using Helm

Scenario:

Solution:

Helm Command ```bash helm create helm-app ``` Edit the `values.yaml` file: ```yaml replicaCount: 3 image: repository: nginx tag: latest pullPolicy: IfNotPresent ``` Deploy the Helm chart: ```bash helm install helm-app ./helm-app ``` #### Steps to Verify: 1. Verify the Helm release: ```bash helm list ``` 2. Check the pods: ```bash kubectl get pods -l app.kubernetes.io/name=helm-app ```

5. Rollback a failed deployment

Scenario:

Solution:

Command Rollback the deployment: ```bash kubectl rollout undo deployment/test-deploy ``` #### Steps to Verify: 1. Check the rollout history: ```bash kubectl rollout history deployment/test-deploy ``` 2. Ensure the pods are running the previous stable version: ```bash kubectl get pods ```

6. Perform a staged rollout with incremental updates

Scenario:

Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: staged-rollout-app spec: replicas: 4 selector: matchLabels: app: staged-rollout template: metadata: labels: app: staged-rollout spec: containers: - name: app-container image: staged-rollout:1.0 strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1 ``` #### Steps to Apply Save the YAML file and deploy the initial version: ```bash kubectl apply -f staged-rollout.yaml ``` Incrementally update the deployment to the new version: ```bash kubectl set image deployment/staged-rollout-app app-container=staged-rollout:1.1 ``` Monitor the rollout status: ```bash kubectl rollout status deployment/staged-rollout-app ```

7. Deploy an application using Helm with custom values

Scenario:

Command #### Create and customize the Helm chart ```bash helm create custom-helm-app ``` Edit the `values.yaml` file: ```yaml replicaCount: 5 image: repository: nginx tag: 1.20 pullPolicy: IfNotPresent ``` Deploy the Helm chart with the custom values: ```bash helm install custom-helm-app ./custom-helm-app ``` #### Steps to Verify 1. List Helm releases: ```bash helm list ``` 2. Check the pods created by the Helm deployment: ```bash kubectl get pods -l app.kubernetes.io/name=custom-helm-app ```

8. Rollback a Helm release to a previous revision

Scenario:

Command #### Rollback the Helm release ```bash helm rollback example-app 1 ``` #### Steps to Verify 1. Check the Helm release history: ```bash helm history example-app ``` 2. Confirm the pods reflect the rollback: ```bash kubectl get pods -l app.kubernetes.io/name=example-app ```

9. Perform a rolling update for a StatefulSet

Scenario:

Declarative YAML Configuration ```yaml apiVersion: apps/v1 kind: StatefulSet metadata: name: stateful-app spec: serviceName: "stateful-service" replicas: 3 selector: matchLabels: app: stateful template: metadata: labels: app: stateful spec: containers: - name: redis image: redis:7.0 ``` #### Steps to Apply Save the YAML file and update the StatefulSet: ```bash kubectl apply -f stateful-update.yaml ``` Monitor the pod updates sequentially: ```bash kubectl rollout status statefulset/stateful-app ```

10. Deploy a workload using Helm with external secrets

Scenario:

External Secret YAML Configuration ```yaml apiVersion: v1 kind: Secret metadata: name: db-credentials namespace: default stringData: username: admin password: securepassword ``` Deploy the Secret: ```bash kubectl apply -f external-secret.yaml ``` #### Helm Chart Configuration Customize `values.yaml` in the Helm chart: ```yaml environment: DB_USER: DB_PASSWORD: ``` Deploy the Helm chart: ```bash helm install secret-helm-app ./helm-chart ``` #### Steps to Verify 1. Ensure the Secret is mounted correctly: ```bash kubectl describe pod -l app.kubernetes.io/name=secret-helm-app ``` 2. Verify the application is running: ```bash kubectl get pods ```

Notes and Tips

  1. Deployment Strategies:
    • Blue/green deployments require two separate environments. Use Service objects to switch traffic between versions.
    • Canary deployments can be implemented using nginx-ingress or service mesh solutions like Istio.
  2. Rolling Updates:
    • Use kubectl rollout status to monitor updates.
    • Always test updates in a non-production environment before applying them.
  3. Helm Tips:
    • Use helm lint to validate your Helm charts before deployment.
    • Always version-control your values.yaml file.

Resources

  1. Kubernetes Official Documentation
  2. Helm Official Documentation
  3. CKAD Exam Curriculum