Connect to kubernetes Pods ( container )and Look at the logs of the container

Hello Team ,

I have specific question as to how we can connect to container/pod and look at the logs from the container .

Question
1)I have created a deployment with 3 replicas .

Now when i say kubectl logs -f nginx-bf5d5cf98-qhbk5

I am looking at the logs from the container or the application running within the container ? The application that is running within the pod is nginx.

PS C:\Users\kkaushal> kubectl get pods
NAME READY STATUS RESTARTS AGE
event-simulator 2/2 Running 0 6m43s
nginx-bf5d5cf98-6zp64 1/1 Running 0 4h57m
nginx-bf5d5cf98-bfqgf 1/1 Running 0 4h57m
nginx-bf5d5cf98-qhbk5 1/1 Running 0 4h57m
nginx-cas-68f7fcd8b7-hxwjh 0/1 Pending 0 3h30m
php-apache-678865dd57-4fp9j 1/1 Running 0 4h57m
PS C:\Users\kkaushal> kubectl logs -f nginx-bf5d5cf98-qhbk5
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/01/19 05:58:33 [notice] 1#1: using the “epoll” event method
2025/01/19 05:58:33 [notice] 1#1: nginx/1.27.3
2025/01/19 05:58:33 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2025/01/19 05:58:33 [notice] 1#1: OS: Linux 5.15.0-1075-azure
2025/01/19 05:58:33 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2025/01/19 05:58:33 [notice] 1#1: start worker processes
2025/01/19 05:58:33 [notice] 1#1: start worker process 30
2025/01/19 05:58:33 [notice] 1#1: start worker process 31
2025/01/19 05:58:33 [notice] 1#1: start worker process 32
2025/01/19 05:58:33 [notice] 1#1: start worker process 33

2)How can we connect to the container within the POD ?

  1. When you run kubectl logs ... you’ll view container logs within the pod, specifically the output of the stdout and stderr streams from the container. Since the application running inside the container in nginx, these logs will reflect logs generated by nginx.

  2. You can use kubectl exec ... command to connect to a container within a pod.

2 Likes

As a follow on from this, if you are writing an application to run in a container, you should effectively print all log information to the console and not to a file. Console output from containers is automatically captured (in docker too, not just kubernetes) in the way described by Srikanth.

You’ll be looking to the application Logs. If you want the Logs for the container OS you should look at syslog or dmesg inside it.

2)How can we connect to the container within the POD ?

If by “connect” you mean a shell session, you should try kubectl exec -it $POD — /bin/sh.
However, if you’d like to access the service endpoint (HTTP) then you should go to the node and try curl the container IP:port.

  1. Find the node: kubectl get pod -o wide (Will show the internal IP and the node where the pod is hosted);
  2. SSH into that node;
  3. Try to connect with curl: curl -XGET $IP:$PORT

To clarify what you see when you run kubectl logs you get whatever is printed to the screen by what ever is running in the container, if the container had a screen (which it doesn’t), e.g.

  • Anything in a shell script that would produce console output
  • print() statements from a python script
  • cosole.log from a javascript app

etc.

1 Like