How to SSH to docker container in kubernetes cluster?

北城以北 提交于 2019-12-20 08:25:03

问题


I am fairly new to the Google Cloud platform and Docker and set-up a cluster of nodes, made a Dockerfile that copies a repo and runs a Clojure REPL on a public port. I can connect to it from my IDE and play around with my code, awesome!

That REPL should however probably tunneled through SSH, but here is where my problem starts. I can't find a suitable place to SSH into for making changes to the repo that Docker runs the REPL on:

  • The exposed IP just exposes the REPL service (correct kubernetes term?) and does not allow me to SSH in.
  • Neither does the cluster master endpoint, it gives me a public key error even though I've followed the Adding or removing SSH keys for all of the instances in your project part here.

I would like to edit the source files via SSH but I would need to access the docker code repo. I don't know how to proceed.

I understand this isn't exactly a typical way to deploy applications so I am not even sure it's possible to have multiple nodes work with a modified docker codebase (do the nodes share the JVM somehow?).

Concretely my question is how do I SSH into the docker container to access the codebase?


回答1:


I can't find a suitable place to SSH into for making changes to the repo that Docker runs the REPL on

When you create a cluster, you provision a number of node VMs in your google cloud project. If you look at https://console.cloud.google.com/compute/instances you should see them and each one will have a External IP address which you will be able to ssh into. Then create an ssh tunnel to a node VM that forwards a local port to the pod IP address.

Note that if you are running multiple replicas of your clojure app, you must connect to each replica separately to update the app.




回答2:


The command format for Kubernetes 1.5.0:

kubectl exec -it <POD NAME> -c <CONTAINER NAME> bash



回答3:


List instances:

gcloud compute instances list

SSH into instance:

gcloud compute ssh <instance_name> --zone=<instance_zone>

In the instance, list the running processes and their container IDs:

sudo docker ps -a

Attach to a container:

sudo docker exec -it <container_id> bash  



回答4:


The best way to attach to the container through exec command.

Attach to docker running container

docker exec -it  YOUR_CONTAINER_ID bash

Attach to Kubernetes running container.

kubectl exec -it  YOUR_CONTAINER/POD_NAME bash

Attach to Kubernetes running container in a given namespace.

kubectl exec -it  YOUR_CONTAINER/POD_NAME -n YOUR_NAMESPACE bash



回答5:


If the pod is in your current namespace, get the list of pods:

kubectl get pods

Pick a pod. Execute a bash session on it:

kubectl exec -it [POD_NAME] -- /bin/bash

Alternatively, find the pod you want in a different namespace:

kubectl get pods --all-namespaces

Pick a pod and execute a bash session on it:

kubectl exec -it [POD_NAME] --namespace [NAMESPACE] -- /bin/bash


来源:https://stackoverflow.com/questions/38485771/how-to-ssh-to-docker-container-in-kubernetes-cluster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!