K8S hosting Rancher cluster

Background

Rancher may have some problems that make the UI interface inaccessible, and there is no way to deploy or update the service. In this case, we use the cluster’s kubeconfig file and use k8s to host the cluster. Even if the UI interface is inaccessible, we can deploy services, restart services, and other operations.

1. Backup the kubeconfig file of the cluster (important)

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-38enrYmL-1684412304437)(null)]

Click the kubeconfig file to copy it out and save it.

Note: Be sure to back up when the Rancher UI is accessed normally, otherwise it cannot be backed up when U crashes.

2. Install command line tools

2.1, kubernetes source configuration

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

2.2 Install kubelet and kubectl

yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet

2.3 Put the cluster files that need to be operated into the specified directory
2.3.1 Create a directory

mkdir -p $HOME/.kube

2.3.2 Moving files

The /tmp/admin.conf file here is the kubeconfig file of the cluster backed up above

sudo cp -i /tmp/admin.conf $HOME/.kube/config

2.4 Log in to the virtual machine, start kubectl, and use it to interact with the kubernetes cluster

Use the –kubeconfig flag to specify it like so:

kubectl --kubeconfig $HOME/.kube/config get pods

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-yJkze0OI-1684412304584)(null)]

3. Update service

3.1 View the namespace of Pod in the current cluster
View a pod name, namespace and running node

kubectl get pod -A -o yaml |grep '^ n'|grep -v nodeSelector

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-ACHt6vWz-1684412304491)(null)]

View the namespace through Deployment corresponding to Pod

kubectl get deployment -A

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-C1lZASek-1684412304677)(null)]

Choose one of the above two methods.

3.2 View Deployment under a namespace

kubectl --kubeconfig $HOME/.kube/config get deployment -n default

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-naU5f5I8-1684412304527)(null)]

3.3 Save the yaml file of this Deployment

kubectl --kubeconfig $HOME/.kube/config get deployment mysql-5-7-34-binlog -n default -o yaml >> mysql.yaml

3.4 Modify the yaml file to update the service, for example, we change the mirror version used by the service

Modify what is shown in the diagram:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-1hGh0kRA-1684412304606)(null)]

The content shown in the following figure needs to be deleted:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-s2bHOjGy-1684412304391)(null)]

3.5 Execute the following command to update the service

kubectl apply -f mysql.yaml

result:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-J1m7qRYH-1684412304357)(null)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-d3NMZIu6-1684412304508)(null)]

Question:
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-nfLf2L9i-1684412304660)(null)]

This problem occurs because the configuration file is time-stamped, and the latest yaml file needs to be used to re-execute the command.

4. Deployment service

4.1 Prepare the yaml files required for deploying services

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-NeKRjb7X-1684412304416)(null)]

4.2 Execute command deployment service

kubectl apply -f base.yaml

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-iZ71aWdw-1684412304559)(null)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-PHR4mRtV-1684412304719)(null)]

5. Common commands

5.1 Get all Pod in the current cluster

kubectl --kubeconfig $HOME/.kube/config get pods

5.2 Query the namespace of Pod
View a pod name, namespace and running node

kubectl get pod -A -o yaml |grep '^ n'|grep -v nodeSelector

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-b3wAHcxR-1684412304637)(null)]

5.3 Check the namespace through Deployment corresponding to Pod

kubectl get deployment -A

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload directly (img-Z2vVLt1X-1684412304461)(null)]

5.4 Get all Services in the current cluster

kubectl --kubeconfig $HOME/.kube/config get svc

5.5 Get all Pod in namespace mysql-clz

kubectl --kubeconfig $HOME/.kube/config get pods -n mysql-clz

5.6 View the description information of Pod

kubectl --kubeconfig $HOME/.kube/config describe pod mysql-clz-78b8cf6-nlwn6 -n mysql-clz

5.6 View yaml file of Pod

kubectl --kubeconfig $HOME/.kube/config get deployment mysql-clz-78b8cf6-nlwn6 -n mysql-clz -o yaml
kubectl --kubeconfig $HOME/.kube/config get mysql-clz-78b8cf6-nlwn6 -o yaml

5.8 View Deployment in namespace mysql-clz

kubectl --kubeconfig $HOME/.kube/config get deployment -n mysql-clz

5.9 View yaml file of Deployment

kubectl --kubeconfig $HOME/.kube/config get deployment mysql-clz -n mysql-clz -o yaml

5.10 Use yaml file to deploy services

kubectl --kubeconfig $HOME/.kube/config apply -f base.yaml