How to SSH to docker container in kubernetes cluster?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When working with Kubernetes clusters, you may find yourself needing to access and troubleshoot individual pods. One common approach is to SSH into a container running inside a pod. However, it's worth noting that standard SSH into containers is not considered a best practice in Kubernetes due to its ephemeral and dynamic nature. Instead, we typically use tools like kubectl to gain shell access for troubleshooting purposes. This article will discuss how to achieve access to a Docker container within a Kubernetes cluster and explore related best practices.
Understanding the Environment
Before we dive into accessing a Docker container, it's essential to understand the architecture and how Kubernetes orchestrates containers. When you deploy an application to a Kubernetes cluster, it is encapsulated within a pod. Each pod can contain one or more containers. Kubernetes uses its kubectl command-line tool to interact with these resources, abstracting away the need for direct SSH access.
Prerequisites
- Kubernetes Cluster: Ensure you have access to a functioning Kubernetes cluster.
- kubectl: Install and configure
kubectlto access your Kubernetes cluster. - Proper Permissions: Ensure you have the necessary permissions to interact with pods.
Accessing Docker Containers through kubectl
Here's a step-by-step guide to accessing a Docker container within a Kubernetes cluster.
Step 1: Identifying the Pod
First, identify the pod running the container you wish to access. You can do this by listing all pods using:
This command will return a list of running pods. Note the name of the pod you want to access.
Step 2: Using exec Command
To access a specific container within a pod, you can use the kubectl exec command. This command allows you to run commands directly in a container.
-it: Stands for interactive terminal.<pod_name>: Replace with the name of your pod./bin/sh: Specifies the shell; you can also use/bin/bashif available.
Example
Suppose you have a pod named webapp-6c59d6d4-c4tj7. Here’s how you would access its main container:
Once executed, you will be inside the container's shell and can run commands as needed.
Troubleshooting and Best Practices
Common Issues
- Pod Not Found: Ensure your spelling is correct and that you are in the correct namespace.
- Permission Denied: Check your Kubernetes RBAC settings to ensure you have the necessary permissions.
Best Practices
- Logging and Monitoring: Use Kubernetes-native solutions or logging tools for monitoring and diagnostics.
- Avoid Persistent Changes: Remember that any changes made within a container are temporary.
- Short Sessions: Use
execfor short-lived tasks or troubleshooting, as it does not replace robust solutions for deeper inspections.
Summary
Here’s a summary table of key points discussed:
| Topic | Description |
| Identifying a Pod | Use kubectl get pods to list and identify the target pod. |
| Accessing Container | Use kubectl exec -it <pod_name> -- /bin/sh for interactive shell access. |
| Common Issues | Includes pod name typos and permission errors. |
| Best Practices | Encourage logging tools, avoid persistent container changes, short sessions. |
Conclusion
While Kubernetes does not inherently support direct SSH access into individual containers, tools like kubectl provide a robust method for accessing and troubleshooting containers within a Kubernetes cluster. By following best practices, you ensure a more efficient and secure interaction with your containerized applications.
For ongoing production operations, consider logging, monitoring, and tracing solutions that provide insight without the need for container access. Always proceed with caution, remembering that containers are designed to be ephemeral and immutable at runtime.
Related reading
- How to start a pod in command line without deployment in kubernetes?
- How to stop all containers when one container stops with docker-compose?
- How to stop all external traffic and allow only inter pod network call within namespace using network policy?
- How to stop Docker and Kubernetes using Docker desktop?
- How to start a stopped Docker container with a different command?
- How to start apache2 automatically in a ubuntu docker container?
- How to stop Replicaset from restarting?
- how to stop/pause a pod in kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.