SSH into GKE Kubernetes cluster?

Solution 1:

First of all it's possible to deploy a pod with a single container consisting of ubuntu targeting a namespace or even a node.

Rather than using SSH to connect to it (which is possible using an extensive combination of either using a LoadBalancer or exposing a NodePort) it's easier to use the kubectl tool.

If you're using Cloud Shell it's already installed or if using local laptop you have to install it using the gcloud tool.

I would suggest connecting to the container directly using the following syntax:

# Run bash on Ubuntu container
kubectl exec -it ubuntu -- bash

# General syntax
kubectl -n {namespace} exec -it {pod-name} -- {command}

First command assumes the container name is ubuntu and in the current namespace. The second command gives the general format.

Example ubuntu pod definition:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
  labels:
    app: ubuntu
spec:
  containers:
  - image: ubuntu
    command:
      - "sleep"
      - "604800"
    imagePullPolicy: IfNotPresent
    name: ubuntu
  restartPolicy: Always

You can add a namespace to it or ensure you have the right context before applying. Something like:

kubectl apply -f path/to/yaml/file