Kubernetes, Beginning with PODs

ustundagsemih

ustundagsemih

Posted on July 1, 2020

Kubernetes, Beginning with PODs

I am planning to prepare for the CKA exam. Since one of the best methods to learn for me is to write down what I have learned, I decided to write them as blog posts. So let's get started!

post-full-content pre { margin: 0;}

What is a POD?

A Pod is the smallest deployable unit that can be created and managed in Kubernetes. In the official documentation you can find much more theoretical information about Pods. In this post I try to go over practical usage of Pods.

How to create and run a POD?

There are mainly 2 ways of creating Pods in Kubernetes. One of them is using kubectl command. kubectl is a command line tool which lets you control Kubernetes clusters. For more information, please check the documentation

With the command below, we are creating a Pod labeled as nginx and using an image nginx.

$ kubectl run nginx --image nginx
pod/nginx created

If we check what Pods are running in our cluster, we can see that the Pod is running.

$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m45s

We can also check the details of the Pod by running describe argument.

$ kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node01/172.17.0.18
Start Time:   Sun, 28 Jun 2020 17:05:41 +0000
Labels:       run=nginx
Annotations:  <none>
Status:       Running
IP:           10.244.1.3
...
...

In the output, we can see which node is our Pod is running at, which image has been used etc. It also shows events related to the Pod. If there is an error, we could easily see it.

...
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  15m   default-scheduler  Successfully assigned default/nginx to node01
  Normal  Pulling    15m   kubelet, node01    Pulling image "nginx"
  Normal  Pulled     15m   kubelet, node01    Successfully pulled image "nginx"
  Normal  Created    15m   kubelet, node01    Created container nginx
  Normal  Started    15m   kubelet, node01    Started container nginx

Let's delete this Pod and create another one with the second method, by writing yaml files.
To delete a Pod we can simply run the command below.

$ kubectl delete pods nginx
pod "nginx" deleted
$ kubectl get pods
No resources found in default namespace.

Now create a file named as pod.yml and write some yaml.

apiVersion: v1
kind: Pod
metadata:
 name: my-nginx
 labels:
  app: my-first-app
  type: frontend
spec:
 containers:
  - name: nginx-container
    image: nginx123

Here we basically specify what kind of resource we want to create, which is Pod.
With metadata we specify a name for our Pod as well as apply some labels to it. Labels are handy in an environment where you have hundreds of Pods.
With spec section, we specify a name for our container and which image to use.
Now let's create a Pod with our definition file.

$ kubectl apply -f pod.yml
pod/my-nginx created

It successfully created the Pod. Let's check the status of it.

$ kubectl get pods
NAME       READY   STATUS         RESTARTS   AGE
my-nginx   0/1     ErrImagePull   0          58s

In STATUS column we can see that there is an error. Let's describe the Pod to get more information in the events section.

$ kubectl describe pods my-nginx
...
...
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  2m47s                default-scheduler  Successfully assigned default/my-nginx to node01
  Normal   Pulling    49s (x4 over 2m22s)  kubelet, node01    Pulling image "nginx123"
  Warning  Failed     48s (x4 over 2m21s)  kubelet, node01    Failed to pull image "nginx123": rpc error: code = Unknown desc = Error response from daemon: pull access denied for nginx123, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
  Warning  Failed     48s (x4 over 2m21s)  kubelet, node01    Error: ErrImagePull
  Normal   BackOff    34s (x6 over 2m20s)  kubelet, node01    Back-off pulling image "nginx123"
  Warning  Failed     23s (x7 over 2m20s)  kubelet, node01    Error: ImagePullBackOff

Here we can see that there is no image named as nginx123. Now let's try to resolve this problem. To solve it, we simply need to edit the definition file with the correct image name and apply it.

spec:
 containers:
  - name: nginx-container
    image: nginx
$ kubectl apply -f pod.yml
pod/my-nginx configured

Now if we check the status, we can see there is no error.

$ kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
my-nginx   1/1     Running   0          6m47s
💖 💪 🙅 🚩
ustundagsemih
ustundagsemih

Posted on July 1, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related