Rapid deployment of k8s based on Rancher2

What is Rancher?

First post the description on the official website of Rancher: What is Rancher

Simply put, Rancher is a k8s management tool, similar to command-line tools like kubectl. With Rancher, we can use the GUI to operate and manage the k8s cluster more concisely and intuitively, and manage various resources of k8s on the k8s cluster.

Rancher installation

Since Rancher is installed in a learning environment, I chose to use Docker to install Rancher . The advantage of this is that Docker can be used to isolate the system environment from the Rancher environment. If you no longer want to use Rancher or are afraid that Rancher will destroy the host environment, you can also You can directly rm off the Rancher container on Docker to directly delete the Rancher environment.

According to the official website of Rancher, the installation of Rancher only needs one line of Docker container creation commands. Here, ports 80 and 443 are reserved for nginx, so when mapping docker ports, map Rancher’s 80 and 443 to ports 8090 and 8091
docker run -d --privileged -p 8090:80 -p 8091:443 --name rancher rancher/rancher:latest
. Just run Rancher in docker, and Rancher also comes with k3s, so open https://localhost:8091 with a browser and after registering users and logging in, you will see a local k3s cluster

Use Rancher to import existing k8s clusters

In the previous article # Deploy your own application to k8s step by step In the minikube, a multinode cluster of three nodes has been created, and Rancher supports multi-cluster management, so you can use Rancher to create the previously created The cluster is imported.

Modify server-url

First, go to Rancher's global settings-advanced settings in the browser, and find the server-url item here. The default value is https://localhost:8091. This is because Rancher, which runs with docker, uses port mapping. The ip is the host's, and Rancher's port 443 is mapped to the host's port 8091 when port mapping is created. This step modification is mainly to be able to access the server-url in the k8s cluster. If it is the default value, use localhost to access the cluster node’s own ip, so here you need to change localhost to the host’s ip (you can Use ifconfig to view, if the host reports an error and cannot find the ifconfig command, you can use sudo apt install net-tools to install)

Import an existing cluster

Go to the management cluster in Rancher and click the "Import Existing Cluster" button, then select "Import Existing Cluster", fill in the cluster name, and click the Create button. At this time, you will enter the cluster status interface that has been created. At this time, the cluster status is displayed as "Pending", indicating that Rancher has not yet connected to the real k8s cluster. Follow Rancher's registration prompt and enter the command on the command line

kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user <your username from your kubeconfig>

curl --insecure -sfL https://192.168.210.128:8091/v3/import/zn8kt5n9mxv7rqx587jzmzxwsgkvwsk9s26w89w2bklcjvl2jqkjls_c-m-l6dsxlbq.yaml | kubectl apply -f -
复制代码

Here you need to replace "" in the command with minikube, so the default username for minikube to create a cluster is minikube, which can be found in the ~/.kube/config file.

用kubectl加-all-namespaces参数查看pod和service,可以发现多了个"cattle-system"的namespace,当这个namespace下的pod和service都正常run起来后,再回到Rancher上会发现导入的集群已经变成"Active"状态了,点击导入的集群,也能看到上一篇文章# 将自己的应用程序部署到k8s上step by step中的三个node以及创建的show_pod_name的Deployment和Service。

使用Rancher进行部署

在之前使用kubectl用命令行部署show_pod_name时,虽然命令不算多,或者用yaml进行部署时,可以直接套用模板,但还是不够直观,Rancher提供了UI界面,也能直接进行部署

再部署一下show_pod_name

Select the multinode cluster we imported in Rancher's browsing cluster, select the "show_deployment" that has been deployed in the Deployment in the workload, then we will create another Deployment. Click the Create button, enter the name "show-pod-name", enter "192.168.210.128:5000/show_pod_name:latest" in the container image, and then select the node port in the port mapping, fill in 8080 in the container port, and in the listening port Fill in a number greater than 32000, and finally click the Create button at the bottom to create a Deployment called show-pod-name, and you can also find an additional "show-pod-name-nodeport" in the Services of service discovery ". This is because the node port is selected when adding port mapping. If you choose not to create a service, there will be no additional Service. You need to add a pod label when creating the Demployment, and manually create a Service, and fill in the pod in the selector Label.

Going back to the command line, you minikube service listcan find that there is an additional "show-pod-name-nodeport", and the access port is the number filled in the listening port before. Use the browser to access the url, and you can get the one created with the command line before. deployment with the same result. Use kubectl to view pod, service, and deployment, and you can also see the corresponding results of show-pod-name, which shows that Rancher successfully helped us implement the deployment operation of the command line.

Guess you like

Origin juejin.im/post/7219885609078439995