> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating Kubernetes Clusters with Claude

> Using Claude Code on a Linux VM to generate and run an idempotent script installing Docker kubectl Minikube and creating a multi node cluster running nginx

In this lesson we'll create a reproducible Kubernetes cluster on a Linux VM using Claude Code. The example demonstrates how Claude Code can generate and execute an idempotent Bash script to:

* install prerequisites,
* install Docker, kubectl and Minikube,
* start a multi-node Minikube cluster,
* enable useful addons,
* deploy a demo nginx app exposed as a NodePort service,
* and provide convenient access details and fallbacks.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/Xs8Ruv7b0QCZf7iM/images/Claude-Code-for-Beginners/Advanced-Features/Creating-Kubernetes-Clusters-with-Claude/kubernetes-clusters-claude-demo-kodekloud.jpg?fit=max&auto=format&n=Xs8Ruv7b0QCZf7iM&q=85&s=f71b15e813791eab7709463ace8d0247" alt="A presentation slide reading &#x22;Creating Kubernetes Clusters with Claude&#x22; on the left and a large &#x22;Demo&#x22; label inside a dark blue curved shape on the right. A small &#x22;© Copyright KodeKloud&#x22; appears in the bottom-left." width="1920" height="1080" data-path="images/Claude-Code-for-Beginners/Advanced-Features/Creating-Kubernetes-Clusters-with-Claude/kubernetes-clusters-claude-demo-kodekloud.jpg" />
</Frame>

Overview

* This guide shows how to install Claude Code on an Ubuntu VM, sign in, and ask it to create a Minikube-based Kubernetes cluster.
* Claude Code can execute Bash commands on the machine where it's installed. The example uses a VM (for example, [Amazon EC2](https://learn.kodekloud.com/user/courses/amazon-elastic-compute-cloud-ec2)), but any Linux VM (local or cloud) works.
* The final result is a 3-node Minikube cluster named `demo`, with an nginx "hello" Deployment exposed through a NodePort.

<Callout icon="lightbulb" color="#1CB2FE">
  For a 3-node Minikube cluster sized with 2 CPUs and 4GB memory per node you will need a VM with enough total RAM and CPU (roughly 12GB RAM + host overhead and at least 6 CPUs). If you only have a single smaller VM, reduce node counts and/or memory values.
</Callout>

Quick configuration summary

| Item | Value / Command |
| - | - |
| Profile name | `demo` |
| Nodes | `3` |
| CPUs per node | `2` |
| Memory per node | `4096` (MB) |
| Addons enabled | `metrics-server`, `ingress`, `dashboard` |
| Demo deployment | `hello` (nginx) |
| Demo access | `minikube -p demo service hello --url` (NodePort) or `kubectl port-forward deploy/hello 8080:80` |

Installing and signing in to Claude Code

1. Install Claude Code on the VM (example):

```bash theme={null}
curl -sS https://claude.ai/install.sh | bash
```

2. Start Claude Code:

```bash theme={null}
claude
```

3. Follow the interactive sign-in flow. If the installer prints an OAuth URL, open it in a browser and paste the code/token back into the Claude prompt. Example installer prompt (truncated):

```text theme={null}
Browser didn't open? Use the url below to sign in:

https://claude.ai/oauth/authorize?code=true&client_id=<...>&response_type=code&redirect_uri=...
Paste code here if prompted >
```

After completing browser authorization you should see a small interactive prompt similar to:

```text theme={null}
Welcome to Claude Code!
/help for help, /status for your current setup
cwd: /home/ubuntu

Tips for getting started:
Run /init to create a CLAUDE.md file with instructions for Claude
Use Claude to help with file analysis, editing, bash commands and git
Be as specific as you would with another engineer for the best results

Note: You have launched claude in your home directory. For the best experience, launch it in a project directory instead.

> Try "refactor <filepath>"
```

Preparing the task: the prompt we gave Claude Code

We asked Claude Code to run a non-interactive plan on the Ubuntu VM to:

* Install prerequisites (`ca-certificates`, `curl`, `gnupg`, `apt-transport-https`, `conntrack`, `socat`, `ebtables`, `ethtool`) and apply sysctls for bridge and IP forwarding.
* Install Docker (`docker.io`), enable/start it, and add the current user to the `docker` group.
* Install `kubectl` from the official APT repo.
* Install Minikube (latest Linux binary).
* Start a 3-node Minikube cluster named `demo` using the Docker driver with `--cpus=2` and `--memory=4096` per node.
* Enable addons: `metrics-server`, `ingress`, `dashboard`.
* Deploy a `hello` nginx Deployment and expose it as a NodePort service.
* Print nodes, services, and a reachable URL via `minikube -p demo service hello --url`. Provide a port-forward fallback command.
* Make actions idempotent and handle docker-group membership changes by attempting a `newgrp docker` block if required.

Human-readable version of the instruction (as supplied to Claude Code):

```text theme={null}
You are an expert DevOps assistant. On THIS Ubuntu VM, please execute the following plan non-interactively using only the Bash tool:

PLAN
1) Install prerequisites: ca-certificates, curl, gnupg, apt-transport-https, conntrack, socat, ebtables, ethtool. Apply sysctls: net.bridge.bridge-nf-call-iptables=1 and net.ipv4.ip_forward=1.
2) Install Docker (docker.io), enable/start it, and add the current user to the docker group.
3) Install kubectl via the official APT repo.
4) Install Minikube (latest Linux binary).
5) Start a 3-node Minikube cluster named "demo" using the Docker driver with --cpus=2 and --memory=4096 per node.
6) Enable addons: metrics-server, ingress, dashboard.
7) Create a Deployment "hello" using nginx and expose as NodePort.
8) Print nodes, services, and a reachable URL using minikube service --url. Also print a port-forward fallback command.
9) Make all steps idempotent and handle docker-group membership (attempt a newgrp docker block before retrying minikube).

Now write the script to /tmp/minikube-demo.sh and run it.
```

The final automated script

Claude Code generated and executed a script at `/tmp/minikube-demo.sh`. Below is a cleaned, idempotent script (the same, improved and consistent version used in the lesson/article). Save this script as `/tmp/minikube-demo.sh` if you want to run it yourself.

```bash theme={null}
#!/usr/bin/env bash
set -euo pipefail

# Configuration
PROFILE_NAME=demo
NODES=3
CPU=2
MEM=4096

# Update
sudo apt-get update -y

# Install prerequisites
sudo apt-get install -y ca-certificates curl gnupg apt-transport-https \
  conntrack socat ebtables ethtool

# Kernel modules and sysctls
sudo modprobe br_netfilter || true
# Create (or overwrite) sysctl file for Kubernetes requirements
echo "net.bridge.bridge-nf-call-iptables=1" | sudo tee /etc/sysctl.d/99-k8s.conf >/dev/null
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.d/99-k8s.conf >/dev/null
sudo sysctl --system

# Install Docker (idempotent)
if ! command -v docker >/dev/null 2>&1; then
  sudo apt-get install -y docker.io
  sudo systemctl enable --now docker
  sudo usermod -aG docker "${SUDO_USER:-$USER}" || true
fi

# Install kubectl (official APT repo)
if ! command -v kubectl >/dev/null 2>&1; then
  sudo install -m 0755 -d /etc/apt/keyrings
  curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.34/deb/Release.key \
    | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
  echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" \
    | sudo tee /etc/apt/sources.list.d/kubernetes.list
  sudo apt-get update -y
  sudo apt-get install -y kubectl
fi

# Install Minikube (latest binary)
if ! command -v minikube >/dev/null 2>&1; then
  curl -Lo minikube-linux-amd64 https://github.com/kubernetes/minikube/releases/latest/download/minikube-linux-amd64
  sudo install minikube-linux-amd64 /usr/local/bin/minikube
  rm -f minikube-linux-amd64
fi

# Start Minikube cluster (idempotent)
if ! minikube -p "$PROFILE_NAME" status >/dev/null 2>&1; then
  if ! docker ps >/dev/null 2>&1; then
    echo "Refreshing docker group and starting Minikube in a subshell..."
    newgrp docker <<NG
minikube start -p "$PROFILE_NAME" --driver=docker --nodes="$NODES" --cpus="$CPU" --memory="$MEM"
NG
  else
    minikube start -p "$PROFILE_NAME" --driver=docker --nodes="$NODES" --cpus="$CPU" --memory="$MEM"
  fi
else
  echo "Minikube profile \"$PROFILE_NAME\" already exists; skipping start."
fi

# Enable addons (ignore failures)
minikube -p "$PROFILE_NAME" addons enable metrics-server || true
minikube -p "$PROFILE_NAME" addons enable ingress || true
minikube -p "$PROFILE_NAME" addons enable dashboard || true

# Deploy demo nginx app if missing
if ! kubectl get deploy hello >/dev/null 2>&1; then
  kubectl create deployment hello --image=nginx --port=80
  kubectl expose deployment hello --type=NodePort --port=80
fi

# Output status
echo "=== Nodes ==="
kubectl get nodes -o wide || true

echo "=== Services ==="
kubectl get svc -o wide || true

echo "=== Demo URL(s) ==="
minikube -p "$PROFILE_NAME" service hello --url || true

echo -e "\nFallback (port-forward from this VM):\n  kubectl port-forward deploy/hello 8080:80\nThen curl http://localhost:8080\n"
```

How Claude executed and validated the setup

* Claude created the script at `/tmp/minikube-demo.sh`, made it executable, and executed it non-interactively.
* When docker-group membership changes blocked some actions, Claude used `newgrp docker` to run the relevant Minikube commands in the correct group context.
* Claude validated the demo application by performing a `kubectl port-forward` and running a `curl` against the forwarded port to confirm the nginx default page responded.

Example verification output (representative, truncated)

```text theme={null}
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080

• Minikube Demo Setup Complete!

✅ Infrastructure Setup:
- Installed prerequisites
- Applied sysctls for bridge-nf-call-iptables and ip_forward
- Installed Docker, enabled/started it, and added user to docker group
- Installed kubectl and Minikube

✅ Cluster Configuration:
- Started 3-node Minikube cluster named "demo" using Docker driver
- Each node configured with 2 CPUs and 4096MB memory (per node)
- Addons enabled: metrics-server, ingress, dashboard

✅ Demo Application:
- Created "hello" deployment using nginx image
- Exposed as NodePort service (e.g., http://192.168.49.2:31902)
- Successfully tested via port-forwarding

Access:
- Direct URL (minikube): minikube -p demo service hello --url
- Port-forward fallback: kubectl port-forward deploy/hello 8080:80  -> curl http://localhost:8080
```

Notes and troubleshooting tips

* Docker group: after adding your user to the `docker` group you may need to log out/in, restart the shell, or run `newgrp docker` for group membership to take effect.
* Resource constraints: Minikube multi-node clusters using the Docker driver create multiple containers on the host; ensure sufficient disk, CPU, and memory. If resources are limited, reduce `NODES`, `CPU`, or `MEM`.
* Addon image pulls: if an addon fails to enable due to image pull/network issues, verify network connectivity and retry:
  * `minikube -p demo addons enable <addon>`
* If kubectl commands fail due to context, ensure Minikube's kubeconfig is active for the profile:
  * `minikube -p demo kubectl -- get nodes`

Conclusion
Using Claude Code on a VM you control streamlines creating reproducible local Kubernetes environments. By providing a concise plan and desired parameters (profile name, node count, CPU/memory), Claude Code can produce an idempotent script, run it, and validate the running cluster—saving time and reducing manual errors.

If you want to reuse this example, save the script above to `/tmp/minikube-demo.sh` and adjust `PROFILE_NAME`, node count, CPU, and memory values to match your available resources.

Links and References

* Minikube: [https://minikube.sigs.k8s.io/](https://minikube.sigs.k8s.io/)
* Kubernetes documentation: [https://kubernetes.io/docs/](https://kubernetes.io/docs/)
* Docker: [https://www.docker.com/](https://www.docker.com/)
* kubectl installation (APT / official instructions): [https://kubernetes.io/docs/tasks/tools/#kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)
* Claude Code installer (used in examples): [https://claude.ai/install.sh](https://claude.ai/install.sh)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/claude-code-for-beginners/module/a295c914-f61e-47bb-8adc-7a3145745aa6/lesson/43ada54a-3944-4d25-8e4a-252597dc38c3" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.