kubeadm安装集群系列-2.Master高可用

时间:2021-09-22 20:50:27

Master高可用安装

VIP负载均衡可以使用haproxy+keepalive实现,云上用户可以使用对应的ULB实现

准备kubeadm-init.yaml文件

 1 apiVersion: kubeadm.k8s.io/v1beta2
 2 bootstrapTokens:
 3 - groups:
 4   - system:bootstrappers:kubeadm:default-node-token
 5   token: k60p22.go0fadibgqm2xcx8
 6   ttl: 24h0m0s
 7   usages:
 8   - signing
 9   - authentication
10 kind: InitConfiguration
11 localAPIEndpoint:
12   advertiseAddress: 10.8.31.84
13   bindPort: 6443
14 nodeRegistration:
15   criSocket: /var/run/dockershim.sock
16   name: k8s-test-master-1
17   taints:
18   - effect: NoSchedule
19     key: node-role.kubernetes.io/master
20 ---
21 apiServer:
22   timeoutForControlPlane: 4m0s
23 apiVersion: kubeadm.k8s.io/v1beta2
24 certificatesDir: /etc/kubernetes/pki
25 clusterName: kubernetes-test
26 controllerManager: {}
27 dns:
28   type: CoreDNS
29 etcd:
30   local:
31     dataDir: /data/etcd
32 imageRepository: k8s.gcr.io
33 kind: ClusterConfiguration
34 kubernetesVersion: v1.15.1
35 controlPlaneEndpoint: 10.8.28.200:6443
36 networking:
37   dnsDomain: cluster.local
38   podSubnet: 192.168.0.0/16
39   serviceSubnet: 10.96.0.0/12
40 scheduler: {}

 

kubeadm安装master-1

kubeadm init执行初始化

1 # 在master-1执行
2 kubeadm init --config kubeadm-init.yaml 

以下为输出

[init] Using Kubernetes version: v1.15.1
[preflight] Running pre-flight checks
    [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[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'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-test-master-1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.8.31.84 10.8.28.200]
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[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 [k8s-test-master-1 localhost] and IPs [10.8.31.84 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-test-master-1 localhost] and IPs [10.8.31.84 127.0.0.1 ::1]
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.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"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 39.002458 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-test-master-1 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-test-master-1 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[kubelet-check] Initial timeout of 40s passed.
[bootstrap-token] Using token: k60p22.go0fadibgqm2xcx8
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, 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

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join 10.8.28.200:6443 --token k60p22.go0fadibgqm2xcx8 \
    --discovery-token-ca-cert-hash sha256:ffd76aed49c8f52dea15d16132897376176aea4c3ee50370e9369ca6c6c5a6b0 \
    --control-plane

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 10.8.28.200:6443 --token k60p22.go0fadibgqm2xcx8 \
    --discovery-token-ca-cert-hash sha256:ffd76aed49c8f52dea15d16132897376176aea4c3ee50370e9369ca6c6c5a6b0

 

配置kubeconfig

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

 

通过kubectl查询当前集群状态

1 [root@k8s-test-master-1 k8s-init]# kubectl get cs
2 NAME                 STATUS    MESSAGE             ERROR
3 controller-manager   Healthy   ok
4 scheduler            Healthy   ok
5 etcd-0               Healthy   {"health":"true"}
6 [root@k8s-test-master-1 k8s-init]# kubectl get nodes
7 NAME                STATUS     ROLES    AGE   VERSION
8 k8s-test-master-1   NotReady   master   20m   v1.15.1
目前只有一个节点,角色是Master,状态是NotReady,状态是NotReady状态是因为还没有安装网络插件。
 

配置其他Master节点

配置证书

拷贝证书至master-2,3节点
 
 1 USER=root
 2 CONTROL_PLANE_IPS="k8s-test-master-2 k8s-test-master-3"
 3 for host in ${CONTROL_PLANE_IPS}; do
 4     ssh -p 8022 "${USER}"@$host "mkdir -p /etc/kubernetes/pki/etcd"
 5     scp -P 8022 /etc/kubernetes/pki/ca.* "${USER}"@$host:/etc/kubernetes/pki/
 6     scp -P 8022 /etc/kubernetes/pki/sa.* "${USER}"@$host:/etc/kubernetes/pki/
 7     scp -P 8022 /etc/kubernetes/pki/front-proxy-ca.* "${USER}"@$host:/etc/kubernetes/pki/
 8     scp -P 8022 /etc/kubernetes/pki/etcd/ca.* "${USER}"@$host:/etc/kubernetes/pki/etcd/
 9     scp -P 8022 /etc/kubernetes/admin.conf "${USER}"@$host:/etc/kubernetes/
10 done

 

执行加入集群

在master2上操作,EndPoint为VIP地址

1 kubeadm join 10.8.28.200:6443 --token k60p22.go0fadibgqm2xcx8 \
2   --discovery-token-ca-cert-hash sha256:ffd76aed49c8f52dea15d16132897376176aea4c3ee50370e9369ca6c6c5a6b0 \
3   --control-plane \
4   --node-name k8s-test-master-2

以下为输出

[preflight] Running pre-flight checks
    [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[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'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-test-master-2 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.8.152.149 10.8.28.200]
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-test-master-2 localhost] and IPs [10.8.152.149 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-test-master-2 localhost] and IPs [10.8.152.149 127.0.0.1 ::1]
[certs] Generating "apiserver-etcd-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] Using existing kubeconfig file: "/etc/kubernetes/admin.conf"
[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] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
[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] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
[etcd] Announced new etcd member joining to the existing etcd cluster
[etcd] Wrote Static Pod manifest for a local etcd member to "/etc/kubernetes/manifests/etcd.yaml"
[etcd] Waiting for the new etcd member to join the cluster. This can take up to 40s
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[mark-control-plane] Marking the node k8s-test-master-2 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-test-master-2 as control-plane by adding the taints [node-role.kubernetes.io/master: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 (master) 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.

master3同理

1 kubeadm join 10.8.28.200:6443 --token k60p22.go0fadibgqm2xcx8 \
2   --discovery-token-ca-cert-hash sha256:ffd76aed49c8f52dea15d16132897376176aea4c3ee50370e9369ca6c6c5a6b0 \
3   --control-plane \
4   --node-name k8s-test-master-3

 

检查master节点

[root@k8s-test-master-1 ~]# kubectl get nodes
NAME                STATUS     ROLES    AGE    VERSION
k8s-test-master-1   NotReady   master   32m    v1.15.1
k8s-test-master-2   NotReady   master   7m9s   v1.15.1
k8s-test-master-3   NotReady   master   2m4s   v1.15.1

 

配置网络

本例使用calico

其他网络可以在pod-network章节找到对应安装方法

1 kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml
 
以下为输出
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.apps/calico-node created
serviceaccount/calico-node created
deployment.apps/calico-kube-controllers created
serviceaccount/calico-kube-controllers created

 

kube-proxy开启ip_vs

修改ConfigMap的kube-system/kube-proxy中的config.conf,mode: "ipvs"
1 kubectl edit cm kube-proxy -n kube-system
2 ## configmap/kube-proxy edited

重启kube-proxy

1 kubectl get pod -n kube-system | grep kube-proxy | awk '{system("kubectl delete pod "$1" -n kube-system")}'
2 ## pod "kube-proxy-52clz" deleted
3 ## pod "kube-proxy-6d7vr" deleted
4 ## pod "kube-proxy-vm6zf" deleted
5 ## pod "kube-proxy-zgtgk" deleted
查看是否开启了ipvs(出现Using ipvs Proxier)
 1 kubectl logs kube-proxy-6q54d -n kube-system
 2 ## I0729 08:14:09.286574       1 server_others.go:170] Using ipvs Proxier.
 3 ## W0729 08:14:09.286940       1 proxier.go:401] IPVS scheduler not specified, use rr by default
 4 ## I0729 08:14:09.288637       1 server.go:534] Version: v1.15.1
 5 ## I0729 08:14:09.295887       1 conntrack.go:52] Setting nf_conntrack_max to 131072
 6 ## I0729 08:14:09.296683       1 config.go:187] Starting service config controller
 7 ## I0729 08:14:09.296711       1 controller_utils.go:1029] Waiting for caches to sync for service config controller
 8 ## I0729 08:14:09.296729       1 config.go:96] Starting endpoints config controller
 9 ## I0729 08:14:09.296737       1 controller_utils.go:1029] Waiting for caches to sync for endpoints config controller
10 ## I0729 08:14:09.396922       1 controller_utils.go:1036] Caches are synced for service config controller
11 ## I0729 08:14:09.396937       1 controller_utils.go:1036] Caches are synced for endpoints config controller