Skip to content

Installing Kubernetes on MacOS

I am assuming you have virtualbox installed on your Mac.

To test most of the stuff on k8s you don’t need multiple nodes, running one node cluster is pretty much what you need.

First we need to install kubectl, a tool to interact with kubernetes cluster:

➜  ~ curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s \
  https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/darwin/amd64/kubectl \
  && chmod +x ./kubectl \
  && sudo mv ./kubectl /usr/local/bin/kubectl

Then we need Minikube – which is a tool that provisions and manages single-node Kubernetes clusters:

➜  ~ curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.23.0/minikube-darwin-amd64 \
  && chmod +x minikube \
  && sudo mv minikube /usr/local/bin/

Now we can start the VM:

➜  ~ minikube  start
Starting local Kubernetes v1.8.0 cluster...
Starting VM...
Downloading Minikube ISO
 140.01 MB / 140.01 MB [============================================] 100.00% 0s
Getting VM IP address...
Moving files into cluster...
Downloading localkube binary
 148.56 MB / 148.56 MB [============================================] 100.00% 0s
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.

Let’s check everything is working:

➜  ~ minikube  version
minikube version: v0.23.0

➜  ~ kubectl get nodes
NAME       STATUS    ROLES     AGE       VERSION
minikube   Ready     <none>    11h       v1.8.0

➜  ~ kubectl version
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.4", GitCommit:"9befc2b8928a9426501d3bf62f72849d5cbcd5a3", GitTreeState:"clean", BuildDate:"2017-11-20T05:28:34Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.0", GitCommit:"0b9efaeb34a2fc51ff8e4d34ad9bc6375459c4a4", GitTreeState:"dirty", BuildDate:"2017-10-17T15:09:55Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
➜  ~


➜  ~ kubectl cluster-info


Kubernetes master is running at https://192.168.99.101:8443

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

The most simple deployment would probably be running busybox as a simple http server with netcat:

➜  ~ kubectl run busybox --image=busybox --port 8080 \
	 -- sh -c "while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; \
	 echo 'smallest http server'; } | nc -l -p  8080; done"
deployment "busybox" created

We now need to expose it as a service to connect to it:

➜  ~ kubectl expose deployment busybox --type=NodePort
service "busybox" exposed

Next we need to get the kubernetes URL for our service:

➜  ~ minikube service busybox --url
http://192.168.99.100:32301

Finally we can test it:

➜  ~ curl http://192.168.99.100:32301
smallest http server

Alternatively, you can skip the service creation by simply using port-forwarding. For this
you need to find pod name first and then pass it to forwarder:

➜  ~ kubectl get pod
NAME                      READY     STATUS    RESTARTS   AGE
busybox-884dc476b-nq2hr   1/1       Running   0          39s

➜  ~ kubectl port-forward busybox-884dc476b-nq2hr  8080:8080
Forwarding from 127.0.0.1:8080 -> 8080

We can now send requests through localhost:

➜  ~ curl localhost:8080
Handling connection for 8080
smallest http server

Finally if you don’t want to type ‘kubectl’ every time, add the alias for it:

➜  ~ grep kubectl .zshrc
alias k="kubectl"

Now you will be able to run commands by just typing ‘k’:

➜  ~ k get pod xxx
Error from server (NotFound): pods "xxx" not found

Next time we will look at how to install k8s on aws.