Docker For Mac Kubernetes

How to Install Kubernetes on Mac. This is a step-by-step guide to installing and running Kubernetes on your Mac so that you can develop applications locally. You will be guided through running and accessing a Kubernetes cluster on your local machine using the following tools: Homebrew; Docker for Mac; Minikube; virtualbox; kubectl.

Up until the last Docker update (Docker 3.0.3) or the latest MacOs update (Big Sur) or IntelliJ (2020.3 ultimate), changing a file in the IDE resulted in immediate visibility of the changes in the file on the Kubernetes pod in the Docker VM. (Note that Docker Desktop does have support for Kubernetes built in, but it is an older version of Kubernetes, and limited to a single node cluster.) You can be up and exploring Kubernetes on Mac within 10 minutes, with all the advantages of an API managed, immutable, secure Kubernetes OS. Docker for Mac required Kubernetes features are only accessible on macOS for now; Docker for Windows and Docker Enterprise Edition betas will follow at a later date. If you need to install a new copy of Docker for Mac you can download it from the Docker Store.

This is a step-by-step guide to installing and running Kubernetes on your Mac so that you can develop applications locally.

You will be guided through running and accessing a Kubernetes cluster on your local machine using the following tools:

  • Homebrew
  • Docker for Mac
  • Minikube
  • virtualbox
  • kubectl

Installation Guide

The only pre-requisite for this guide is that you have Homebrew installed. Homebrew is a package manager for the Mac. You’ll also need Homebrew Cask, which you can install after Homebrew by running brew tap caskroom/cask in your Terminal.

  1. Install Docker for Mac. Docker is used to create, manage, and run our containers. It lets us construct containers that will run in Kubernetes Pods.

  2. Install VirtualBox for Mac using Homebrew. Run brew cask install virtualbox in your Terminal. VirtualBox lets you run virtual machines on your Mac (like running Windows inside macOS, except for a Kubernetes cluster.)

    Skip to step three if everything has worked to this point.

    In my case, I already had the non-Homebrew VirtualBox app installed which caused issues when trying to start minikube.

    If you already have VirtualBox installed, start the installation as before with brew cask install virtualbox. You will get a warning that confirms this saying Warning: Cask 'virtualbox' is already installed.. Once this is confirmed, you can reinstall VirtualBox with Homebrew by running brew cask reinstall virtualbox.

    If you happen to have VirtualBox already running when you do this, you could see an error saying Failed to unload org.virtualbox.kext.VBoxDrv - (libkern/kext) kext is in use or retained (cannot unload).

    This is because the kernel extensions that VirtualBox uses were in use when the uninstall occurred. If you scroll up in the output of that command, beneath Warning! Found the following active VirtualBox processes: you’ll see a list of the processes that need to be killed.

    Kill each of these in turn by running kill first_column_number (first_column_number is the process identifier for that process).

    Now re-run brew cask reinstall virtualbox and it should succeed.

  3. Install kubectl for Mac. This is the command-line interface that lets you interact with Kuberentes. Run brew install kubectl in your Terminal.

  4. Install Minikube via the Installation > OSX instructions from the latest release. At the time of writing, this meant running the following command in Terminal…

    Minikube will run a Kubernetes cluster with a single node.

  5. Everything should work! Start your Minikube cluster with minikube start. Then run kubectl api-versions. If you see a list of versions, everything’s working! minikube start might take a few minutes.

  6. At this point, I got an error saying Error starting host: Error getting state for host: machine does not exist. because I had previously tried to run Minikube. You can fix this by running open ~/.minikube/ to open Minikube’s data files, and then deleting and deleting the machines directory. Then run minikube start again.

Docker for mac kubernetes tutorial

Come Together

You’ve installed all these tools and everything looks like it’s working. A quick explanation of how the components relate is needed.

  • VirtualBox is a generic tool for running virtual machines. You can use it to run Ubuntu, Windows, etc. inside your macOS operating system host.
  • Minikube is a Kubernetes-specific package that runs a Kubernetes cluster on your machine. That cluster has a single node and has some unique features that make it more suitable for local development. Minikube tells VirtualBox to run. Minikube can use other virtualization tools—not just VirtualBox—however these require extra configuration.
  • kubectl is the command line application that lets you interact with your Minikube Kubernetes cluster. It sends request to the Kubernetes API server running on the cluser to manage your Kubernetes environment. kubectl is like any other application that runs on your Mac—it just makes HTTP requests to the Kubernetes API on the cluster.

The Docker team announced support for running Kubernetes using the Dockerfor Mac application. For a while, it was only available on unstable Edgereleases of the Mac application.

Docker for Mac’s Kubernetes cluster is now available in the stable release channel! 🎉

This makes running Kubernetes for local development somuch easier. You no longer need to install and run a separate cluster usingsomething like Minikube. (Though, if you need to, I have a guide on installingMinikube on your Mac.)

It's available on Docker Community Edition version 18.06.0-ce-mac70 2018-07-25 or later.

Docker For Mac Kubernetes Local Image

Setting up kubectl with Docker for Mac Kubernetes

Setting up kubectl to use Docker for Mac is simple.

Docker For Mac Kubernetes Tutorial

  1. If you don't already have kubectl installed, the easiest way is with Homebrew.

    brew install kubernetes-cli

  2. Install the most recent version of Docker for Mac, one that includes the Kubernetescluster functionality.
  3. Click the Docker menu bar icon > Preferences, then click the Kubernetes tab. Click the checkboxto enable Kubernetes and switch the default orchestrator to Kubernetes. Docker might take a fewminutes to install more components here.
  4. Docker for Mac will start the Kubernetes cluster. Once it’s running,click the Docker menu bar icon again and expand the Kubernetes menu item. Then under Context, selectdocker-for-desktop. This will switch kubectl to the docker-for-desktop context.
  5. Run kubectl config current-context in a Terminal window. It should output docker-for-desktopif kubectl’s context has updated.
  6. If you’re curious, you can also see the configuration settings that kubectl uses to connect to Docker for Mac’s Kubernetes cluster. Run kubectl config view to outputall your available kubectl contexts. (I have a chapter in my book that explains kubectl config in detail)

Running your first Kubernetes pod with Docker for Mac

Docker For Mac Kubernetes

Once you’ve set up kubectl to use Docker for Mac’s Kubernetes cluster,you can run your first pod!

Docker For Mac Kubernetes Ingress

Create a file called pod.yaml containing the following YAML configuration.

Now run the pod, and then check its status as it gets run on Docker for Mac’s single node cluster

Docker for mac kubernetes expose service

Does this replace Minikube?

Docker For Mac Kubernetes Software

I’d expect Minikube to stick around for a while—it’s a much more stable and matureimplementation of a single-node Kubernetes cluster that you can run locallyon your Mac. But Docker for Mac is a great, simple way to run a simple cluster on your local machine. Plus, the Docker menu bar icon gives you a new, fast way to switch kubectl contexts.