> ## Content Index
> Fetch the complete content index at: https://scriptcrunch.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# CKA Series: Create a Pod Using Dry Run Flag
- URL: https://scriptcrunch.com/cka-create-pod-dry-run/
- Published: 2023-10-21T18:42:30.000Z
- Updated: 2026-06-23T08:55:57.000Z
- Author: Aswin Vijayan
- Tags: #Migrated-1758801465347, #wp, #wp-post, #Import 2025-09-25 11:57

In this CKA Series blog, we will look at the steps to create a [Kubernetes](https://scriptcrunch.com/tag/kubernetes/) pod using the dry run flag with an init container.

This method is particularly useful when you appear for [Kubernetes certifications](https://scriptcrunch.com/tag/kubernetes-certifications/) like [CKA](https://scriptcrunch.com/kubernetes-exam-guide/).

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.

```yaml
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](https://scriptcrunch.com/tag/kubernetes/) 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](https://scriptcrunch.com/linux-foundation-coupon/) page to get up to 50% CKA discount.