Logs in Kubernetes

If the pod is running, we can view the logs using kubectl logs command with the pod name. Use the -f option to stream the logs live.

apiVersion: v1
kind: Pod
metadata:
  name: logs-pod
  labels:
    app: logs-pod
spec:
  containers:
    - name: logs-pod-container
      image: jayjodev/k8s-node-app:latest
      ports:
        - containerPort: 4000
          protocol: TCP
$ kubectl create -f logs-pod.yaml 
pod/logs-pod created
$ kubectl logs -f event-pod
kubectl logs -f logs-pod

> k8s-service@1.0.0 start
> node index.js

Lunch app is listening on port !4000
process.env is: {"KUBERNETES_PORT":"tcp://10.96.0.1:443","KUBERNETES_SERVICE_PORT":"443","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP_PORT":"80","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP_PROTO":"tcp","npm_config_user_agent":"npm/7.3.0 node/v15.5.1 linux x64","NODE_VERSION":"15.5.1","HOSTNAME":"logs-pod","YARN_VERSION":"1.22.5","npm_node_execpath":"/usr/local/bin/node","SHLVL":"2","HOME":"/root","npm_package_json":"/app/package.json","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP":"tcp://10.108.29.96:80","npm_config_userconfig":"/root/.npmrc","COLOR":"0","npm_config_metrics_registry":"https://registry.npmjs.org/","npm_config_prefix":"/usr/local","npm_config_cache":"/root/.npm","KUBERNETES_PORT_443_TCP_ADDR":"10.96.0.1","npm_config_node_gyp":"/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js","PATH":"/app/node_modules/.bin:/node_modules/.bin:/usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/node-gyp-bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NODE":"/usr/local/bin/node","npm_package_name":"k8s-service","KUBERNETES_PORT_443_TCP_PORT":"443","npm_config_init.module":"/root/.npm-init.js","KUBERNETES_PORT_443_TCP_PROTO":"tcp","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_SERVICE_HOST":"10.108.29.96","npm_lifecycle_script":"node index.js","PREFIX":"/usr/local","npm_package_version":"1.0.0","npm_lifecycle_event":"start","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_SERVICE_PORT":"80","KUBERNETES_SERVICE_PORT_HTTPS":"443","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT":"tcp://10.108.29.96:80","KUBERNETES_PORT_443_TCP":"tcp://10.96.0.1:443","npm_config_init_module":"/root/.npm-init.js","npm_config_globalconfig":"/usr/local/etc/npmrc","KUBERNETES_SERVICE_HOST":"10.96.0.1","PWD":"/app","npm_execpath":"/usr/local/lib/node_modules/npm/bin/npm-cli.js","npm_command":"run-script","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP_ADDR":"10.108.29.96","INIT_CWD":"/app","EDITOR":"vi"}
In Secrets, MYSQL HOST: undefined
In Secrets, MYSQL PASSWORD: undefined
In ConfigMaps, jay phone number: undefined
In ConfigMaps, tim phone number: undefined
Kubectl Logs Test Random number is: 0.9646949892134025
Kubectl Logs Test Random number is: 0.46915083064316687

These logs are specific to the container running inside the pod. Kubernetes pods can have multiple docker containers in them.

Multiple containers in the same pod with logs

apiVersion: v1
kind: Pod
metadata:
  name: logs-pod
  labels:
    app: logs-pod
spec:
  containers:
    - name: logs-pod-container
      image: jayjodev/k8s-node-app:latest
      ports:
        - containerPort: 4000
          protocol: TCP
    - name: nginx-pod-container
      image: nginx
      ports:
        - containerPort: 80
          protocol: TCP
$ kubectl create -f event-pod.yaml 
pod/event-pod created
$ kubectl logs -f logs-pod logs-pod-container

> k8s-service@1.0.0 start
> node index.js

Lunch app is listening on port !4000
process.env is: {"KUBERNETES_SERVICE_PORT":"443","KUBERNETES_PORT":"tcp://10.96.0.1:443","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP_PORT":"80","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP_PROTO":"tcp","npm_config_user_agent":"npm/7.3.0 node/v15.5.1 linux x64","NODE_VERSION":"15.5.1","HOSTNAME":"logs-pod","YARN_VERSION":"1.22.5","npm_node_execpath":"/usr/local/bin/node","SHLVL":"2","HOME":"/root","npm_package_json":"/app/package.json","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP":"tcp://10.108.29.96:80","npm_config_userconfig":"/root/.npmrc","COLOR":"0","npm_config_metrics_registry":"https://registry.npmjs.org/","npm_config_prefix":"/usr/local","npm_config_cache":"/root/.npm","KUBERNETES_PORT_443_TCP_ADDR":"10.96.0.1","npm_config_node_gyp":"/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js","PATH":"/app/node_modules/.bin:/node_modules/.bin:/usr/local/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/node-gyp-bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NODE":"/usr/local/bin/node","npm_package_name":"k8s-service","KUBERNETES_PORT_443_TCP_PORT":"443","npm_config_init.module":"/root/.npm-init.js","KUBERNETES_PORT_443_TCP_PROTO":"tcp","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_SERVICE_HOST":"10.108.29.96","npm_lifecycle_script":"node index.js","PREFIX":"/usr/local","npm_package_version":"1.0.0","npm_lifecycle_event":"start","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT":"tcp://10.108.29.96:80","KUBERNETES_SERVICE_PORT_HTTPS":"443","KUBERNETES_PORT_443_TCP":"tcp://10.96.0.1:443","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_SERVICE_PORT":"80","npm_config_init_module":"/root/.npm-init.js","npm_config_globalconfig":"/usr/local/etc/npmrc","KUBERNETES_SERVICE_HOST":"10.96.0.1","PWD":"/app","npm_execpath":"/usr/local/lib/node_modules/npm/bin/npm-cli.js","npm_command":"run-script","K8S_CONFIGMAPS_NODE_NODEPORT_SERVICE_PORT_80_TCP_ADDR":"10.108.29.96","INIT_CWD":"/app","EDITOR":"vi"}
In Secrets, MYSQL HOST: undefined
In Secrets, MYSQL PASSWORD: undefined
In ConfigMaps, jay phone number: undefined
In ConfigMaps, tim phone number: undefined
Kubectl Logs Test Random number is: 0.9377947177681158
Kubectl Logs Test Random number is: 0.7889909345690462
Kubectl Logs Test Random number is: 0.10207869288904692
Kubectl Logs Test Random number is: 0.07571241293602404
Kubectl Logs Test Random number is: 0.8404475294076039
Kubectl Logs Test Random number is: 0.4636819003895545
Kubectl Logs Test Random number is: 0.03149863030615552
Kubectl Logs Test Random number is: 0.8382361718486933
Kubectl Logs Test Random number is: 0.21040081016385148

Container name: logs-pod-container

$ kubectl logs -f logs-pod nginx-pod-container
/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: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

Leave a Reply

Your email address will not be published.

ANOTE.DEV