Kubernetes
kubectl
containers
pods
command-line

How to list containers and its info running a pod using kubectl command

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In the Kubernetes ecosystem, pods operate as the foundational units of deployment. A pod encapsulates one or more containers, each running a microservice or application. Efficiently managing pods involves not only deploying and scaling but also monitoring what's running inside them, especially their containers. In this article, we delve into how to list and gather detailed information about containers within a pod using the kubectl command. This is crucial for debugging, monitoring, and verifying the deployment states in a Kubernetes cluster.

Understanding Pods and Containers

Before diving into commands, let's recapitulate the structure:

  • Pod: The smallest deployable unit in Kubernetes. It can have one or more containers.
  • Container: A lightweight, executable software unit that includes application code and the dependencies needed to run it.

Why List Containers in a Pod?

To maintain robust applications, you need access to:

  • Status and health checks: Ensure that containers are running as expected.
  • Resource usage: Monitor CPU and memory consumption.
  • Logs: Fetch logs for debugging applications.

Exploring kubectl Commands

kubectl is the command-line interface for running commands against Kubernetes clusters. With intuitive syntax, it helps manage cluster resources effectively.

Command to List Containers in a Pod

To list containers in a running pod, start by fetching basic pod details:

  • Container names and image versions
  • Environment variables
  • Resource limits and requests
  • Mounts and volumes
  • State and readiness probes
    • name: nginx-container
  • Container Name: nginx-container
  • Image: nginx:1.19.2
  • Ports, Conditions, and Events
  • Fetch Logs: Use kubectl logs ``<pod-name>`` -c ``<container-name>`` to get the logs of a specific container.
  • Resource Usage: Integrate with monitoring tools or use kubectl top pod ``<pod-name>```` for resource consumption metrics.

Course illustration
Course illustration

All Rights Reserved.