04 Build a Kubernetes Cluster - Local Quick Setup #
From our previous learning, we already know that there are some necessary components in K8S and different roles in a cluster. In this section, we will quickly set up a local cluster to further deepen our understanding.
Solution Selection #
In the previous section, we learned that there are multiple functional components in K8S, and all these components need to be set up locally, requiring some basic knowledge and wasting a significant amount of time. This may potentially affect our goal of building a cluster.
Therefore, here we provide two simplest and easiest-to-use tools to achieve our goal:
KIND #
Introduction #
KIND (Kubernetes in Docker) is designed to provide a simpler and more efficient way to start a Kubernetes cluster. It is currently mainly used in environments like Kubernetes’ own CI.
Installation #
- You can directly download pre-compiled binary files from the Release page of the project. (The following example uses version v0.1.0 of the binary package)
Note: If you don’t want to use the binary package directly, but prefer to download it using go get sigs.k8s.io/kind
, it is not compatible with the configuration file mentioned below. Please refer to the article “Build Your Local Kubernetes Cluster Using Kind” for reference.
Update (February 5th, 2020): KIND has released version v0.7.0. If you want to use the new version, it is recommended to refer to the article “Creating a Kubernetes Cluster in an Offline Environment with Kind,” which uses the latest version of KIND.
Creating a Cluster #
Before using KIND, you need to have Docker installed on your local machine. This will not be covered in detail here.
Due to networking issues, we also need to create a configuration file that allows kind
to use a mirror source in China. (This operation is unnecessary in the latest version of KIND, as all the required images are already built in)
apiVersion: kind.sigs.k8s.io/v1alpha1
kind: Config
kubeadmConfigPatches:
- |
apiVersion: kubeadm.k8s.io/v1alpha3
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
pod-infra-container-image: registry.aliyuncs.com/google_containers/pause-amd64:3.1
- |
apiVersion: kubeadm.k8s.io/v1alpha3
kind: ClusterConfiguration
imageRepository: registry.aliyuncs.com/google_containers
kubernetesVersion: v1.12.2
networking:
serviceSubnet: 10.0.0.0/16
Save the above content as a kind-config.yaml
file, and then execute the following command.
kind create cluster --image kindest/node:v1.12.2 --config kind-config.yaml --name moelove
The following output is the result on my machine:
(MoeLove) ➜ kind ✗ kind create cluster --image kindest/node:v1.12.2 --config kind-config.yaml --name moelove
Creating cluster 'kind-moelove' ...
✓ Ensuring node image (kindest/node:v1.12.2) 🖼
✓ [kind-moelove-control-plane] Creating node container 📦
✓ [kind-moelove-control-plane] Fixing mounts 🗻
✓ [kind-moelove-control-plane] Starting systemd 🖥
✓ [kind-moelove-control-plane] Waiting for docker to be ready 🐋
✓ [kind-moelove-control-plane] Starting Kubernetes (this may take a minute) ☸
Cluster creation complete. You can now use the cluster with:
export KUBECONFIG="$(kind get kubeconfig-path --name="moelove")"
kubectl cluster-info
Here, by passing the kind-config.yaml
file to kind create cluster
and providing a name through the --name
parameter, we create a cluster.
Follow the instructions provided in the program output:
export KUBECONFIG="$(kind get kubeconfig-path --name="moelove")"
kubectl cluster-info
The following output is the result on my machine:
(MoeLove) ➜ kind ✗ export KUBECONFIG="$(kind get kubeconfig-path --name="moelove")"
(MoeLove) ➜ kind ✗ kubectl cluster-info
Kubernetes master is running at https://localhost:35911
KubeDNS is running at https://localhost:35911/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
(MoeLove) ➜ kind ✗ kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.3", GitCommit:"a4529464e4629c21224b3d52edfe0ea91b072862", GitTreeState:"clean", BuildDate:"2018-09-09T18:02:47Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.2", GitCommit:"17c77c7898218073f14c8d573582e8d2313dc740", GitTreeState:"clean", BuildDate:"2018-10-24T06:43:59Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
Note that you need to install kubectl
here. The installation of kubectl
can be referred to in the following content.
If you can see output similar to what I showed above when running kubectl cluster-info
, then your local K8S cluster has been successfully deployed. You can directly refer to section 5 or section 6 for further information.
If you already have some understanding of K8S and have a strong need for the Dashboard, you can directly refer to section 20.
Minikube #
Introduction #
Minikube is a set of tools provided by K8S to allow developers to run K8S on their personal computers. It is implemented in Go language and creates a single-node cluster running in a virtual machine by calling the virtualization manager.
Note: From here, it can be seen that there is no limit on the number of nodes for the basic functionality of the K8S cluster. A cluster can be created with only one node.
Prerequisites #
- First, it is necessary to confirm that the BIOS has enabled support for
VT-x
orAMD-v
virtualization. For specific methods, please refer to the articles Confirm whether BIOS virtualization is enabled and Enable BIOS virtualization support. - Secondly, we need to install a virtualization manager. The choice here depends on the actual operating system you are using. The official recommendations are as follows:
- macOS: VirtualBox or VMware Fusion or HyperKit. If using Hyperkit, please note that the system must be
OS X 10.10.3 Yosemite
or later. - Linux: VirtualBox or KVM.
- Windows: VirtualBox or Hyper-V.
- macOS: VirtualBox or VMware Fusion or HyperKit. If using Hyperkit, please note that the system must be
I personally recommend that regardless of the operating system you use for Minikube, you choose Virtualbox as the virtualization manager. 1. Virtualbox is relatively easy to use and install. 2. Minikube has better support for it, and it has already been tested by a large number of users, with most related issues fixed.
If you are using a Linux system, there is another option. You can set the --vm-driver
parameter of Minikube to none, provided that Docker is correctly installed on your machine. This method does not require virtualization support.
Installing kubectl #
In the previous section, we learned that the K8S cluster is a typical C/S architecture and has a CLI tool called kubectl
provided by the official. Here, we need to install kubectl
so that we can manage the deployed cluster in subsequent steps.
Note: Due to API version compatibility issues, it is recommended to keep the kubectl version consistent with the K8S cluster version, or at most differ within a minor version.
The official documentation provides detailed installation methods for operating systems such as macOS, Linux, and Windows. Here, we will not repeat them. Documentation.
Here is a different installation method from the official documentation.
- Access the CHANGELOG file of the K8S main repository to find the version you need. Since we are going to use the latest stable version v0.28.2 of Minikube, and the Kubernetes version it includes is v1.10, we will choose the corresponding 1.10 version of
kubectl
. Of course, we can also create a cluster with a different version by passing parameters. For example,minikube start --kubernetes-version v1.11.3
will create a cluster with versionv1.11.3
, and the version ofkubectl
needs to be upgraded accordingly.
Click Client Binaries to find the corresponding package download for your desired system architecture. Here, I will use the example of the package for 64-bit Linux.
➜ wget https://dl.k8s.io/v1.10.7/kubernetes-client-linux-amd64.tar.gz
➜ echo '169b57c6707ed8d8be9643b0088631e5c0c6a37a5e99205f03c1199cd32bc61e kubernetes-client-linux-amd64.tar.gz' | sha256sum -c -
kubernetes-client-linux-amd64.tar.gz: Success
➜ tar zxf kubernetes-client-linux-amd64.tar.gz
➜ sudo mv kubernetes/client/bin/kubectl /usr/local/bin/kubectl
➜ /usr/local/bin/kubectl version --client
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.7", GitCommit:"0c38c362511b20a098d7cd855f1314dad92c2780", GitTreeState:"clean", BuildDate:"2018-08-20T10:09:03Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Execute the above command to complete the installation of kubectl
, and the current installed version information will be displayed in the last step.
Installing Minikube #
First, check the Release page of Minikube. The current latest stable version is v0.28.2. Find the version for your desired system, click to download, and add the downloaded executable file to your PATH.
Note: The installation package for the current Windows system is still experimental. If you are using Windows, you can download and install it.
Take the Linux installation as an example:
➜ wget -O minikube https://github.com/kubernetes/minikube/releases/download/v0.28.2/minikube-linux-amd64
➜ chmod +x minikube
➜ sudo mv minikube /usr/local/bin/minikube
➜ /usr/local/bin/minikube version
minikube version: v0.28.2
The last step displays the current installed version information of Minikube. If the installation is successful, you will see the same result as the content above.
Creating the First K8S Cluster #
To create a cluster using Minikube, simply execute minikube start
. Under normal circumstances, you will see output similar to mine.
➜ ~ minikube start
Starting local Kubernetes v1.10.0 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.
Loading cached images from config file.
➜ ~ minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100
To verify that our cluster is configured correctly, you can run the following command.
➜ ~ kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use ‘kubectl cluster-info dump’.
➜ ~ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 1d v1.10.0
If you receive any errors such as connection refused, it may be because your kubectl
configuration is incorrect. You can check the $HOME/.kube/config
file for the configuration. Here is an example output.
➜ ~ cat .kube/config
apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/tao/.minikube/ca.crt
server: https://192.168.99.100:8443
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
client-certificate: /home/tao/.minikube/client.crt
client-key: /home/tao/.minikube/client.key
If you don’t have this file, you can create it using the example content above, replacing the paths and server
address configuration. You can obtain the server
address by running minikube status
or minikube ip
.
(Tao) ➜ ~ minikube ip
192.168.99.100
(Tao) ➜ ~ minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100
Viewing Cluster Status through the Dashboard #
One of the advantages of using Minikube
is that you don’t have to worry too much about the installation process. You can simply enter minikube dashboard
in the terminal to open the system dashboard and view the cluster status from there.
Running minikube dashboard
will automatically open the browser, listening on port 3000 of the IP obtained through minikube ip
by default. The image below shows an example:
Related links: #
Summary #
In this section, in order to quickly experience the K8S cluster and avoid many complicated installation steps, we chose to use the officially provided Minikube
tool to build a local cluster.
The essence of Minikube is to package a “customized” K8S cluster as an ISO image. When executing minikube start
, it starts a virtual machine through this image and builds a K8S cluster with only one node using the kubeadm
tool on this virtual machine. We will explain the kubeadm
tool in the next section.
At the same time, the address information of the recently launched virtual machine and other related configuration interfaces will be obtained through the virtual machine, and the configuration of the local kubectl
tool will be completed, so that users can operate the cluster using the kubectl
tool.
In fact, Docker for Mac 17.12 CE Edge, Docker for Windows 18.02 CE Edge, and higher versions of these two platforms have built-in support for K8S, but they are all Edge versions. We will not go into too much detail here.