Learn Helm
上QQ阅读APP看书,第一时间看更新

Preparing a local Kubernetes environment with Minikube

Helm won't be able to deploy applications without access to a Kubernetes cluster. For this reason, let's discuss one option that users can follow to run their own cluster on their machine—Minikube.

Minikube is a community-driven tool that allows users to easily deploy a small, single-node Kubernetes cluster to their local machine. A cluster created with Minikube is created inside a virtual machine (VM), so it can be created and later discarded in a way that is isolated from the host operating system that the VM is running on. Minikube presents an excellent way to experiment with Kubernetes and it can also be used to learn how to use Helm alongside the examples provided throughout this book.

In the next few sections, we'll cover how Minikube can be installed and configured so that you have a Kubernetes cluster available while learning how to use Helm. For more comprehensive instructions, please refer to the Getting Started page from the official Minikube website at https://minikube.sigs.k8s.io/docs/start/.

Installing Minikube

Minikube, like the other tools that will be installed within this chapter, has binaries compiled for the Windows, macOS, and Linux operating systems. The easiest way to install the latest version of Minikube on Windows and macOS is via a package manager, such as Chocolatey for Windows and Homebrew for macOS.

Linux users will find it easier to install the latest minikube binary by downloading it from Minikube's GitHub releases page, though this method can also be used on Windows and macOS as well.

The following steps describe how you can install Minikube based on your machine and installation preference. Please note that Minikube version v1.5.2 was used during the writing and development of the examples used throughout this book.

To install it via a package manager (on Windows and macOS), do the following:

  • Use the following command for Windows:

    > choco install minikube

  • Use the following command for macOS:

    $ brew install minikube

The following steps show you how to install it via a download link (on Windows, macOS, and Linux).

The Minikube binary can be downloaded directly from its releases page on GitHub at https://github.com/kubernetes/minikube/releases/:

  1. At the bottom of the releases page, there is a section called Assets, which consists of the Minikube binaries available for the various supported platforms:
    Figure 2.1: The Minikube binaries from the GitHub releases page

    Figure 2.1: The minikube binaries from the GitHub releases page

  2. Under the Assets section, the binary that corresponds to the target platform should be downloaded. Once downloaded, you should rename the binary to minikube. If you are downloading the Linux binary, for example, you would run the following command:

    $ mv minikube-linux-amd64 minikube

  3. In order to execute minikube, Linux and macOS users may need to add the executable bit by running the chmod command:

    $ chmod u+x

  4. minikube should then be moved to a location managed by the PATH variable so that it can be executed from any location in your command line. The locations that the PATH variable contains vary depending on your operating system. For macOS and Linux users, these locations can be determined by running the following command in the Terminal:

    $ echo $PATH

  5. Windows users can determine the PATH variable's locations by running the following command in Command Prompt or PowerShell:

    > $env:PATH

  6. You can then move the minikube binary to a new location by using the mv command. The following example moves minikube to a common PATH location on Linux:

    $ mv minikube /usr/local/bin/

  7. You can verify your Minikube installation by running minikube version and ensuring that the displayed version corresponds with the version that was downloaded:

    $ minikube version

    minikube version: v1.5.2

    commit: 792dbf92a1de583fcee76f8791cff12e0c9440ad-dirty

Although you have downloaded Minikube, you will also need a hypervisor to be able to run your local Kubernetes cluster. This can be done by installing VirtualBox, which we will describe in the next section.

Installing VirtualBox

Minikube depends on the existence of hypervisors in order to install a single-node Kubernetes cluster on a VM. For this book, we have chosen to discuss VirtualBox as the hypervisor option, since it is the most flexible and is available on the Windows, macOS, and Linux operating systems. Additional hypervisor options for each operating system can be found in the official Minikube documentation at https://minikube.sigs.k8s.io/docs/start/.

Like Minikube, VirtualBox is easily installed via Chocolatey or Homebrew, but can also be easily installed using apt-get for Debian-based Linux and dnf for RPM/RHEL-based Linux:

  • Use the following code to install VirtualBox on Windows:

    > choco install virtualbox

  • Use the following code to install VirtualBox on macOS:

    $ brew cask install virtualbox

  • Use the following code to install VirtualBox on Debian-based Linux:

    $ apt-get install virtualbox

  • Use the following code to install VirtualBox on RHEL-based Linux:

    $ dnf install VirtualBox

Alternative methods of installing VirtualBox can be found at its official download page at https://www.virtualbox.org/wiki/Downloads.

With VirtualBox installed, Minikube must be configured to leverage VirtualBox as its default hypervisor. This configuration will be made in the next section.

Configuring VirtualBox as the designated hypervisor

VirtualBox can be made the default hypervisor by setting the vm-driver option of minikube to virtualbox:

$ minikube config set vm-driver virtualbox

Note that this command may produce the following warning:

These changes will take effect upon a minikube delete and then a minikube start

This message can be safely ignored if there are no active Minikube clusters on the workstation. This command states that any existing Kubernetes clusters will not make use of VirtualBox as the hypervisor until the cluster is deleted and then recreated.

The change to VirtualBox can be confirmed by assessing the value of the vm-driver configuration option:

$ minikube config get vm-driver

If all is well, the output will be as follows:

Virtualbox

In addition to configuring the default hypervisor, you can also configure the resources that are allocated to a Minikube cluster, discussed in the next section.

Configuring Minikube resource allocation

By default, Minikube will allocate two CPUs and 2 GB of RAM to its VM. These resources are sufficient for each of the examples in this book except for those in Chapter 7, which are more resource intensive. If your machine has the available resources, you should increase the default memory allocation to 4 GB (the CPU allocation can remain the same).

Run the following command to increase the default memory allocation of new Minikube VMs to 4 GB (4000 MB).

$ minikube config set memory 4000

This change can be verified by running the minikube config get memory command, similar to the way the vm-driver change was verified previously.

Let's continue exploring Minikube by discussing its basic usage.

Exploring the basic usage

Throughout this book, it will be handy to understand the key commands used in a typical Minikube operation. They will also be essential to understand during the execution of the examples provided throughout the course of this book. Fortunately, Minikube is an easy tool to get started with.

Minikube has three key subcommands:

  • start
  • stop
  • delete

The start subcommand is used to create a single-node Kubernetes cluster. It will create a VM and bootstrap the cluster within it. The command will terminate once the cluster is ready:

$ minikube start

minikube v1.5.2 on Fedora 30

  Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ...

  Preparing Kubernetes v1.16.2 on Docker '18.09.9' ...

  Pulling images ...

  Launching Kubernetes ...

  Waiting for: apiserver

  Done! kubectl is now configured to use 'minikube'

The stop subcommand is used to shut down the cluster and the VM. The state of the cluster and VM are saved to the disk, allowing users to run the start subcommand again to quickly begin working, rather than having to build a new VM from scratch. You should try to get into the habit of running minikube stop when you have finished working with a cluster that you would like to return to later:

$ minikube stop

  Stopping 'minikube' in virtualbox ...

  'minikube' stopped.

The delete subcommand is used to delete a cluster and the VM. This command erases the state of the cluster and VM, freeing up the space on the disk that was previously allocated. The next time minikube start is executed, a fresh cluster and VM will be created. You should run the delete subcommand when you would like to remove all of the allocated resources and work on a fresh Kubernetes cluster on your next invocation of minikube start:

$ minikube delete

  Deleting 'minikube' in virtualbox ...

  The 'minikube' cluster has been deleted.

  Successfully deleted profile 'minikube'

There are more Minikube subcommands available, but these are the main ones that you should be aware of.

With Minikube installed and configured on a local machine, you can now install kubectl, the Kubernetes command-line tool, and satisfy the remaining prerequisites for working with Helm.