CKA Series: Create a Pod Using Dry Run Flag
- Last Updated On: October 21, 2023
- By: Aswin Vijayan
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 thedefault
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 thebusybox
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:
- Write the YAML definition for the Pod, including both the main container and the init container.
- Apply the YAML to create the Pod.
- Verify that the init container ran successfully and that the
nginx
container is running. - 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.
Aswin Vijayan
Other Interesting Blogs
[50% OFF] Kubernetes Certification Coupons (CKA, CKAD, CKS, KCNA and KCSA)
Looking for CKA coupon and other Kubernetes certification coupon codes? You have come to the right place. Following are the list of
[80% OFF] Linux Foundation Coupon for December 2024
Looking for CKA Coupon and other Linux Foundation certifications coupons? We got you covered. With the latest Linux Foundation Coupon, you can
[65% OFF] Linux Foundation LFCA, LFCS & LFCT Exam Voucher Codes
Linux Foundation has announced up to a $284 discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux