Showing posts with label debug. Show all posts
Showing posts with label debug. Show all posts

Debugging your pod on Kubernetes?

 Debugging the pods on Kubernetes




Debugging is very important to identify the issue in Pod. sometimes your application may behave differently /wrong. Your Pod has stopped or some other issues happening inside your Pod. You can always debug that to identify the issue and fix it. 

So the most basic thing we do to debug the issue is to start with checking the logs. Logs are very crucial part and play very important role in any application. If anything goes wrong we can always check the logs and analyse it. Similar to above we are going to check the logs, events and definition of Pod to identify the issues. 

To start with it, we first need to run the pod. You can follow below command to run the pod.


kubectl run mypod --image=nginx


Here mypod is name of the pod and nginx image will be used.

Check if the pod is running. 

kubectl get pods


Result below:

NAME    READY   STATUS              RESTARTS   AGE

mypod   0/1     ContainerCreating   0          5s


It says container creating state. We may wait and see if it is completed and running state.

NAME    READY   STATUS    RESTARTS   AGE

mypod   1/1     Running   0          21s


So it's in running state. We can also use yaml file to create the pod.

Let's break something and see what we issues and status we get. I'll delete the pod and recreate it with the wrong image name.

To delete the Pod use below command:


kubectl delete pod mypod

pod "mypod" deleted


Now since the pod has been delete let's recreate it with wrong image.

kubectl run mypod --image=nginx-myimage-123


Run it and pod will be created successfully. But wait did we check the status if the pod is running. 
Check it using Kubectl get pods command.

kubectl get pods

NAME    READY   STATUS         RESTARTS   AGE

mypod   0/1     ErrImagePull   0          9s


You can identify under the status column that there is an error. It says ErrImagePull. We can guess that there is an error during image pull. We know that because we put that wrong image name. But in real life scenario we don't know about if our image is wrong. So we can check the events. 

To check the events we can use describe command:

kubectl describe pod mypod


Once we run this command it will give us result in key/value pair format. Just scroll to the bottom and look for the events. 

Events:

  Type     Reason     Age                From               Message

  ----     ------     ----               ----               -------

  Normal   Scheduled  34s                default-scheduler  Successfully assigned default/mypod to docker-desktop

  Normal   BackOff    26s                kubelet            Back-off pulling image "nginx-myimage-123"

  Warning  Failed     26s                kubelet            Error: ImagePullBackOff

  Normal   Pulling    12s (x2 over 33s)  kubelet            Pulling image "nginx-myimage-123"

  Warning  Failed     6s (x2 over 27s)   kubelet            Failed to pull image "nginx-myimage-123": rpc error: code = Unknown desc = Error response from daemon: pull access denied for nginx-myimage-123, repository does not exist or may require 'docker login': denied: requested access to the resource is denied

  Warning  Failed     6s (x2 over 27s)   kubelet            Error: ErrImagePull


Here we can see it says either repository does not exist or we don't have access to it. 

So checking the events is very important step. This can tell us what happened when we ran our kubectl run command. There is one more way by which we can check and that is logs. By checking logs can give us some insights. Sometimes we don't get much information from the events and in those scenarios checking logs can be very helpful if we can find something. We can check the log in this case as well but since the issue is only with image. We used wrong image name and this issue was very much understandable from the events. But let's check the logs anyway by running below command.

kubectl logs mypod


Result:

Error from server (BadRequest): container "mypod" in pod "mypod" is waiting to start: trying and failing to pull image


In logs also we can check that its saying "trying and failing to pull image" step.

If we have specific container failing inside the pod then you can run below command.

kubectl logs mypod container_name                           

If our container has ever crashed previously then we can use --previous flag in command.

kubectl logs --previous mypod container_name                 

Conclusion 

These are the basic ways by which we can check and fix the issues in Pod. Events can be very helpful but sometimes logs can be more helpful. So it depends on what is the issue. It's always better to check for events and then go for logs. 

Hope this can be helpful!

Note: If you think this helped you and you want to learn more stuff on devops, then I would recommend joining the Kodecloud devops course and go for the complete certification path by clicking this link

Docker series for beginners: How to debug your first container part 1

 Today we are going to learn how to debug your container on docker. Sometimes when you run a container ,your command gets executed successfully but you identify that your container is not running. 




What could be the issues that container is not running or exited abruptly ,we'll see today:

If you haven't gone through previous article where I explained how to run your first container then I'll suggest plz go through that article then start on this page. Here is the link below:

Running first container on docker

Now I am assuming you already know how to pull the image from docker hub and how to run it inside docker container.

Example: 

I am going to run alpine linux image I downloaded on my system. Now I'll use the inspect command to check whats inside the image. First check what are the images and out of those image which one we want to inspect.

docker image ls

docker image ls

REPOSITORY                       TAG           IMAGE ID       CREATED       SIZE

alpine                           latest        bb3de5531c18   2 weeks ago   5.34MB

mcr.microsoft.com/mssql/server   2019-latest   56beb1db7406   7 weeks ago   1.54GB

I have two images now I want to inspect the Alpine image. You can use image name or image id to inspect.

docker image inspect Alpine

or

docker image inspect bb3de5531c18

docker image inspect bb3de5531c18

[

    {

        "Id": "sha256:bb3de5531c18f185667b0be0e400ab24aa40f4440093de82baf4072e14af3b84",

        "RepoTags": [

            "alpine:latest"

        ],

        "RepoDigests": [

            "alpine@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a"

        ],

        "Parent": "",

        "Comment": "",

        "Created": "2021-08-27T17:39:33.633925527Z",

        "Container": "789638f977e407396e28e5c30280983578253237755af8

Just go through all the details. Now let run this image. I am going run this image in container with errors.

So I am going to write a wrong command below:

docker run alpine 

docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

No container is running. Strange that's how we run the container. Lets check the container logs if we see something. First check how many containers are in running state or stopped. If we use below command we'll know.

docker ps -a

docker ps -a

CONTAINER ID   IMAGE                                        COMMAND                  CREATED          STATUS                      PORTS     NAMES

ac001968f4fc   alpine                                       "/bin/sh"                2 minutes ago    Exited (0) 45 seconds ago             confident_heisenberg

Now take the container id and check the logs.

docker container logs 71f2364b452c

$

Nothing is there in logs. Let's inspect the container.

docker container inspect 71f2364b452c

[

    {

        "Id": "71f2364b452c57011481cc6c696e5fdae0af0b31ca8f404f899d474acbe",

        "Created": "2021-09-15T07:26:10.800075211Z",

        "Path": "/bin/sh",

        "Args": [],

        "State": {

            "Status": "exited",

            "Running": false,

            "Paused": false,

            "Restarting": false,

            "OOMKilled": false,

            "Dead": false,

            "Pid": 0,

            "ExitCode": 0,

            "Error": "",

No error!

Now think, what we are trying to do? We are trying to run the Alpine Linux and we need to enter into the Alpine shell. But we are not doing it and then logs were not generated. 

Let's do it right way.

docker run -it alpine /bin/sh 

docker run -it alpine /bin/sh        

/ # whoami

root

/ # ls

bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var

/ # ps

PID   USER     TIME  COMMAND

    1 root      0:00 /bin/sh

   10 root      0:00 ps

/ # 

Check logs:

docker container logs 5cb4aaa4e5c5


docker container logs 5cb4aaa4e5c5

/ # whoami

root

/ # ls

bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var

/ # ps

PID   USER     TIME  COMMAND

    1 root      0:00 /bin/sh

   10 root      0:00 ps

/ # exit

Logs are rolling now.

We have successfully learned what to check when something is not wrong. Always check for docker logs, docker container inspect and image inspect also would be helpful.

Note: If you think this helped you and you want to learn more stuff on devops, then I would recommend joining the Kodecloud devops course and go for the complete certification path by clicking this link

Quantum Computing: The Future of Supercomputing Explained

  Introduction Quantum computing is revolutionizing the way we solve complex problems that classical computers struggle with. Unlike tradi...