What it is
Minikube is a tool that runs a single-node Kubernetes cluster inside a virtual machine on your local machine, allowing you to develop and test Kubernetes applications locally.
Installation
Linux
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
rm minikube-linux-amd64
macOS
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64
sudo install minikube-darwin-amd64 /usr/local/bin/minikube
rm minikube-darwin-amd64
Homebrew:
brew install minikube
Windows
Download the appropriate binary from the Minikube releases page. Alternatively, use Chocolatey:
choco install minikube
Docker (as driver)
Ensure Docker is installed and running. Minikube can use Docker as its driver, which is often faster than a full VM.
Core Concepts
- Node: Minikube runs a single-node Kubernetes cluster. This node is essentially a virtual machine.
- Driver: The underlying technology used to run the Kubernetes node. Common drivers include Docker, VirtualBox, KVM, and Hyper-V. The Docker driver is often the fastest.
- Addons: Pre-configured Kubernetes components that can be enabled to extend cluster functionality (e.g., dashboard, ingress).
Commands / Usage
Starting and Stopping Minikube
-
Start a cluster:
minikube startStarts a Kubernetes cluster using the default driver.
minikube start --driver=dockerStarts a Kubernetes cluster using the Docker driver.
minikube start --kubernetes-version=v1.28.0Starts a cluster with a specific Kubernetes version.
minikube start --nodes 3 --driver=dockerStarts a multi-node cluster (experimental, requires specific drivers and versions).
-
Stop the cluster:
minikube stopStops the Minikube VM without deleting its contents.
-
Delete the cluster:
minikube deleteStops and deletes the Minikube VM and all its data.
-
Pause the cluster:
minikube pausePauses the Minikube VM.
-
Unpause the cluster:
minikube unpauseResumes a paused Minikube VM.
-
SSH into the node:
minikube sshOpens an SSH session to the Minikube VM.
Cluster Information and Status
-
Check status:
minikube statusShows the status of the Minikube node and Kubernetes.
-
Get cluster info:
minikube cluster-infoPrints the Kubernetes master endpoint.
-
Get Kubernetes version:
minikube versionPrints the Minikube version.
-
Get node IP:
minikube ipPrints the IP address of the Minikube node.
Managing Addons
-
List available addons:
minikube addons listLists all available addons and their status.
-
Enable an addon:
minikube addons enable dashboardEnables the Kubernetes dashboard addon.
-
Disable an addon:
minikube addons disable ingressDisables the ingress addon.
-
Check addon status:
minikube addons statusShows the status of all enabled addons.
Accessing Services and Applications
-
Open the Kubernetes dashboard:
minikube dashboardOpens the Kubernetes dashboard in your default web browser.
-
Get the URL for a service:
minikube service <service-name> --urlGets the URL for a specific Kubernetes service.
minikube service my-app-service --url -n defaultGets the URL for
my-app-servicein thedefaultnamespace. -
Open a service in the browser:
minikube service my-app-serviceOpens a specific Kubernetes service in your default web browser.
Container Management
-
Load a Docker image into Minikube:
eval $(minikube docker-env) docker build -t my-custom-image .Builds an image using the Minikube Docker daemon, making it available within the Minikube cluster.
-
Get Docker environment variables:
minikube docker-envOutputs shell commands to set environment variables so your local Docker client can connect to Minikube’s Docker daemon.
eval $(minikube docker-env)Executes the output of
minikube docker-envto configure your shell.
Configuration and Settings
-
Set Minikube configuration:
minikube config set memory 4096Sets the memory allocated to the Minikube VM to 4096MB.
minikube config set cpus 4Sets the number of CPUs for the Minikube VM to 4.
-
Get Minikube configuration:
minikube config get memoryRetrieves the current memory setting for Minikube.
-
Unset Minikube configuration:
minikube config unset memoryResets the memory setting to its default.
Other Useful Commands
-
Update Minikube:
minikube update-contextUpdates the kubectl context to point to the Minikube cluster.
-
Get logs:
minikube logsShows the logs from the Minikube node.
-
Mount a directory:
minikube mount /local/path:/remote/pathMounts a local directory into the Minikube VM.
-
Start a tunnel:
minikube tunnelStarts a network tunnel to allow access to services of type LoadBalancer.
Common Patterns
-
Building and deploying an image:
eval $(minikube docker-env) docker build -t my-web-app:latest . kubectl create deployment my-web-app --image=my-web-app:latest kubectl expose deployment my-web-app --type=NodePort --port=80 minikube service my-web-appBuilds a Docker image using Minikube’s daemon, deploys it to Kubernetes, exposes it as a NodePort service, and opens it in the browser.
-
Using Ingress: First, ensure the ingress addon is enabled:
minikube addons enable ingressThen, create an Ingress resource:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress spec: rules: - host: myapp.local http: paths: - path: / pathType: Prefix backend: service: name: my-app-service port: number: 80Apply the Ingress:
kubectl apply -f ingress.yamlAdd an entry to your hosts file (
/etc/hostson Linux/macOS,C:\Windows\System32\drivers\etc\hostson Windows):192.168.49.100 myapp.local(Replace
192.168.49.100withminikube ip) Then accesshttp://myapp.localin your browser. -
Accessing a LoadBalancer service: Create a service of type LoadBalancer:
apiVersion: v1 kind: Service metadata: name: my-lb-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancerApply the service:
kubectl apply -f service-lb.yamlStart the tunnel:
minikube tunnelThe tunnel will allocate an IP address for the LoadBalancer service, which you can then access.
Gotchas
- Docker driver vs. VM driver: The Docker driver is generally faster and uses fewer resources than VM-based drivers (like VirtualBox). However, it requires Docker to be running and configured.
- Resource allocation: If you experience slow performance, try increasing the memory and CPU allocated to Minikube using
minikube config set memory <MB>andminikube config set cpus <count>. Remember to restart Minikube after changing these settings. eval $(minikube docker-env): This command is crucial for using Minikube’s Docker daemon. If you forget to run it,docker buildwill use your local Docker daemon, and the image won’t be available inside Minikube. It only affects your current shell session.- Kubernetes version compatibility: Ensure your local
kubectlversion is compatible with the Kubernetes version running in Minikube. Minikube usually handles this well, but it’s good to be aware of. - Network access for LoadBalancer services: Services of type
LoadBalancertypically requireminikube tunnelto be running to get an external IP. Without the tunnel, the service might remain in a pending state. - Clearing state: If you encounter persistent issues,
minikube deletefollowed byminikube startis often the quickest way to get a clean slate.