CKA Series: Create a Pod Using Dry Run Flag

In this CKA Series blog, we will look at the steps to create a Kubernetes pod using the dry run flag with an init container.

This method is particularly useful when you appear for Kubernetes certifications like CKA.

The –dry-run=client flag is used with kubectl to create the pod YAML.

Here is an example. test-pod is the pod name.

kubectl run test-pod  --image=nginx --dry-run=client -oyaml

You will get an output like the following.

root@controlplane:#  kubectl run test-pod  --image=nginx --dry-run=client -oyaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: test-pod
  name: test-pod
spec:
  containers:
  - image: nginx
    name: test-pod
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
root@controlplane:/home/ubuntu# 

During the CKA exam, you will be asked to deploy pods with custom values. First, you need to create the YAML using dry-run flag and then redirect it to a file. Let’s look at a scenario.

Example Scenario

You are responsible for deploying a new web server in your Kubernetes cluster. The requirements are as follows:

  • Create a Pod named webserver in the default namespace.
  • The Pod should run a container using the nginx image.
  • Before the nginx container starts, an init container should run to completion.
  • The init container should be named init-webserver and use the busybox image.
  • The init container must execute a shell command to create a file named welcome.txt in the /usr/share/nginx/html directory. The file should contain the text “Welcome to our web server!”

Tasks:

  1. Write the YAML definition for the Pod, including both the main container and the init container.
  2. Apply the YAML to create the Pod.
  3. Verify that the init container ran successfully and that the nginx container is running.
  4. Confirm that the welcome.txt file exists in the specified directory within the Pod.

Solution

Let’s take a look at how to solve the above scenario.

First, create a pod YAML using the dry run and write it to a file named webserver-pod.yaml

kubectl run webserver --image=nginx --dry-run=client -o yaml > webserver-pod.yaml

Next open webserver-pod.yaml file and add the init container spec as shown below. If you don’t remember the spec, you can use the official k8s documentation.

apiVersion: v1
kind: Pod
metadata:
  name: webserver
  namespace: default
spec:
  initContainers:
  - name: init-webserver
    image: busybox
    command: ['sh', '-c', 'echo "Welcome to our web server!" > /usr/share/nginx/html/welcome.txt']
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  containers:
  - name: webserver
    image: nginx
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  volumes:
  - name: html
    emptyDir: {}

Now you can deploy the pod using the following command.

kubectl apply -f webserver-pod.yaml

The task is pretty easy but it’s about time for the CKA exam. So practice the scenario well. Try to use aliases as much as possible.

If you are registering for the CKA certification don’t forget to use the coupon code from the Linux Foundation Coupon page to get up to 50% CKA discount.

Other Interesting Blogs

Leave a Comment

Share via
Copy link
Powered by Social Snap