Let’s have a snack today. It doesn’t involve K8S. Rancher is a great way to deploy and manage a K8S cluster.
Install the K8S cluster using Rancher
Hosting plan
The host name | role | The IP address | Based on the software |
---|---|---|---|
k8s-rancher | Manage the K8S cluster | 192.168.242.109 | docker-ce |
k8s-master | K8s Primary node of the cluster | 192.168.242.108 | docker-ce |
k8s-worker-node01 | K8s secondary node 1 | 192.168.242.107 | docker-ce |
k8s-worker-node02 | K8s secondary node 2 | 192.168.242.106 | docker-ce |
❝
Host Name indicates the host on which the operation is performed.
❞
Docker-ce is installed on each machine, and the installation command is in
Container Technology Docker- From earth to Heaven
Already mentioned. It is also provided here:
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
systemctl enable docker
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://yours.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Copy the code
Install the rancher
On host K8S-Rancher, run the following command:
docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher
Copy the code
If you cannot access the http://192.168.242.109 rancher page, use docker ps to observe the “container running status” of docker rancher
Docker logs container ID
Check the log and find the following error message:
rancher must be ran with the --privileged flag when running outside of kubernetes
This is caused by the permission issue of Selinux. When launching the container, it is necessary to add –privileged, delete the container created just now and run it again:
docker run --privileged -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher
Copy the code
❝
If you find that the container restarts frequently after running for a while, you can increase the memory size. My VM was originally given 1 GB of memory, but the VM keeps restarting. After adjusting the memory size, the fault is rectified.
❞
Open the page:
The Rancher page can be displayed in Chinese, and the next step is to create a cluster with a click of the mouse.
Click Add Cluster to enter the page for creating a K8S cluster:
Click Customize:
Enter the cluster name, select the K8S version, and click Next to enter:
As shown below, operations are required on k8S-Master:
Now go to the Rancher management page, click Global, see the k8S cluster created just now:
Click cluster, enter, and click host:
You can see various states during the cluster deployment process. When the green Active state appears, it indicates that the Master node of the K8S cluster is successfully created:
Continue editing the cluster and copy the command below to add the K8S slave node:
Run the following commands on hosts K8s-worker-node01 and k8s-worker-node02:
sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher-agent:v2.5.3 --server https://192.168.242.109 --token 84dphjc9v2949f887gtfxwvk2fhnv7xjlqq84nqb2r565kqn7qnxdp --ca-checksum e4d1715ec0295d2e9e1fbc3bba3386529cdf4e8b1be9e5b8c3b48acb1bd679f3 --worker
Copy the code
It is important to note that the runtime is slow, and once the container is up, Rancher will need to perform various health checks on the cluster nodes. This process will depend on your machine’s CPU, memory, and network configuration.
Final cluster status:
The dashboard:
Project/namespace:
Click Add Project on this page and let’s add a project proA:
Entry project:
Deploying services under the proA project:
Enter the name of the deployment service, namespace, Docker image, port mapping rule, etc:
Click launch:
After a few seconds:
Click on the nginx container link:
The nginx page is opened directly:
Even load balance are all ready, visit http://192.168.242.106:30001/, http://192.168.242.107:30001/, http://192.168.242.108:30001/ are:
It’s really silky smooth! Snap! The nginx container is running, fast! Ma Baoguo straight call expert!!
Official Website Information
docs.rancher.cn/rancher2/