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
  creationTimestamp: null
    run: test-pod
  name: test-pod
  - image: nginx
    name: test-pod
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

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!”


  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.


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
  name: webserver
  namespace: default
  - name: init-webserver
    image: busybox
    command: ['sh', '-c', 'echo "Welcome to our web server!" > /usr/share/nginx/html/welcome.txt']
    - name: html
      mountPath: /usr/share/nginx/html
  - name: webserver
    image: nginx
    - name: html
      mountPath: /usr/share/nginx/html
  - 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