DEV Community

Cover image for CKA & CKAD Series (Part2): Basics of Yaml & creating a pod using yaml
Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on • Updated on

CKA & CKAD Series (Part2): Basics of Yaml & creating a pod using yaml

IN YAML , we keep things in Key: value format
Image description
here keys are Fruit, Vegetable etc & values are Apple, Carrot etc.

Also , this is how array & dictionary is placed in YAML:
Image description

Note: You need to make sure the gaps are formatted well.

Also, you may have a mix up of all of these:
Image description

Let's check an example where we can use YAML to represent that one
Example 1:
Image description
You can see the keys color, model, transmission & price. All of then had their different values. Even the model is not a key here. Rather a dictionary which has keys Name & Year in it.

SO, this is how we use YAML.

Example 2:
Image description
Here we we have added all the cars names with colors in the list format.

But when we would like to have every details of them, we will use dictionary format here. Like this

Image description
and this is basically list of dictionaries.

Let's create a pod using YAML fle

Let's create a file called pods-definition.yaml
Image description
For a pod,we must have these contents. There might be different values for different things but for our purpose we will just use this one.
Image description
We will use the apiVersion as V1 for now.

Image description

For the kind, we will use "pod" as we are creating a pod.
Image description

For the metadata, we are going to provide additional information like name of the pod and labels we want to add so that we can filter later among many many pods to find this one.
Image description
Look, there are strings for some values and dictionaries for some.
Image description
You can also add additional information for labels:
Image description

Now, lets check the spec one:

Here we are going so set the containers list etc. For this pod, we just want 1 container to be created. We are going to use "nginx" image with "nginx-container" as the name of the container.
lOOK, we have formatted it in list format. Because there might be several containers in a pod.
Image description

Image description

Also we can have additional information for a container like resources, ports etc.
Image description

Now lets create a pod. For that, use this codes:

Image description

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod1
  labels:
    name: myapp1
spec:
  containers:
  - name: nginx-container
    image: nginx

Enter fullscreen mode Exit fullscreen mode

Lets save it and go to our terminal and then run this command to create the pod

kubectl apply -f pod.yml
Enter fullscreen mode Exit fullscreen mode

Image description
We have used -f to force it to use pod.yml to create a pod using apply command.

You can check the pod using:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Also to know more about a particular pod, you can use this command :

"kubectl describe pod "

kubectl describe pod myapp-pod1
Enter fullscreen mode Exit fullscreen mode

Image description

You can delete the pod too using "kubectl delete pod "

kubectl delete pod myaap-pod
Enter fullscreen mode Exit fullscreen mode

Image description

And then check if it is available or not:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Image description

Another hack for you if you use VS code;
For the pod.yaml, press this outline in bottom-left:
Image description
You can see :
Image description
This means that, metadata,labels, spec etc are in dictionary format {}

but containers has the type array/list.

Why did I share this?
Just to make you comfortable with this YAML file formats .

Done for this blog

Top comments (1)

Collapse
 
jhawithu profile image
Alok Kumar

Very nice on CKA. I also created a dedicated course on Udemy for CKA exam and more than 1k+ people get benefited

My CKA Course