CKA & CKAD Series (Part2): Basics of Yaml & creating a pod using yaml
Tags: kubernetes, yaml
Published: 2022-07-29 07:22:00

IN YAML , we keep things in **Key: value** format
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzfdxdanja86utykqyss.png)
here keys are Fruit, Vegetable etc  & values are Apple, Carrot etc.

Also , this is how array & dictionary is placed in YAML:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs1y1lmsyc71s154ekbu.png)

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

Also, you may have a mix up of all of these:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7mjzxo64su934z2x61k.png)

Let's check an example where we can use YAML to represent that one
Example 1:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6muw9czp4055jngc5jk3.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y1o2rltjwya7dnwuf8c.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9n6fiaosi962symddof.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8ms3ojtnhuw7p6dsgrf.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5uaoshkb4raqv55nmain.png)
We will use the apiVersion as V1 for now.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lku0i6cqe3tyd26lsv0k.png)
 
 For the kind, we will use "pod" as we are creating a pod.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6g9dkefvu8mgtc1gwmum.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxhii1arr18pzs1kxmzj.png)
Look, there are strings for some values and dictionaries for some.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3382mmbnzr3mq1iyqke.png)
You can also add additional information for labels:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apr2yoz2p2zasbwkiwh7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47k1orr5736kywsycxzu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbl80zbqdel1va75klkl.png)

Also we can have additional information for a container like resources, ports etc.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27vl0zu00cwry8zz0ijr.png)
 
Now lets create a pod. For that, use this codes:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtvf9t9s7t28g76qrhnk.png)
 
```
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod1
  labels:
    name: myapp1
spec:
  containers:
  - name: nginx-container
    image: nginx

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

```
kubectl apply -f pod.yml
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rb7x6qjqrx77o6pdul09.png)
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
```
Also to know more about a particular pod, you can use this command :


"kubectl describe pod <pod name>"

```
kubectl describe pod myapp-pod1
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwevrtobh1i0tb9idpic.png)

You can delete the pod too using "kubectl delete pod <pod name>"

```
kubectl delete pod myaap-pod
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4t4pz11wbsf2omit64pe.png)
 
And then check if it is available or not:

```
kubectl get pods
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/extaurg5k28hl0pmez2c.png)
 
Another hack for you if you use VS code;
For the pod.yaml, press this outline in bottom-left:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylqjy9d8r6t5bdpakehq.png)
You can see :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byr0jisrhqt0sdc7hrn3.png)
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
<img src="https://media0.giphy.com/media/Nfs5y1kzJKfu/giphy.gif?cid=ecf05e472d3w835ptrri5m1p6ifnwl9mi7ope9pifvhpcxf6&rid=giphy.gif&ct=g">