if ! /usr/bin/killall -0 haproxy; then systemctl restart haproxy.service &> /dev/null if ! [ $? -eq 0 ]; then systemctl stop keepalived.service fi fi
1.3.2 部署 HAproxy
(1)在 HA1 上部署 HAproxy 服务,HA2 配置相同
1 2 3 4 5 6 7 8 9
root@k8s-ha1-238:~# apt -y install haproxy root@k8s-ha1-238:~# vim /etc/haproxy/haproxy.cfg listen k8s-api-6443 bind 10.243.20.250:6443 # 监听k8s-Master的6443端口 mode tcp server 10.243.20.230 10.243.20.230:6443 check inter 3s fall 3 rise 3 server 10.243.20.231 10.243.20.231:6443 check inter 3s fall 3 rise 3 server 10.243.20.232 10.243.20.232:6443 check inter 3s fall 3 rise 3 root@k8s-ha1-238:~# systemctl restart haproxy.service
root@k8s-ha2-deploy-239:~# ./ezdown -D # 下载完成 root@k8s-ha2-deploy-239:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE easzlab/kubeasz 3.6.1 0ae1e2a7c7f0 7 months ago 157MB easzlab/kubeasz-k8s-bin v1.27.2 a9d4ca76c91b 7 months ago 1.12GB calico/kube-controllers v3.24.6 baf4466ddf40 7 months ago 77.5MB easzlab.io.local:5000/calico/kube-controllers v3.24.6 baf4466ddf40 7 months ago 77.5MB easzlab.io.local:5000/calico/cni v3.24.6 ca9fea5e07cb 7 months ago 212MB calico/cni v3.24.6 ca9fea5e07cb 7 months ago 212MB calico/node v3.24.6 3953a481aa9d 7 months ago 245MB easzlab.io.local:5000/calico/node v3.24.6 3953a481aa9d 7 months ago 245MB easzlab/kubeasz-ext-bin 1.7.1 5c1895de99b2 9 months ago 606MB easzlab/metrics-server v0.6.3 1da5af8117da 9 months ago 68.9MB easzlab.io.local:5000/easzlab/metrics-server v0.6.3 1da5af8117da 9 months ago 68.9MB easzlab/k8s-dns-node-cache 1.22.20 d1157efdd316 10 months ago 67.8MB easzlab.io.local:5000/easzlab/k8s-dns-node-cache 1.22.20 d1157efdd316 10 months ago 67.8MB easzlab/pause 3.9 78d53e70b442 14 months ago 744kB easzlab.io.local:5000/easzlab/pause 3.9 78d53e70b442 14 months ago 744kB kubernetesui/dashboard v2.7.0 07655ddf2eeb 15 months ago 246MB easzlab.io.local:5000/kubernetesui/dashboard v2.7.0 07655ddf2eeb 15 months ago 246MB kubernetesui/metrics-scraper v1.0.8 115053965e86 19 months ago 43.8MB easzlab.io.local:5000/kubernetesui/metrics-scraper v1.0.8 115053965e86 19 months ago 43.8MB coredns/coredns 1.9.3 5185b96f0bec 19 months ago 48.8MB easzlab.io.local:5000/coredns/coredns 1.9.3 5185b96f0bec 19 months ago 48.8MB registry 2 b8604a3fe854 2 years ago 26.2MB root@k8s-ha2-deploy-239:~# ls /etc/kubeasz/ ansible.cfg bin docs down example ezctl ezdown manifests pics playbooks README.md roles tools
1.5.3 生成并自定义 hosts 文件
1 2 3 4 5 6 7
root@k8s-ha2-deploy-239:~# cd /etc/kubeasz/ root@k8s-ha2-deploy-239:/etc/kubeasz# ./ezctl new k8s-cluster1 # 新建一个容器 2024-01-02 14:05:59 DEBUG generate custom cluster files in /etc/kubeasz/clusters/k8s-cluster1 2024-01-02 14:05:59 DEBUG set versions 2024-01-02 14:05:59 DEBUG cluster k8s-cluster1: files successfully created. 2024-01-02 14:05:59 INFO next steps 1: to config '/etc/kubeasz/clusters/k8s-cluster1/hosts' 2024-01-02 14:05:59 INFO next steps 2: to config '/etc/kubeasz/clusters/k8s-cluster1/config.yml'
1.5.3.1 编辑生成的 Ansible hosts 文件
指定 etcd 节点、Master节点、node节点、VIP、运行时、网络组件类型、Service IP 与 pod IP 范围等配置信息。
root@k8s-ha2-deploy-239:/etc/kubeasz# vim clusters/k8s-cluster1/hosts root@k8s-ha2-deploy-239:/etc/kubeasz# cat clusters/k8s-cluster1/hosts # 'etcd' cluster should have odd member(s) (1,3,5,...) [etcd] 10.243.20.235 10.243.20.236 10.243.20.237
# master node(s), set unique 'k8s_nodename' for each node # CAUTION: 'k8s_nodename' must consist of lower case alphanumeric characters, '-' or '.', # and must start and end with an alphanumeric character [kube_master] 10.243.20.230 k8s_nodename='10.243.20.230' 10.243.20.231 k8s_nodename='10.243.20.231' 10.243.20.232 k8s_nodename='10.243.20.232'
# work node(s), set unique 'k8s_nodename' for each node # CAUTION: 'k8s_nodename' must consist of lower case alphanumeric characters, '-' or '.', # and must start and end with an alphanumeric character [kube_node] 10.243.20.240 k8s_nodename='10.243.20.240' 10.243.20.241 k8s_nodename='10.243.20.241' 10.243.20.242 k8s_nodename='10.243.20.242'
# [optional] harbor server, a private docker registry # 'NEW_INSTALL': 'true' to install a harbor server; 'false' to integrate with existed one [harbor] #192.168.1.8 NEW_INSTALL=false
# [optional] loadbalance for accessing k8s from outside [ex_lb] #192.168.1.6 LB_ROLE=backup EX_APISERVER_VIP=192.168.1.250 EX_APISERVER_PORT=8443 #192.168.1.7 LB_ROLE=master EX_APISERVER_VIP=192.168.1.250 EX_APISERVER_PORT=8443
# [optional] ntp server for the cluster [chrony] #192.168.1.1
[all:vars] # --------- Main Variables --------------- # Secure port for apiservers SECURE_PORT="6443"
# Cluster container-runtime supported: docker, containerd # if k8s version >= 1.24, docker is not supported CONTAINER_RUNTIME="containerd"
root@k8s-ha2-deploy-239:/etc/kubeasz# ./ezctl setup –help Usage: ezctl setup available steps: 01 prepare to prepare CA/certs & kubeconfig & other system settings 02 etcd to setup the etcd cluster 03 container-runtime to setup the container runtime(docker or containerd) 04 kube-master to setup the master nodes 05 kube-node to setup the worker nodes 06 network to setup the network plugin 07 cluster-addon to setup other useful plugins 90 all to run 01~07 all at once 10 ex-lb to install external loadbalance for accessing k8s from outside 11 harbor to install a new harbor server or to integrate with an existed one
# 在其中一个 etcd 服务器验证集群状态 root@k8s-etcd1-235:~# export NODE_IPS="10.243.20.235 10.243.20.236 10.243.20.237" root@k8s-etcd1-235:~# for IP in ${NODE_IPS};do ETCDCTL_API=3 /usr/local/bin/etcdctl --endpoints=https://${IP}:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem endpoint health;done https://10.243.20.235:2379 is healthy: successfully committed proposal: took = 11.440413ms https://10.243.20.236:2379 is healthy: successfully committed proposal: took = 11.130143ms https://10.243.20.237:2379 is healthy: successfully committed proposal: took = 12.942469ms
1 2 3
# 查看成员列表 root@k8s-etcd1-235:~# export NODE_IPS="10.243.20.235 10.243.20.236 10.243.20.237" root@k8s-etcd1-235:~# ETCDCTL_API=3 /usr/local/bin/etcdctl --write-out=table member list --endpoints=https://10.243.20.235:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem
1 2 3 4 5 6
# 验证节点心跳状态 root@k8s-etcd1-235:~# export NODE_IPS="10.243.20.235 10.243.20.236 10.243.20.237" root@k8s-etcd1-235:~# for ip in ${NODE_IPS}; do ETCDCTL_API=3 /usr/local/bin/etcdctl --endpoints=https://${ip}:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem endpoint health; done https://10.243.20.235:2379 is healthy: successfully committed proposal: took = 10.120964ms https://10.243.20.236:2379 is healthy: successfully committed proposal: took = 11.224057ms https://10.243.20.237:2379 is healthy: successfully committed proposal: took = 11.444497ms
1 2 3
# 查看 etcd 详细信息 root@k8s-etcd1-235:~# export NODE_IPS="10.243.20.235 10.243.20.236 10.243.20.237" root@k8s-etcd1-235:~# for ip in ${NODE_IPS}; do ETCDCTL_API=3 /usr/local/bin/etcdctl --write-out=table endpoint status --endpoints=https://${ip}:2379 --cacert=/etc/kubernetes/ssl/ca.pem --cert=/etc/kubernetes/ssl/etcd.pem --key=/etc/kubernetes/ssl/etcd-key.pem; done
root@k8s-node1-240:~# nerdctl login harbor.wuhaolam.top Enter Username: admin Enter Password: WARN[0003] skipping verifying HTTPS certs for"harbor.wuhaolam.top" WARNING: Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store
# 修改的地址需要与 pod 中域名地址相同 root@k8s-ha2-deploy-239:/etc/kubeasz# kubectl run net-test1 --image=alpine sleep 36000 root@k8s-ha2-deploy-239:/etc/kubeasz# kubectl get pod NAME READY STATUS RESTARTS AGE net-test1 1/1 Running 0 18s root@k8s-ha2-deploy-239:/etc/kubeasz# kubectl exec -it net-test1 sh kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. / # cat /etc/resolv.conf search default.svc.cluster.local svc.cluster.local cluster.local nameserver 10.100.0.2 options ndots:5
1.6.2 部署 CoreDNS 服务
(1)开始部署
1 2 3 4 5 6 7 8 9 10 11
root@k8s-ha2-deploy-239:~# kubectl apply -f coredns-v1.10.1.yaml serviceaccount/coredns created clusterrole.rbac.authorization.k8s.io/system:coredns created clusterrolebinding.rbac.authorization.k8s.io/system:coredns created configmap/coredns created deployment.apps/coredns created service/kube-dns created
root@k8s-ha2-deploy-239:/etc/kubeasz# kubectl exec -it net-test1 sh kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. / # ping www.baidu.com PING www.baidu.com (183.2.172.42): 56 data bytes 64 bytes from 183.2.172.42: seq=0 ttl=48 time=23.531 ms 64 bytes from 183.2.172.42: seq=1 ttl=48 time=23.447 ms ^C --- www.baidu.com ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 23.447/23.489/23.531 ms / # ping 223.6.6.6 -c 3 PING 223.6.6.6 (223.6.6.6): 56 data bytes 64 bytes from 223.6.6.6: seq=0 ttl=112 time=10.705 ms 64 bytes from 223.6.6.6: seq=1 ttl=112 time=10.654 ms 64 bytes from 223.6.6.6: seq=2 ttl=112 time=10.591 ms
--- 223.6.6.6 ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 10.591/10.650/10.705 ms