This is a tutorial on learning Kubernetes locally with Minikube. We intend to add to it on a weekly basis until we explore most the Kubernetes features. Follow along if you like.
This tutorial assumes that you know Docker well enough to build an image from the command line. You do not need a local Docker daemon running. You only need the `docker` client executable. Minikube provides its own Docker daemon.
Ubuntu:
( cd ~/.local/bin ;# OR YOUR FAVORITE PERSONAL BIN DIR IN YOUR $PATH
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.14.0/minikube-linux-amd64 ;
chmod +x minikube )
MacOS:
( cd ~/.local/bin ;# OR YOUR FAVORITE PERSONAL BIN DIR IN YOUR $PATH
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.14.0/minikube-darwin-amd64 ;
chmod +x minikube )
Ubuntu (KVM):
sudo ( apt-get update ;
apt install libvirt-bin qemu-kvm ;
usermod -a -G libvirtd $(whoami) )
( cd ~/.local/bin ;# OR YOUR FAVORITE PERSONAL BIN DIR IN YOUR $PATH
curl -OL https://github.com/dhiltgen/docker-machine-kvm/releases/download/v0.7.0/docker-machine-driver-kvm ;
chmod +x docker-machine-driver-kvm )
newgrp libvirtd
Ubuntu or MacOS (VirtualBox): Head over to https://virtualbox.org/downloads and use their instructions
Ubuntu:
( cd ~/.local/bin ;
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/linux/amd64/kubectl ;
chmod +x kubectl )
MacOS:
( cd ~/.local/bin ;
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/darwin/amd64/kubectl ;
chmod +x kubectl )
Minikube helps you manage a local single-node Kubernetes cluster. You will still use `kubectl` for all Kubernetes management functions like you would with a real cluster. Here are some basic operation examples.
KVM (Linux):
minikube start \
--cpus=1 \
--disk-size="10g" \
--kubernetes-version="v1.5.1" \
--memory=1024 \
--vm-driver=kvm
VirtualBox (Linux|Mac):
minikube start \
--cpus=1 \
--disk-size="10g" \
--kubernetes-version="v1.5.1" \
--memory=1024 \
--vm-driver=virtualbox
Show the version information of Kubernetes client & server.
kubectl version
Show a summary of services & their URLs running on the cluster.
kubectl cluster-info
This is a convenient way to fire up the Kubernetes dashboard in the browser.
minikube dashboard
This doesn’t destroy the cluster. It just stops it. You can use plain ‘start’ to fire it up later.
minikube stop
If you’ve previously shutdown your computer or issued a `minikube stop` then you can restart it with a simple start command.
minikube start
If you want to mess around inside of the node that hosts your minikube instance, you can SSH.
minikube ssh
Or just run commands directly on the minikube instance
minikube ssh 'docker images'
This will completely destroy your minikube instance. This is useful if want to start fresh.
minikube delete
This is a a simple web server. It only serves static files & doesn’t need to connect to a database. We need two components of Kubernetes to deploy it. The Deployment (defines the distributed application) and the Service (exposes the distributed application externally).
First we need to tell Kubernetes what to deploy. We’ll use a Deployment file to describe this. Deployment files contain meta-data with pod and volume specifications.
All the details about your docker images, ports, environment variables and the like go here. In this specific case, it’s pretty simple. We label the deployment, pod and container as ‘webserver’ so we can select based on that metadata later when we define the service. We have a regular www port 80 to expose from 1 container. We’ve requested 1 replica to be available in the cluster.
http://kubernetes.io/docs/user-guide/deployments/
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: webserver
spec:
replicas: 1
template:
metadata:
labels:
app: webserver
spec:
containers:
- name: webserver
image: nginx:stable-alpine
ports:
- name: http
containerPort: 80
Next we define the Service file. Here we tell Kubernetes that we want to select on app=webserver from our above Deployment file and expose http port 80 to the world as a LoadBalancer.
http://kubernetes.io/docs/user-guide/services/
apiVersion: v1
kind: Service
metadata:
name: webserver
spec:
ports:
- name: http
port: 80
targetPort: http
selector:
app: webserver
type: LoadBalancer
In the 1-webserver directory run these commands to deploy, pause for 10 seconds & then gather information about your deploy.
kubectl create -f .
sleep 10
kubectl get service webserver
kubectl get deployment webserver
kubectl get replicasets
kubectl get pods
If you didn’t get enough information from the the get `command` you can dive deeper with `describe`.
kubectl describe service webserver
kubectl describe deployment webserver
kubectl describe replicasets webserver
kubectl describe pods webserver
This command will open your new webserver service in your browser, using the exposed service port on your host VM network.
minikube service webserver
We want update our webserver. We’ll do that by creating a new webserver docker image. We want to see that our newly updated image deployed successfully. The easiest way to do this with a webserver is to put some new html content in the webserver directory. We’ll do by defining a new landing page for nginx (index.html). Place this in 1-webserver/html/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Updated!</title>
</head>
<body>
<h1>HELLO FROM THE UPDATED WEBSERVER!</h1>
</body>
</html>
Then we’ll use a Dockerfile to extend our webserver’s default nginx webserver image with the custom HTML page above. Create `1-webserver/Dockerfile` with the following content.
FROM nginx:stable-alpine
COPY html /usr/share/nginx/html
In the `1-webserver` directory issues these commands to direct your docker client to use the minikube instance’s docker daemon and then build a new Docker image for the webserver deployment.
eval $(minikube docker-env)
docker build -t webserver:0.1.0 .
Upgrade to the new version of our webserver’s docker image, pause for 10 seconds while it deploys & then gather information about how it went.
kubectl set image deployment/webserver webserver=webserver:0.1.0
sleep 10
kubectl get service webserver
kubectl get deployment webserver
kubectl get replicasets
kubectl get pods
Remember you can also go to the dashboard & look in your browser as well.
minikube dashboard
You can see by looking at Replica Sets that you’ve had two deployments. There is an option to roll back that we’ll explore later. This is useful or operations.
minikube service webserver
Make sure you refresh your browser. Sometimes browser caching can plan tricks on you.
You can also check it your webserver’s output on the command line. Use the –url flag to just print the URL instead of opening it in the browser. Combine this with curl to pull the webpage & print it on the console.
curl -sSL $(minikube service --url webserver)
You can delete your Deployment and Service at any time. It wont hurt anything. Deploy it again later if you repeating the steps above.
kubectl delete service webserver
kubectl delete deployment webserver