DEV Community

Cover image for Running Kubernetes in Docker (KIND) Locally While Your Cluster Lives in Google Cloud Shell
M. Oly Mahmud
M. Oly Mahmud

Posted on

Running Kubernetes in Docker (KIND) Locally While Your Cluster Lives in Google Cloud Shell

You want to develop with Kubernetes locally using KIND, but your laptop doesn't have enough resources. Google Cloud Shell has plenty of compute, but it's not meant for long-running clusters. So run KIND in Cloud Shell and control it from your machine.

The benefits of it:

  • Cloud Shell has more compute than your dev machine
  • Your laptop stays clean
  • No Docker Desktop overhead

Create Your KIND Cluster in Cloud Shell

First, the cluster config. The important part is extraPortMappings—this exposes ports 80 and 443 to Cloud Shell.

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: streamx-cluster
nodes:
  - role: control-plane
    image: kindest/node:v1.35.1
    extraPortMappings:
      - containerPort: 80
        hostPort: 80
        protocol: TCP
      - containerPort: 443
        hostPort: 443
        protocol: TCP
  - role: worker
    image: kindest/node:v1.35.1
  - role: worker
    image: kindest/node:v1.35.1
Enter fullscreen mode Exit fullscreen mode

Create it and verify:

kind create cluster --config kind-config.yaml
kubectl cluster-info --context kind-streamx-cluster
Enter fullscreen mode Exit fullscreen mode

Export the Kubeconfig

KIND generates a kubeconfig automatically. Export it to a file you can move:

kind export kubeconfig --name streamx-cluster --kubeconfig my-cluster-config.yaml
cat my-cluster-config.yaml
Enter fullscreen mode Exit fullscreen mode

Copy the Kubeconfig to Your Local Machine

gcloud cloud-shell scp cloudshell:~/my-cluster-config.yaml localhost:~/my-cluster-config.yaml
Enter fullscreen mode Exit fullscreen mode

Set Up Port Forwarding

Your cluster's API server is in Cloud Shell on 127.0.0.1:46151 (check your kubeconfig). Tunnel it through SSH:

gcloud cloud-shell ssh --ssh-flag="-f" --ssh-flag="-N" --ssh-flag="-T" --ssh-flag="-L 46151:127.0.0.1:46151"
Enter fullscreen mode Exit fullscreen mode

The flags:

  • -f runs in the background
  • -N just forward ports, don't run a remote command
  • -T no pseudo-terminal
  • -L 46151:127.0.0.1:46151 forwards your local port to the remote one

The tunnel stays open as long as your Cloud Shell session is active.

Connect kubectl Locally

export KUBECONFIG="~/my-cluster-config.yaml"
kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

You should see your control plane and worker nodes. If you get a certificate error, check that your SSH tunnel is running.

Deploy Nginx

Create nginx-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-deployment
  template:
    metadata:
      labels:
        app: nginx-deployment
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

And nginx-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport-service
spec:
  type: NodePort
  selector:
    app: nginx-deployment
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 32000
Enter fullscreen mode Exit fullscreen mode

Deploy it:

kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml
kubectl port-forward svc/nginx-nodeport-service 8080:80
Enter fullscreen mode Exit fullscreen mode

Open http://localhost:8080. You're looking at Nginx running in a pod in Cloud Shell.


Thanks for reading

Top comments (0)