Do I have to do service to a get shell from localhost?

10/16/2018

I want to use ssh from localhost to Pod. Is there any way to connect using ssh [Pod_IP] without using kubectl exec -it [Pod] /bin/bash?

The results are as shown in ERROR: ssh: connect to host [ip] port 22: Connection refused.

-- K.k
devops
kubernetes
kubernetes-pod
ssh

1 Answer

10/16/2018

I think you can achieve this goal by installing openssh-server on the targeted Pod

For example:

Establish SSH connection to the Pod:

$ kubectl exec -it <Pod_name> -- /bin/bash
$ apt-get update
$ apt-get install -y openssh-server

Ensure that SSHD service is up and running:

$ service ssh status

Start it if necessary:

$ service ssh start

Edit the /etc/ssh/sshd_config file if you want to change some specific settings, and restart the SSH service.

Check the connection via SSH from your local machine using Pod IP address.

Update:

I use the following Pod configuration in order to establish SSH connection to the Centos 7 container:

apiVersion: v1
kind: Pod
metadata:
  name: centos
spec:
  containers:
  - name: centos
    image: centos:latest
    command: [ "/bin/bash", "-c", "yum install openssh-server -y && /usr/bin/ssh-keygen -A && /usr/sbin/sshd -p 
22 -f /etc/ssh/sshd_config && tail -f /dev/null" ]
    securityContext:
      privileged: true
-- mk_sta
Source: StackOverflow