K8s集群添加Master节点

635 阅读5分钟

生成添加Mater节点的命令

先执行添加node节点的命令

root@node1:/root# kubeadm token create --print-join-command
kubeadm join 192.168.202.151:6443 --token 9e27gy.jov2cblxelz085ux --discovery-token-ca-cert-hash sha256:5ee09d78e7ffba7d1f757b71035182e4c69bd51f07082e31b37d472cb9d540b9 

生成Key

root@node1:/root# kubeadm init phase upload-certs --upload-certs
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
83cabc28964ffc107fa7e1069ead5b9c0c8a3f5075ca195abeaad41c40ab0d13

将生成的添加node节点的命令和生成的key组合成添加master节点的命令

kubeadm join 192.168.202.151:6443 --token 9e27gy.jov2cblxelz085ux --discovery-token-ca-cert-hash sha256:5ee09d78e7ffba7d1f757b71035182e4c69bd51f07082e31b37d472cb9d540b9  --control-plane --certificate-key 83cabc28964ffc107fa7e1069ead5b9c0c8a3f5075ca195abeaad41c40ab0d13

## 说明
# --control-plane指定添加控制面
# --certificate-key 为前面生成的key

编辑kubeadm-config

root@node1:/root# kubectl edit cm kubeadm-config -n kube-system

添加如下内容: controlPlaneEndpoint: 192.168.202.151:6443

image.png

在新的节点执行添加控制面的命令

root@node5:/root# kubeadm join 192.168.202.151:6443 --token 9e27gy.jov2cblxelz085ux --discovery-token-ca-cert-hash sha256:5ee09d78e7ffba7d1f757b71035182e4c69bd51f07082e31b37d472cb9d540b9  --control-plane --certificate-key 83cabc28964ffc107fa7e1069ead5b9c0c8a3f5075ca195abeaad41c40ab0d13
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[preflight] Running pre-flight checks before initializing the new control plane instance
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
W1021 10:46:37.699685    5387 checks.go:835] detected that the sandbox image "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.9" of the container runtime is inconsistent with that used by kubeadm. It is recommended that using "registry.aliyuncs.com/google_containers/pause:3.9" as the CRI sandbox image.
[download-certs] Downloading the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[download-certs] Saving the certificates to the folder: "/etc/kubernetes/pki"
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost node5] and IPs [192.168.202.155 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost node5] and IPs [192.168.202.155 127.0.0.1 ::1]
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local node5] and IPs [10.96.0.1 192.168.202.155 192.168.202.151]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Valid certificates and keys now exist in "/etc/kubernetes/pki"
[certs] Using the existing "sa" key
[kubeconfig] Generating kubeconfig files
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[check-etcd] Checking that the etcd cluster is healthy
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
[etcd] Announced new etcd member joining to the existing etcd cluster
[etcd] Creating static Pod manifest for "etcd"
[etcd] Waiting for the new etcd member to join the cluster. This can take up to 40s
The 'update-status' phase is deprecated and will be removed in a future release. Currently it performs no operation
[mark-control-plane] Marking the node node5 as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node node5 as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]

This node has joined the cluster and a new control plane instance was created:

* Certificate signing request was sent to apiserver and approval was received.
* The Kubelet was informed of the new secure connection details.
* Control plane label and taint were applied to the new node.
* The Kubernetes control plane instances scaled up.
* A new etcd member was added to the local/stacked etcd cluster.

To start administering your cluster from this node, you need to run the following as a regular user:

        mkdir -p $HOME/.kube
        sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
        sudo chown $(id -u):$(id -g) $HOME/.kube/config

Run 'kubectl get nodes' to see this node join the cluster.

拷贝文件

        mkdir -p $HOME/.kube
        sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
        sudo chown $(id -u):$(id -g) $HOME/.kube/config

查看节点

可以看到有两个控制面了

root@node1:/home/tang# kubectl get nodes
NAME    STATUS   ROLES           AGE   VERSION
node1   Ready    control-plane   17m   v1.28.2
node2   Ready    <none>          16m   v1.28.2
node3   Ready    <none>          16m   v1.28.2
node4   Ready    <none>          16m   v1.28.2
node5   Ready    control-plane   65s   v1.28.2

排错

在新的节点执行添加控制面初始化的命令时会出现下面错误

root@node5:/root# kubeadm join 192.168.202.151:6443 --token 9e27gy.jov2cblxelz085ux --discovery-token-ca-cert-hash sha256:5ee09d78e7ffba7d1f757b71035182e4c69bd51f07082e31b37d472cb9d540b9  --control-plane --certificate-key 83cabc28964ffc107fa7e1069ead5b9c0c8a3f5075ca195abeaad41c40ab0d13
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
error execution phase preflight: 
One or more conditions for hosting a new control plane instance is not satisfied.

unable to add a new control plane instance to a cluster that doesn't have a stable controlPlaneEndpoint address

Please ensure that:
* The cluster has a stable controlPlaneEndpoint address.
* The certificates that must be shared among control plane instances are provided.


To see the stack trace of this error execute with --v=5 or higher

image.png

解决办法

编辑kubeadm-config,添加controlPlaneEndpoint: 192.168.202.151:6443

参考文档