learn-minikube
learn-minikube copied to clipboard
#+title: Learning Kubernetes with Minikube #+author: Tim Dysinger #+options: toc:nil ^:nil #+startup: content hidestars hideblocks
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.
- Minikube Setup
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.
** Install Kubernetes Minikube Executable
Ubuntu: #+begin_src sh ( 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 ) #+end_src
MacOS: #+begin_src sh ( 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 ) #+end_src
*** Install a Virtual Machine Driver
Ubuntu (KVM): #+begin_src sh 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 #+end_src
Ubuntu or MacOS (VirtualBox): Head over to https://virtualbox.org/downloads and use their instructions
** Install Kubernetes Control Executable
Ubuntu: #+begin_src sh ( cd ~/.local/bin ; curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/linux/amd64/kubectl ; chmod +x kubectl ) #+end_src
MacOS: #+begin_src sh ( cd ~/.local/bin ; curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/darwin/amd64/kubectl ; chmod +x kubectl ) #+end_src
- Minikube Management
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.
** Start Kubernetes with Minikube
KVM (Linux):
#+begin_src sh
minikube start
--cpus=1
--disk-size="10g"
--kubernetes-version="v1.5.1"
--memory=1024
--vm-driver=kvm
#+end_src
VirtualBox (Linux|Mac):
#+begin_src sh
minikube start
--cpus=1
--disk-size="10g"
--kubernetes-version="v1.5.1"
--memory=1024
--vm-driver=virtualbox
#+end_src
** Exercise Kubernetes Control
Show the version information of Kubernetes client & server. #+begin_src sh kubectl version #+end_src
Show a summary of services & their URLs running on the cluster. #+begin_src sh kubectl cluster-info #+end_src
** View Kubernetes Dashboard
This is a convenient way to fire up the Kubernetes dashboard in the browser. #+begin_src sh minikube dashboard #+end_src
** Stop Kubernetes with Minikube
This doesn't destroy the cluster. It just stops it. You can use plain 'start' to fire it up later. #+begin_src sh minikube stop #+end_src
** Start Kubernetes again with Minikube
If you've previously shutdown your computer or issued a minikube stop then you
can restart it with a simple start command.
#+begin_src sh
minikube start
#+end_src
** SSH Into Minkube Kubernetes Nodes
If you want to mess around inside of the node that hosts your minikube instance, you can SSH. #+begin_src sh minikube ssh #+end_src
Or just run commands directly on the minikube instance #+begin_src sh minikube ssh 'docker images' #+end_src
** Deleting Kubernetes with Minikube
This will completely destroy your minikube instance. This is useful if want to start fresh. #+begin_src sh minikube delete #+end_src
- Kubernetes Learning
** Lesson 1 - Deploying Your First Application
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).
*** Kubernetes Deployment File
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/
#+begin_src yaml :tangle 1-webserver/deployment.yaml 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 #+end_src
*** Kubernetes Service File
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/
#+begin_src yaml :tangle 1-webserver/service.yaml apiVersion: v1 kind: Service metadata: name: webserver spec: ports: - name: http port: 80 targetPort: http selector: app: webserver type: LoadBalancer #+end_src
*** Deploy the Webserver & Create the Service
In the 1-webserver directory run these commands to deploy, pause for 10 seconds & then gather information about your deploy. #+begin_src sh :tangle 1-webserver/deploy.sh :shebang "#!/bin/bash -eux" kubectl create -f . sleep 10 kubectl get service webserver kubectl get deployment webserver kubectl get replicasets kubectl get pods #+end_src
If you didn't get enough information from the the get command you can dive
deeper with describe.
#+begin_src sh :tangle 1-webserver/deploy.sh :shebang "#!/bin/bash -eux"
kubectl describe service webserver
kubectl describe deployment webserver
kubectl describe replicasets webserver
kubectl describe pods webserver
#+end_src
*** Accessing Your Service in Your Browser
This command will open your new webserver service in your browser, using the exposed service port on your host VM network. #+begin_src sh :tangle 1-webserver/deploy.sh :shebang "#!/bin/bash -eux" minikube service webserver #+end_src
*** Customize the Webserver Image
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 #+begin_src html :tangle 1-webserver/html/index.html
HELLO FROM THE UPDATED WEBSERVER!
#+end_srcThen 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.
#+begin_src dockerfile :tangle 1-webserver/Dockerfile
FROM nginx:stable-alpine
COPY html /usr/share/nginx/html
#+end_src
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.
#+begin_src sh :tangle 1-webserver/update.sh :shebang "#!/bin/bash -eux"
eval $(minikube docker-env)
docker build -t webserver:0.1.0 .
#+end_src
*** Updating the Deployment
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. #+begin_src sh :tangle 1-webserver/update.sh :shebang "#!/bin/bash -eux" 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 #+end_src
Remember you can also go to the dashboard & look in your browser as well. #+begin_src sh :tangle 1-webserver/update.sh :shebang "#!/bin/bash -eux" minikube dashboard #+end_src
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.
*** View Your Updates in the Browser
#+begin_src sh :tangle 1-webserver/update.sh :shebang "#!/bin/bash -eux" minikube service webserver #+end_src 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. #+begin_src sh :tangle 1-webserver/update.sh :shebang "#!/bin/bash -eux" curl -sSL $(minikube service --url webserver) #+end_src *** Deleting your application (optional)
You can delete your Deployment and Service at any time. It wont hurt anything. Deploy it again later if you repeating the steps above. #+begin_src sh kubectl delete service webserver kubectl delete deployment webserver #+end_src