Kubernetes: Getting Started with Minikube

The following documentation is for Mac users and to help get you started using Kubernetes with Minikube.

Install prerequisites

Install Oracle VirtualBox for Mac.

Install Docker for Mac.

Install Minikube for Mac.

brew cask install minikube

Install kubectl for Mac.

brew install kubectl

Launch minikube with the following command.

minikube start

Example

minikube start
Starting local Kubernetes v1.7.5 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.

You should now see minikube running in VirtualBox.

Test your minikube installation with the following commands.

minikube status

Example

minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100

Launch kubernetes dashboard.

minikube dashboard

Or you can view the dashboard URL.

minikube dashboard --url

Example

minikube dashboard --url
http://192.168.99.100:30000

Create a minikube directory for your project files and cd into it

mkdir minikube; cd minikube

Setup environment

This command sets up the Docker environment variables so a Docker client can communicate with the minikube Docker daemon.

eval $(minikube docker-env)

If you are managing multiple kubernetes clusters, you can view them using get-context.

kubectl config get-contexts

Make sure you are using the minikube cluster.

kubectl config use-context minikube

Helloworld Demo

Create httpd application

Within your minkube directory, create a helloworld-v1 directory and your public-html directory. Then cd into it.

mkdir -p helloworld/public-html; cd helloworld

Echo Hello World! into public-html/index.html.

echo "Hello World" > public-html/index.html

Create docker container

From within the helloworld dir, create Dockerfile.

vi Dockerfile
FROM httpd
COPY ./public-html/ /usr/local/apache2/htdocs/
EXPOSE 80

Build your docker image.

docker build -t helloworld:1.0 .

Example

docker build -t helloworld:1.0 .
Sending build context to Docker daemon 3.584kB
Step 1/3 : FROM httpd
 ---> 7239615c0645
Step 2/3 : COPY ./public-html/ /usr/local/apache2/htdocs/
 ---> 6f2cf58784ef
Step 3/3 : EXPOSE 80
 ---> Running in 7b1e2c50b246
Removing intermediate container 7b1e2c50b246
 ---> d4c64d7e968b
Successfully built d4c64d7e968b
Successfully tagged helloworld:1.0

Create deployment

kubectl run helloworld --image=helloworld:1.0 --port=80

Example

kubectl run helloworld --image=helloworld:1.0 --port=80
deployment "helloworld" created

A few view commands (because why not).

kubectl get services    # view services
kubectl get deployments # view deployments
kubectl get pods        # view pods
kubectl get events      # view events
kubectl config view     # view kubectl config

You should now be able to see your helloworld deployment within the kubernetes dashboard.

Now that we’ve created a Deployment, we need to create what is called a Service to expose the application.

kubectl expose deployment helloworld --type=NodePort --name=helloworld

Example

kubectl expose deployment helloworld --type=NodePort --name=helloworld
service "helloworld" exposed

You should now be able to see your helloworld service within the kubernetes dashboard.

Launch services

minikube service helloworld

Example

minikube service helloworld
Opening kubernetes service default/helloworld in default browser...

In my case, it opened my browser to http://192.168.99.100:31457/.

Logs
View your pods.

kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-1142207609-hrv11 1/1 Running 0 18m

View the pods logs.

kubectl logs helloworld-1142207609-hrv11

Example

kubectl logs helloworld-1142207609-hrv11
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.7. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.7. Set the 'ServerName' directive globally to suppress this message
[Wed Mar 28 23:38:05.123930 2018] [mpm_event:notice] [pid 1:tid 140311540979584] AH00489: Apache/2.4.33 (Unix) configured -- resuming normal operations
[Wed Mar 28 23:38:05.124010 2018] [core:notice] [pid 1:tid 140311540979584] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [28/Mar/2018:23:49:31 +0000] "GET / HTTP/1.1" 200 12
172.17.0.1 - - [28/Mar/2018:23:49:31 +0000] "GET /favicon.ico HTTP/1.1" 404 209
172.17.0.1 - - [28/Mar/2018:23:55:59 +0000] "GET / HTTP/1.1" 304 -

Update your application

Let’s update our application.

echo "Hello World Again" > public-html/index.html

Build a new version of your image.

docker build -t helloworld:2.0 .

View image on minikube vm.

minikube ssh docker images | grep helloworld

Example

minikube ssh docker images | grep helloworld
helloworld 2.0 a4a1a0a3f100 55 seconds ago 177.5 MB
helloworld 1.0 e6b9b5ebfd34 42 minutes ago 177.5 MB

Update the image of your Deployment.

kubectl set image deployment/helloworld helloworld=helloworld:2.0

Example

kubectl set image deployment/helloworld helloworld=helloworld:2.0
deployment "helloworld" image updated

Refresh your browser to view your updated deployment or run minikube service helloworld.

minikube service helloworld

Scaling

We are currently running 1 pod within our deployment. Lets make it 4.

First, open another terminal so you can watch the pods be created in realtime.

kubectl get pods -w

Scale the pods.

You should see the pods go from Pending, ContainerCreating to Running.

kubectl scale deployment helloworld --replicas=4

Clean up

This will delete the service and deployment called helloworld.

kubectl delete service,deployment helloworld