Build a k8s environment based on minikube from 0

1. Prepare a virtual machine environment

Personally I use Vitual Box

The Linux system uses Centos7

2. Install Linux on a virtual machine

Just choose the minimum content to install, I personally like pure and pure

3. Install Docker

refer to this article

https://blog.csdn.net/jll126/article/details/123113823

Just go from start to finish

The docker version I chose here is the same as in the article, which is 18.09.5

4. Install K8S environment

4.1 Abandon kubeadm

I originally tried to install the k8s cluster based on kubeadm, and after the installation is complete, the k8s-master node can start normally, but during observation, I found that the cpu will be super high every time it starts (virtual machine 2c4g environment). Once the k8s-node node is started, the CPU will not be 100% directly. After trying to increase the virtual machine configuration to 4c8g, there is no obvious improvement. I didn’t determine the specific reason, so I gave up trying kubeadm decisively. Anyway, it was just for learning, and I directly used minikube, a lightweight way, to build a k8s cluster.

4.2 Use minikube to build k8s

  1. First add the yum source of kubernetes, and execute the following command in the virtual machine

cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
enabled=1
EOF
  1. Install the kubectl library, kubectl is an official k8s CLI tool library.

yum install -y kubectl-1.21.2
  1. Download and install minikube

curl -LO https://storage.googleapis.com/minikube/releases/v1.22.0/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
  1. Start minikube, stepped on a lot of pits here, and record them in turn at the end of the article

minikube start --image-mirror-country='cn'

If you don’t add –image-mirror-country=’cn’, the default is to take the foreign mirror, and the speed is very slow

If you use the root user to start directly, you need to add –force after the command (not a good habit, but we are just for learning, so it doesn’t matter)

  1. When restarting minikube later, just use minikube start directly, without adding the following –image-mirror-country=’cn’ parameter

  1. Use the minikube dashboard –url=false command to view the k8s console

The url printed below is the page of the console, but at this time we cannot directly access the console in the browser on the host computer, we need to set it up and release the port in the virtual machine.

# port is the port number returned after the above dashboard command address is the ip address of your own virtual machine
nohup kubectl proxy --port=36123 --address='192.168.0.107' --accept-hosts='^.*' > ./nohup.out & amp;

Then you can directly access the k8s console in the windows browser, and you can play happily

5. The pit you stepped on when starting minikube

  1. Warn at startup

The image ‘registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0’ was not found; unable to add it to cache.

This kind of error of not being able to find the mirror will be encountered frequently later, but it will not affect the normal startup of minikube. We will solve it after the startup is completed.

All the problems of not finding the mirror can be solved by manually pulling the mirror. For example, the registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0 mirror cannot be found here.

We can go to docker’s official warehouse (https://hub.docker.com/) to see if there is a coredns/coredns image, and here we find some, and then we use docker pull coredns/coredns:1.8 directly in the virtual machine. 0 command to pull the official image.

Then use the docker tag coredns/coredns:1.8.0 registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0 command to change the image name to the image name that just prompted that it could not be found.

  1. In the minikube dashboard, the system is stuck when verifying the health of the proxy

Using the kubectl get pod -A command, it is found that several images have not started successfully

Use kubectl describe pod storage-provisioner -n kube-system to see the specific reason

It was found that the k8s-minikube/storage-provisioner:v5 image was not pulled down. Similarly, we went to the official website of dockerhub to find out if there is such an image. There is no official image here, but it seems that there are images uploaded by users themselves.

Here, because an error is reported inside minikube, we need to use the minikube ssh command to enter the internal environment of minikube, and then pull the image.

minikube ssh
docker pull kedu0105/storage-provisioner:v5
docker tag kedu0105/storage-provisioner:v5 registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-minikube/storage-provisioner:v5
exit

Similarly, the other two pods lack registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/metrics-scraper:v1.0.4 and registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/dashboard:v2. 1.0 image, we use the same method to manually pull

minikube ssh
docker pull kubernetesui/metrics-scraper:v1.0.4
docker tag kubernetesui/metrics-scraper:v1.0.4 registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/metrics-scraper:v1.0.4
docker pull kubernetesui/dashboard:v2.1.0
docker tag kubernetesui/dashboard:v2.1.0 registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetesui/dashboard:v2.1.0
exit

At this time, execute the kubectl get pod -A command again, and you will find that all pods have started normally (if there are any pods that have not been started, manually delete the pod and let him rebuild himself)

you’re done