The Kubernetes Workshop
上QQ阅读APP看书,第一时间看更新

Setting up Kubernetes

Had you asked the question, "How do you easily install Kubernetes?" three years ago, it would have been hard to give a compelling answer. Embarrassing, but true. Kubernetes is a sophisticated system, and getting it installed and managing it well isn't an easy task.

However, as the Kubernetes community has expanded and matured, more and more user-friendly tools have emerged. As of today, based on your requirements, there are a lot of options to choose from:

  • If you are using physical (bare-metal) servers or virtual machines (VMs), Kubeadm is a good fit.
  • If you're running on cloud environments, Kops and Kubespray can ease Kubernetes installation, as well as integration with the cloud providers. In fact, we will teach you how to deploy Kubernetes on AWS using Kops in Chapter 11, Build Your Own HA Cluster, and we will take another look at the various options we can use to set up Kubernetes.
  • If you want to drop the burden of managing the Kubernetes control plane (which we will learn about later in this chapter), almost all cloud providers have their Kubernetes managed services, such as Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), Azure Kubernetes Service (AKS), and IBM Kubernetes Service (IKS).
  • If you just want a playground to study Kubernetes in, Minikube and Kind can help you spin up a Kubernetes cluster in minutes.

We will use Minikube extensively throughout this book as a convenient learning environment. But before we proceed to the installation process, let's take a closer look at Minikube itself.

An Overview of Minikube

Minikube is a tool that can be used to set up a single-node cluster, and it provides handy commands and parameters to configure the cluster. It primarily aims to provide a local testing environment. It packs a VM containing all the core components of Kubernetes that get installed onto your host machine, all at once. This allows it to support any operating system, as long as there is a virtualization tool (also known as a Hypervisor) pre-installed. The following are the most common Hypervisors supported by Minikube:

  • VirtualBox (works for all operating systems)
  • KVM (Linux-specific)
  • Hyperkit (macOS-specific)
  • Hyper-V (Windows-specific)

Regarding the required hardware resources, the minimum requirement is 2 GB RAM and any dual-core CPU that supports virtualization (Intel VT or AMD-V), but you will, of course, need a more powerful machine if you are trying out heavier workloads.

Just like any other modern software, Kubernetes provides a handy command-line client called kubectl that allows users to interact with the cluster conveniently. In the next exercise, we will set up Minikube and use some basic kubectl commands. We will go into more detail about kubectl in the next chapter.

Exercise 2.01: Getting Started with Minikube and Kubernetes Clusters

In this exercise, we will use Ubuntu 20.04 as the base operating system to install Minikube, using which we can start a single-node Kubernetes cluster easily. Once the Kubernetes cluster has been set up, you should be able to check its status and use kubectl to interact with it:

Note

Since this exercise deals with software installations, you will need to be logged in as root/superuser. A simple way to switch to being a root user is to run the following command: sudo su -.

In step 9 of this exercise, we will create a regular user and then switch back to it.

  1. First, ensure that VirtualBox is installed. You can confirm this by using the following command:

    which VirtualBox

    You should see the following output:

    /usr/bin/VirtualBox

    If VirtualBox has been successfully installed, the which command should show the path of the executable, as shown in the preceding screenshot. If not, then please ensure that you have installed VirtualBox as per the instructions provided in the Preface.

  2. Download the Minikube standalone binary by using the following command:

    curl -Lo minikube https://github.com/kubernetes/minikube/releases/download/<version>/minikube-<ostype-arch> && chmod +x minikube

    In this command, <version> should be replaced with a specific version, such as v1.5.2 (which is the version we will use in this chapter) or the latest. Depending on your host operating system, <ostype-arch> should be replaced with linux-amd64 (for Ubuntu) or darwin-amd64 (for macOS).

    Note

    To ensure compatibility with the commands provided in this book, we recommend that you install Minikube version v1.5.2.

    You should see the following output:

    Figure 2.1: Downloading the Minikube binary

    The preceding command contains two parts: the first command, curl, downloads the Minikube binary, while the second command, chmod, changes the permission to make it executable.

  3. Move the binary into the system path (in the example, it's /usr/local/bin) so that we can directly run Minikube, regardless of which directory the command is run in:

    mv minikube /usr/local/bin

    When executed successfully, the move (mv) command does not give a response in the terminal.

  4. After running the move command, we need to confirm that the Minikube executable is now in the correct location:

    which minikube

    You should see the following output:

    /usr/local/bin/minikube

    Note

    If the which minikube command doesn't give you the expected result, you may need to explicitly add /usr/local/bin to your system path by running export PATH=$PATH:/usr/local/bin.

  5. You can check the version of Minikube using the following command:

    minikube version

    You should see the following output:

    minikube version: v1.5.2

    commit: 792dbf92a1de583fcee76f8791cff12e0c9440ad-dirty

  6. Now, let's download kubectl version v1.16.2 (so that it's compatible with the version of Kubernetes that our setup of Minikube will create later) and make it executable by using the following command:

    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.16.2/bin/<ostype>/amd64/kubectl && chmod +x kubectl

    As mentioned earlier, <ostype> should be replaced with linux (for Ubuntu) or darwin (for macOS).

    You should see the following output:

    Figure 2.2: Downloading the kubectl binary

  7. Then, move it to the system path, just like we did for the executable of Minikube earlier:

    mv kubectl /usr/local/bin

  8. Now, let's check whether the executable for kubectl is at the correct path:

    which kubectl

    You should see the following response:

    /usr/local/bin/kubectl

  9. Since we are currently logged in as the root user, let's create a regular user called k8suser by running the following command:

    useradd k8suser

    Enter your desired password when you are prompted for it. You will also be prompted to enter other details, such as your full name. You may choose to skip those details by simply pressing Enter. You should see an output similar to the following:

    Figure 2.3: Creating a new Linux user

    Enter Y and hit Enter to confirm the final prompt for creating a user, as shown at the end of the previous screenshot.

  10. Now, switch user from root to k8suser:

    su - k8suser

    You should see the following output:

    root@ubuntu:~# su – k8suser

    k8suser@ubuntu:~$

  11. Now, we can create a Kubernetes cluster using minikube start:

    minikube start --kubernetes-version=v1.16.2

    Note

    If you want to manage multiple clusters, Minikube provides a --profile <profile name> parameter to each cluster.

    It will take a few minutes to download the VM images and get everything set up. After Minikube has started up successfully, you should see a response that looks similar to the following:

    Figure 2.4: Minikube first startup

    As we mentioned earlier, Minikube starts up a VM instance with all the components of Kubernetes inside it. By default, it uses VirtualBox, and you can use the --vm-driver flag to specify a particular hypervisor driver (such as hyperkit for macOS). Minikube also provides the --kubernetes-version flag so you can specify the Kubernetes version you want to use. If not specified, it will use the latest version that was available when the Minikube release was finalized. In this chapter, to ensure compatibility of the Kubernetes version with the kubectl version, we have specified Kubernetes version v1.16.2 explicitly.

    The following commands should help establish that the Kubernetes cluster that was started by Minikube is running properly.

  12. Use the following command to get the basic status of the various components of the cluster:

    minikube status

    You should see the following response:

    host: Running

    kubelet: Running

    apiserver: Running

    kubeconfig: Configured

  13. Now, let's look at the version of the kubectl client and Kubernetes server:

    kubectl version --short

    You should see the following response:

    Client Version: v1.16.2

    Server Version: v1.16.2

  14. Let's learn how many machines comprise the cluster and get some basic information about them:

    kubectl get node

    You should see a response similar to the following:

    NAME STATUS ROLES AGE VERSION

    minikube Ready master 2m41s v1.16.2

After finishing this exercise, you should have Minikube set up with a single-node Kubernetes cluster. In the next section, we will enter the Minikube VM to take a look at how the cluster is composed and the various components of Kubernetes that make it work.