> ## 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.

# Demo Creating Kubernetes Clusters with Claude

> Demonstrates using Claude Code to generate and run an idempotent Bash script that installs prerequisites and launches a multi-node Minikube Kubernetes cluster with a demo nginx app

In this lesson you'll see how a single detailed prompt to [Claude Code For Beginners](https://learn.kodekloud.com/user/courses/claude-code-for-beginners) can generate and execute an idempotent Bash script that:

* Installs prerequisites on an Ubuntu VM,
* Configures kernel settings and systemd services,
* Installs Docker, kubectl (from pkgs.k8s.io), and Minikube,
* Starts a multi-node Minikube Kubernetes cluster (Docker driver),
* Enables add-ons (metrics-server, ingress, dashboard),
* Deploys an nginx demo application and prints access information.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/4gvfMR9MneljS-Uy/images/Claude-Code-For-Beginners/Advanced-Features/Demo-Creating-Kubernetes-Clusters-with-Claude/creating-kubernetes-clusters-claude-demo.jpg?fit=max&auto=format&n=4gvfMR9MneljS-Uy&q=85&s=adf031747e75a17d0e41025067201b0f" alt="A presentation slide titled &#x22;Creating Kubernetes Clusters with Claude&#x22; with a large &#x22;Demo&#x22; label on a dark curved background. A small &#x22;© Copyright KodeKloud&#x22; appears in the bottom corner." width="1920" height="1080" data-path="images/Claude-Code-For-Beginners/Advanced-Features/Demo-Creating-Kubernetes-Clusters-with-Claude/creating-kubernetes-clusters-claude-demo.jpg" />
</Frame>

Estimated time: \~15–30 minutes (depending on downloads and VM resources)\
Difficulty: Intermediate

Environment and prerequisites

| Requirement                          | Notes / Recommendation                                                                                                       |
| ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
| VM (Linux — Ubuntu) with sudo access | Local VM, EC2, GCP, Azure, etc.                                                                                              |
| RAM & CPU                            | Recommended: at least 8 GB RAM and 2+ vCPUs. If you plan a 3-node profile with 4 GB per node, increase host RAM accordingly. |
| Internet access                      | Required to download packages and container images                                                                           |
| Shell access                         | Ability to run curl and install packages with sudo                                                                           |

1 — Install Claude Code on the VM

Install the Claude Code CLI on the machine where you plan to run this automation. For the KodeKloud course use the provided installer:

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

After installing, launch the Claude Code CLI and follow the interactive sign-in. You may be asked to open a browser to paste an authorization code. Example abbreviated output:

```text theme={null}
Browser didn't open? Use the url below to sign in:
https://claude.ai/oauth/authorize?code=...

Logged in as [REDACTED_EMAIL]
Login successful. Press Enter to continue...
```

Once signed in you’ll see the Claude Code 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>"

? for shortcuts
```

2 — Provide a detailed prompt (the setup guide)

We provided Claude Code a comprehensive prompt called “Minikube Kubernetes Demo Setup Guide” describing:

* Prerequisites and system-level configuration (sysctls, modules),
* Packages to install (Docker, kubectl, Minikube),
* Cluster profile (name, node count, CPU, memory),
* Add-ons to enable (metrics-server, ingress, dashboard),
* Demo application to deploy (nginx "hello" deployment),
* Verification steps and fallbacks (port-forward).

Claude Code used that prompt to generate a single idempotent Bash script that performs the entire setup. The script below is the consolidated output saved as `/tmp/minikube-demo.sh`.

3 — The generated installation script

Save the following to `/tmp/minikube-demo.sh`. The script is written to be idempotent: it checks for existing installs and skips re-installation where appropriate.

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

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

# Determine non-root user (when run with sudo)
TARGET_USER="${SUDO_USER:-$USER}"

# Update package list
sudo apt-get update -y

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

# Configure kernel modules and sysctls
sudo modprobe br_netfilter || true
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
    # Add the non-root user (or current user) to the docker group
    sudo usermod -aG docker "$TARGET_USER" || true
fi

# Install kubectl (using official packages)
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://pkgs.k8s.io/core:/stable:/v1.34/deb/ /" \
        | sudo tee /etc/apt/sources.list.d/kubernetes.list
    sudo apt-get update -y
    sudo apt-get install -y kubectl
fi

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

# Start Minikube profile if it doesn't exist
if ! minikube profile list -o=json | grep -q "\"Name\": \"$PROFILE_NAME\""; then
    # Start minikube using the docker driver
    minikube start -p "$PROFILE_NAME" --driver=docker --nodes="$NODES" --cpus="$CPU" --memory="$MEM"
else
    echo "Minikube profile \"$PROFILE_NAME\" already exists; skipping start."
fi

# Enable addons (run under docker group context if necessary)
# Some addon operations may require being in the docker group context
if ! minikube -p "$PROFILE_NAME" addons list | grep -q "metrics-server.*enabled"; then
    newgrp docker <<'EOFF'
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
EOFF
fi

# Deploy demo app if not present
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

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

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

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

echo
echo "Fallback (port-forward from this VM):"
echo "  kubectl port-forward deploy/hello 8080:80"
echo "Then curl http://localhost:8080"
```

Make the script executable and run it:

```bash theme={null}
chmod +x /tmp/minikube-demo.sh
/tmp/minikube-demo.sh
```

4 — Handling Docker group and add-ons

When the installer adds your user to the docker group, the membership may not take effect until you start a new login shell. The generated script attempts to enable add-ons inside a `newgrp docker` subshell to avoid permission errors. If an add-on fails due to permissions, re-run the add-on commands after obtaining docker group privileges:

```bash theme={null}
# Ensure the current shell has docker group privileges, then:
newgrp docker
minikube -p demo addons enable metrics-server
minikube -p demo addons enable ingress
minikube -p demo addons enable dashboard
```

<Callout icon="warning" color="#FF6B6B">
  If you do not start a new shell or run `newgrp docker`, Minikube or addon commands may fail with permission errors. Log out and log back in, or use `newgrp docker`, before re-running addon commands.
</Callout>

5 — Test the demo application (port-forward)

Quick port-forward test from the VM:

```bash theme={null}
kubectl port-forward deploy/hello 8080:80 &
PF_PID=$!
sleep 2
curl -s http://localhost:8080 | head -n 5
kill "$PF_PID" 2>/dev/null || true
```

Alternatively, use the Minikube-provided service URL printed by the script:

```bash theme={null}
minikube -p demo service hello --url
```

6 — Expected result summary

After the script runs successfully you should see:

| Component | What to expect                                                                                   |
| --------: | ------------------------------------------------------------------------------------------------ |
|   Prereqs | ca-certificates, curl, gnupg, apt-transport-https, conntrack, socat, ebtables, ethtool installed |
|   Sysctls | net.bridge.bridge-nf-call-iptables=1 and net.ipv4.ip\_forward=1 applied                          |
|    Docker | Installed, enabled, and user added to docker group                                               |
|   kubectl | Installed from the official pkgs.k8s.io repository                                               |
|  Minikube | Installed and a 3-node Minikube profile named "demo" started (Docker driver)                     |
|   Add-ons | metrics-server, ingress, dashboard enabled (if permissions allow)                                |
|  Demo app | "hello" nginx Deployment created and exposed as a NodePort service                               |
|    Access | Demo URL(s) printed and a port-forward fallback provided                                         |

Example verification console snippets you may see:

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

and

```text theme={null}
=== Nodes ===
NAME                 STATUS   ROLES    AGE   VERSION
demo-m01             Ready    control-plane   2m    v1.33.1
demo-m02             Ready    <none>          2m    v1.33.1
demo-m03             Ready    <none>          2m    v1.33.1

=== Services ===
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
hello        NodePort    10.96.0.123      <none>        80:31902/TCP   1m

=== Demo URL(s) ===
http://192.168.49.2:31902
```

Notes and tips

<Callout icon="lightbulb" color="#1CB2FE">
  Running the full three-node demo on a single VM requires sufficient host resources. If you have limited RAM/CPU, reduce NODES or MEM in the script (for example, use 1 node or 2GB per node) to test locally. Also consider using fewer add-ons while troubleshooting.
</Callout>

Quick checklist (sanity checks)

* Ensure the VM has enough RAM and CPUs for the configured node count.
* Confirm internet connectivity for package and image downloads.
* If kubectl or minikube fail unexpectedly, inspect logs and retry after ensuring docker group membership is active.

Links and references

* [Claude Code For Beginners (KodeKloud course)](https://learn.kodekloud.com/user/courses/claude-code-for-beginners)
* [Kubernetes Documentation — Concepts](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/)
* Minikube: [https://minikube.sigs.k8s.io/](https://minikube.sigs.k8s.io/)
* Docker: [https://docs.docker.com/](https://docs.docker.com/)
* pkgs.k8s.io (kubectl packages): [https://pkgs.k8s.io/](https://pkgs.k8s.io/)

This lesson demonstrates how a single clear, structured prompt to Claude Code can produce an automated, idempotent script that reduces manual setup time for local Kubernetes testing with Minikube.

<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>
