IN YAML , we keep things in Key: value format
here keys are Fruit, Vegetable etc & values are Apple, Carrot etc.
Also , this is how array & dictionary is placed in YAML:
Note: You need to make sure the gaps are formatted well.
Also, you may have a mix up of all of these:
Let's check an example where we can use YAML to represent that one
Example 1:
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:
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
and this is basically list of dictionaries.
Let's create a pod using YAML fle
Let's create a file called pods-definition.yaml
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.
We will use the apiVersion as V1 for now.
For the kind, we will use "pod" as we are creating a pod.
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.
Look, there are strings for some values and dictionaries for some.
You can also add additional information for labels:
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.
Also we can have additional information for a container like resources, ports etc.
Now lets create a pod. For that, use this codes:
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
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 "
kubectl describe pod myapp-pod1
You can delete the pod too using "kubectl delete pod "
kubectl delete pod myaap-pod
And then check if it is available or not:
kubectl get pods
Another hack for you if you use VS code;
For the pod.yaml, press this outline in bottom-left:
You can see :
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 .
Top comments (1)
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