ssh to k8s pod with code examples

SSH to Kubernetes Pod with Code Examples

Kubernetes is a powerful container orchestration system that has been rapidly adopted by organizations to deploy, manage, and scale applications. One way to manage a Kubernetes Pod is through SSH, which provides remote access to the container shell. This article will walk through the process of connecting to a Pod using SSH, including setting up the basic environment and configuration, and then demonstrate how to use SSH to manage the Pod.

Setting up the Environment

To connect to a Kubernetes Pod using SSH, we will need to have a few things in place. First, we need a cluster with at least one node running, as well as the Kubernetes command-line tool (kubectl) installed on our local machine. If you don't have these, you can quickly set up a cluster using a tool like Minikube, which creates a local Kubernetes cluster that you can use for testing and development.

Once we have a cluster set up with kubectl installed, we need to create a Pod to which we can connect through SSH. We can create a simple Pod running an Nginx container with the following YAML manifest:

apiVersion: v1
kind: Pod
  name: nginx-pod
  - name: nginx
    image: nginx

Save the YAML manifest to a file, say nginx-pod.yaml, and then create the Pod using the kubectl create command:

$ kubectl create -f nginx-pod.yaml

If the Pod was created successfully, we can run kubectl get pods to verify that it is running:

$ kubectl get pods
nginx-pod   1/1     Running   0          1m

Configuring SSH access

Now that our Pod is up and running, we need to configure it to allow SSH access. By default, SSH is not installed in the Nginx container, so we need to add it and configure the necessary settings.

To do this, we will create a custom Docker image based on the Nginx image that includes the SSH server. We can start by creating a Dockerfile in a new directory and adding the following lines:

FROM nginx

RUN apt-get update && \
    apt-get install -y openssh-server && \
    mkdir /var/run/sshd && \
    echo 'root:password' | chpasswd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config


CMD ["/usr/sbin/sshd", "-D"]

This Dockerfile installs the SSH server and sets the root user's password. It also configures the SSH daemon to allow root login and exposes port 22, the default port used by SSH.

Build the Docker image using the docker build command:

$ docker build -t nginx-ssh .

Once the image is built, we can use it to create a new Pod that runs our SSH-enabled Nginx container. Modify the nginx-pod.yaml file to use the new image, like so:

apiVersion: v1
kind: Pod
  name: nginx-ssh-pod
  - name: nginx
    image: nginx-ssh
    - containerPort: 22

We also need to specify the container port that SSH will be listening on, in this case, port 22. Save the changes to nginx-pod.yaml and run kubectl create to create the Pod:

$ kubectl create -f nginx-pod.yaml

Now that our Pod is running with SSH enabled, we can connect to it using any SSH client.

Using SSH to Manage the Pod

To connect to our Pod using SSH, we need to obtain the Pod's IP address. We can do this by running kubectl describe pod and looking for the IP address in the output:

$ kubectl describe pod nginx-ssh-pod | grep IP

We can use this IP address to connect to the Pod using the root user.

Let's first check that the Nginx server is running inside the container. Start by opening an SSH session to the Pod as follows:

$ ssh root@

You will be prompted to enter the password you set earlier (in our example, "password"). Once you've logged in, you will be connected to the container shell.

Now we can check that the Nginx server is running using the ps command:

# ps aux | grep nginx
root         1  0.0  0.0   1720   688 ?        Ss   11:47   0:00 nginx: master process nginx -g daemon off;
nginx        7  0.0  0.0   2052  1216 ?        S    11:47   0:00 nginx: worker process
root        20  0.0  0.0   5892   856 ?        S+   11:59   0:00 grep nginx

We can also view the Nginx access logs by running, for instance:

# tail -f /var/log/nginx/access.log

We can manage the Pod and the containers inside it using any command available in the container's shell.


Connecting to a Kubernetes Pod using SSH is a powerful way to manage the container and the application running inside it. This article has shown how to create a Pod running an SSH-enabled Nginx container, and then connect to it using the SSH client.

While SSH access can be useful for debugging and troubleshooting, it also introduces security risks. It's important to carefully manage SSH access to Kubernetes Pods and ensure that access is properly secured.

That said, if you have a legitimate use case and have properly secured access, connecting to a Kubernetes Pod using SSH can be a valuable tool in your Kubernetes toolbox.

I can expand on some of the previous topics in more detail. Let's start with Kubernetes.

Kubernetes is a container orchestration system that helps manage containerized applications. It provides a platform for automating deployment, scaling, and operations of application containers across clusters of hosts. Some of the key features of Kubernetes include automatic load balancing, scaling, self-healing of applications, and rolling updates.

Kubernetes uses a declarative model for application deployment and management. This means that you define the desired state of your application, and Kubernetes will work to ensure that the application is running in that state. Kubernetes does this by continuously monitoring the state of the system and making any necessary changes to ensure that it matches the desired state.

One of the key benefits of Kubernetes is its ability to automatically scale applications based on demand. Kubernetes can automatically scale up or down the number of replicas of your application based on factors like CPU usage or network traffic. This helps ensure that your application is always available and responsive to users.

When it comes to security, Kubernetes provides a number of features to help ensure the safety and privacy of your applications. These include network isolation, secure communications between services, and the ability to define and enforce access controls. Kubernetes also allows you to configure secure resource access, limiting the ability of attackers to exploit vulnerabilities.

Next, let's talk about SSH. Secure Shell (SSH) is a protocol used to securely connect to a remote computer. SSH is commonly used by system administrators for remote access and management of servers. It provides a secure and encrypted way to connect to a remote machine and execute commands.

SSH is also commonly used for key-based authentication, which is a more secure method than traditional password-based authentication. When you use key-based authentication, you generate a public-private key pair, and the public key is added to the remote machine's authorized keys file. When you connect using SSH, your private key is used instead of a password, providing stronger security.

In addition to remote access and management, SSH can also be used for tunneling. SSH forwarding (also known as tunneling) allows you to create a secure connection between two machines, even if they are not directly connected to each other. This can be useful for securely accessing resources that are behind firewalls or other security barriers.

Finally, let's look at Docker. Docker is a containerization platform that helps developers package and distribute applications as self-contained and portable containers. Docker uses container technology to provide a lightweight and efficient way to deploy and run applications.

Docker containers provide a standardized environment for applications, making it easier to develop and deploy applications across different environments. Docker containers are isolated from each other, ensuring that applications run in a consistent and reliable manner.

One of the key benefits of Docker is its ability to simplify the application deployment process. Developers can create a Docker container with all of the dependencies, libraries, and code needed to run the application. This container can then be deployed on any system with Docker installed, making it easy to move applications between development, testing, and production environments.

Docker also provides features to help with application scaling, orchestration, and management. Docker Swarm is a built-in orchestration tool that allows you to manage a cluster of Docker hosts, providing features like auto-scaling, load balancing, and service discovery.

In terms of security, Docker provides tools to help ensure that your containers are secure. This includes the ability to define security policies, limit network access, and run containers with minimal system privileges. Docker also provides support for security scanning of container images, which can help identify and mitigate any vulnerabilities in your application.

Popular questions

  1. What is Kubernetes?
    Ans: Kubernetes is an open-source container-orchestration system that automates the deployment, scaling, and management of containerized applications.

  2. What is SSH, and how does it work with Kubernetes?
    Ans: Secure Shell (SSH) is a protocol used to connect to a remote computer securely. By enabling SSH access to Kubernetes pods, one can remotely manage the Kubernetes pods using a remote terminal.

  3. How can you configure SSH access to a Kubernetes Pod?
    Ans: You can configure SSH access to a Kubernetes Pod by creating a custom Docker image that includes an SSH server. You can then run this image as a container inside a Pod. From there, you can connect to the container using SSH and manage it as you would any other remote system.

  4. What are some security considerations when configuring SSH access to Kubernetes Pods?
    Ans: SSH access introduces security risks, so it's important to properly secure access to Kubernetes Pods. This includes limiting access to trusted users, properly securing passphrases, and limiting privileges for SSH users.

  5. What are some benefits of using SSH to manage Kubernetes Pods?
    Ans: Using SSH to manage Kubernetes Pods provides remote access to the container shell and allows for easy management of the containerized application. This can be particularly useful for debugging and troubleshooting. Additionally, by using SSH forwarding, you can create a secure connection between two machines, making it easier to access resources behind firewalls or other security barriers.



Have an amazing zeal to explore, try and learn everything that comes in way. Plan to do something big one day! TECHNICAL skills Languages - Core Java, spring, spring boot, jsf, javascript, jquery Platforms - Windows XP/7/8 , Netbeams , Xilinx's simulator Other - Basic’s of PCB wizard
Posts created 3116

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top