[关闭]
@zhangyy 2020-12-28T13:28:43.000000Z 字数 6310 阅读 170

kubernetes 1.18.x 高可用二进制部署

kubernetes系列


  • 一: kubernetes 高可用简介
  • 二: kubernetes 高可用部署

一: kubernetes 高可用简介

1.1 kubernetes 高可用介绍

  1. 高可用架构(扩容多Master架构)
  2. Kubernetes作为容器集群系统,通过健康检查+重启策略实现了Pod故障自我修复能力,通过调度算法实现将Pod分布式部署,并保持预期副本数,根据Node失效状态自动在其他Node拉起Pod,实现了应用层的高可用性。
  3. 针对Kubernetes集群,高可用性还应包含以下两个层面的考虑:Etcd数据库的高可用性和Kubernetes Master组件的高可用性。而Etcd我们已经采用3个节点组建集群实现高可用,本节将对Master节点高可用进行说明和实施。
  4. Master节点扮演着总控中心的角色,通过不断与工作节点上的Kubelet进行通信来维护整个集群的健康工作状态。如果Master节点故障,将无法使用kubectl工具或者API做任何集群管理。
  5. Master节点主要有三个服务kube-apiserverkube-controller-mansgerkube-scheduler,其中kube-controller-mansgerkube-scheduler组件自身通过选择机制已经实现了高可用,所以Master高可用主要针对kube-apiserver组件,而该组件是以HTTP API提供服务,因此对他高可用与Web服务器类似,增加负载均衡器对其负载均衡即可,并且可水平扩容。

1.2 部署结构图

image_1eg26cjs6bev1pk8ioekrg13m69.png-397.7kB

1.3 多master 结构图

image_1eg26eoql1es9sq7r481sh61l4sm.png-1393.7kB


二:部署承接上文

  1. node04.flyfish 节点上面部署docker
  2. 2.1 解压二进制包
  3. tar zxvf docker-19.03.9.tgz
  4. mv docker/* /usr/bin

image_1eg26nm471e421q9bqt91vphs8p9.png-74.4kB

image_1eg26o800nuu5nfjoh1r0a113qm.png-43.6kB

  1. 2.2 systemd管理docker
  2. cat > /usr/lib/systemd/system/docker.service << EOF
  3. [Unit]
  4. Description=Docker Application Container Engine
  5. Documentation=https://docs.docker.com
  6. After=network-online.target firewalld.service
  7. Wants=network-online.target
  8. [Service]
  9. Type=notify
  10. ExecStart=/usr/bin/dockerd
  11. ExecReload=/bin/kill -s HUP $MAINPID
  12. LimitNOFILE=infinity
  13. LimitNPROC=infinity
  14. LimitCORE=infinity
  15. TimeoutStartSec=0
  16. Delegate=yes
  17. KillMode=process
  18. Restart=on-failure
  19. StartLimitBurst=3
  20. StartLimitInterval=60s
  21. [Install]
  22. WantedBy=multi-user.target
  23. EOF
  1. 2.3 创建配置文件
  2. mkdir /etc/docker
  3. cat > /etc/docker/daemon.json << EOF
  4. {
  5. "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
  6. }
  7. EOF
  8. registry-mirrors 阿里云镜像加速器

image_1eg26pmuj15f7161ajdn163vlfi13.png-188.2kB

  1. 2.4 启动并设置开机启动
  2. systemctl daemon-reload
  3. systemctl start docker
  4. systemctl enable docker

image_1eg26q3j216m511bp16khurj1rit1g.png-107.9kB


  1. 部署Master2 Node192.168.100.14
  2. Master2 与已部署的Master1所有操作一致。所以我们只需将Master1所有K8s文件拷贝过来,再修改下服务器IP和主机名启动即可。
  3. 1. 创建etcd证书目录
  4. Master2创建etcd证书目录:
  5. mkdir -p /opt/etcd/ssl

image_1eg274r7ig4olnt1pn81gc85f1t.png-25.4kB

  1. 2. 拷贝文件(Master1操作)
  2. 拷贝Master1上所有K8s文件和etcd证书到Master2
  3. scp -r /opt/kubernetes root@192.168.100.14:/opt
  4. scp -r /opt/cni/ root@192.168.100.14:/opt
  5. scp -r /opt/etcd/ssl root@192.168.100.14:/opt/etcd
  6. scp /usr/lib/systemd/system/kube* root@192.168.100.14:/usr/lib/systemd/system
  7. scp /usr/bin/kubectl root@192.168.100.14:/usr/bin

image_1eg275qf71r3tltc1t7nalebh82a.png-218.8kB

  1. 3. 删除证书文件
  2. 删除kubelet证书和kubeconfig文件:
  3. rm -f /opt/kubernetes/cfg/kubelet.kubeconfig
  4. rm -f /opt/kubernetes/ssl/kubelet*

image_1eg276cac1mm41tsvonf86i18bj2n.png-54.5kB


  1. 4. 修改配置文件IP和主机名
  2. 修改apiserverkubeletkube-proxy配置文件为本地IP
  3. vim /opt/kubernetes/cfg/kube-apiserver.conf
  4. ...
  5. --bind-address=192.168.100.14 \
  6. --advertise-address=192.168.100.14 \
  7. ...
  8. vim /opt/kubernetes/cfg/kubelet.conf
  9. --hostname-override=node04.flyfish
  10. vim /opt/kubernetes/cfg/kube-proxy-config.yml
  11. hostnameOverride: node04.flyfish

image_1eg27aj5g17ld15ss1u5e1nn6ddc34.png-184.5kB

image_1eg27f6nr19fa1bgiauf176m1t003h.png-112.2kB

image_1eg27g5ehqsaphssquok4133k3u.png-100.3kB


  1. 5. 启动设置开机启动
  2. systemctl daemon-reload
  3. systemctl start kube-apiserver
  4. systemctl start kube-controller-manager
  5. systemctl start kube-scheduler
  6. systemctl start kubelet
  7. systemctl start kube-proxy
  8. systemctl enable kube-apiserver
  9. systemctl enable kube-controller-manager
  10. systemctl enable kube-scheduler
  11. systemctl enable kubelet
  12. systemctl enable kube-proxy

image_1eg27hiq41hmjrmq1emds4kkb94b.png-258.5kB


  1. kubectl get cs

image_1eg27iuo4hqd1mpqq7m1o3h1eer4o.png-67.3kB


  1. 7. 批准kubelet证书申请
  2. node01.flyfish 节点 上面 批准授权
  3. kubectl get csr
  4. kubectl certificate approve node-csr-fyeyjxpS4JMpC2QvfmLOyeBbYUiMoYTSTGQETWVlqD4

image_1eg27l0c82vq1bt8rbq1n9ctbk55.png-129kB

image_1eg27oqg0oie1hd1kl6p108nb5i.png-86.4kB


  1. kubectl get node

image_1eg27qd7v1k3p1n05pve1u6b1j1u5v.png-73.9kB


三:部署nginx 负载均衡服务器

  1. kube-apiserver高可用架构图:

image_1eg2ajbbsknc16sk1bml1jm01j866c.png-143.3kB

  1. node05.flyfish node07.flyfish 上面部署 nginx keepalive
  2. 注意在 node06.flyfish 上面部署了vmware harbor
  3. yum install epel-release -y
  4. yum install nginx keepalived -y

  1. cat > /etc/nginx/nginx.conf << "EOF"
  2. user nginx;
  3. worker_processes auto;
  4. error_log /var/log/nginx/error.log;
  5. pid /run/nginx.pid;
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8. worker_connections 1024;
  9. }
  10. # 四层负载均衡,为两台Master apiserver组件提供负载均衡
  11. stream {
  12. log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
  13. access_log /var/log/nginx/k8s-access.log main;
  14. upstream k8s-apiserver {
  15. server 192.168.100.11:6443; # Master1 APISERVER IP:PORT
  16. server 192.168.100.14:6443; # Master2 APISERVER IP:PORT
  17. }
  18. server {
  19. listen 6443;
  20. proxy_pass k8s-apiserver;
  21. }
  22. }
  23. http {
  24. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  25. '$status $body_bytes_sent "$http_referer" '
  26. '"$http_user_agent" "$http_x_forwarded_for"';
  27. access_log /var/log/nginx/access.log main;
  28. sendfile on;
  29. tcp_nopush on;
  30. tcp_nodelay on;
  31. keepalive_timeout 65;
  32. types_hash_max_size 2048;
  33. include /etc/nginx/mime.types;
  34. default_type application/octet-stream;
  35. server {
  36. listen 80 default_server;
  37. server_name _;
  38. location / {
  39. }
  40. }
  41. }
  42. EOF

3. keepalived配置文件(Nginx Master)

  1. cat > /etc/keepalived/keepalived.conf << EOF
  2. global_defs {
  3. notification_email {
  4. acassen@firewall.loc
  5. failover@firewall.loc
  6. sysadmin@firewall.loc
  7. }
  8. notification_email_from Alexandre.Cassen@firewall.loc
  9. smtp_server 127.0.0.1
  10. smtp_connect_timeout 30
  11. router_id NGINX_MASTER
  12. }
  13. vrrp_script check_nginx {
  14. script "/etc/keepalived/check_nginx.sh"
  15. }
  16. vrrp_instance VI_1 {
  17. state MASTER
  18. interface ens33
  19. virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
  20. priority 100 # 优先级,备服务器设置 90
  21. advert_int 1 # 指定VRRP 心跳包通告间隔时间,默认1秒
  22. authentication {
  23. auth_type PASS
  24. auth_pass 1111
  25. }
  26. # 虚拟IP
  27. virtual_ipaddress {
  28. 192.168.100.100/24
  29. }
  30. track_script {
  31. check_nginx
  32. }
  33. }
  34. EOF

  1. vrrp_script:指定检查nginx工作状态脚本(根据nginx状态判断是否故障转移)
  2. virtual_ipaddress:虚拟IPVIP

  1. 检查nginx状态脚本:
  2. cat > /etc/keepalived/check_nginx.sh << "EOF"
  3. #!/bin/bash
  4. count=$(ps -ef |grep nginx |egrep -cv "grep|$$")
  5. if [ "$count" -eq 0 ];then
  6. exit 1
  7. else
  8. exit 0
  9. fi
  10. EOF
  11. chmod +x /etc/keepalived/check_nginx.sh

四:keepalived配置文件(Nginx Backup)

  1. cat > /etc/keepalived/keepalived.conf << EOF
  2. global_defs {
  3. notification_email {
  4. acassen@firewall.loc
  5. failover@firewall.loc
  6. sysadmin@firewall.loc
  7. }
  8. notification_email_from Alexandre.Cassen@firewall.loc
  9. smtp_server 127.0.0.1
  10. smtp_connect_timeout 30
  11. router_id NGINX_BACKUP
  12. }
  13. vrrp_script check_nginx {
  14. script "/etc/keepalived/check_nginx.sh"
  15. }
  16. vrrp_instance VI_1 {
  17. state BACKUP
  18. interface ens33
  19. virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
  20. priority 90
  21. advert_int 1
  22. authentication {
  23. auth_type PASS
  24. auth_pass 1111
  25. }
  26. virtual_ipaddress {
  27. 192.168.31.88/24
  28. }
  29. track_script {
  30. check_nginx
  31. }
  32. }
  33. EOF
  1. 上述配置文件中检查nginx运行状态脚本:
  2. cat > /etc/keepalived/check_nginx.sh << "EOF"
  3. #!/bin/bash
  4. count=$(ps -ef |grep nginx |egrep -cv "grep|$$")
  5. if [ "$count" -eq 0 ];then
  6. exit 1
  7. else
  8. exit 0
  9. fi
  10. EOF
  11. chmod +x /etc/keepalived/check_nginx.sh
  12. 注:keepalived根据脚本返回状态码(0为工作正常,非0不正常)判断是否故障转移。

  1. 5. 启动并设置开机启动
  2. systemctl daemon-reload
  3. systemctl start nginx
  4. systemctl start keepalived
  5. systemctl enable nginx
  6. systemctl enable keepalived

image_1eg2de1gah2s18tv1rqdl4g1ttl6p.png-156.8kB

image_1eg2deg3pn6hkg1clb1oag9vl76.png-183.6kB


  1. 6. 查看keepalived工作状态
  2. ip addr
  3. node05.flyfish 上面 有一个 虚拟VIP

image_1eg2djmdav50lega0n16rtjpb7j.png-139.3kB


  1. 7. Nginx+Keepalived高可用测试
  2. 关闭主节点Nginx,测试VIP是否漂移到备节点服务器。
  3. 杀掉node05.flyfish nginx
  4. pkill nginx
  5. 查看浮动IP 是否 飘到了node07.flyfish 节点

image_1eg2doohu1njnq351dkp1bn31c1j80.png-128.3kB

  1. 可以看到 浮动VIP 已经飘到了node07.flyfish 主机上面了

image_1eg2dpbrq58bi3q11hm10p3lj48d.png-131.6kB


  1. 去任意一个k8s 节点查看 服务器VIP 是否能够 获取到kube-apiserver 信息
  2. curl -k https://192.168.100.100:6443/version

image_1eg2duhts1jn4vrut8f12c515ap9a.png-83.9kB


  1. 检查 nignx 日志

image_1eg2e1u5m61ne4a57nm518ek9n.png-78.1kB

7.4 修改所有Worker Node连接LB VIP

  1. 虽然我们增加了Master2和负载均衡器,但是我们是从单Master架构扩容的,也就是说目前所有的Node组件连接都还是Master1,如果不改为连接VIP走负载均衡器,那么Master还是单点故障。
  2. 因此接下来就是要改所有Node组件配置文件,由原来192.168.100.11修改为192.168.100.100VIP):

image_1eg2e7qra1cv01soe1evd5p2s2fa4.png-82kB

  1. 所有node 节点执行命令
  2. sed -i 's#192.168.31.71:6443#192.168.31.88:6443#' /opt/kubernetes/cfg/*
  3. systemctl restart kubelet
  4. systemctl restart kube-proxy
  5. kubectl get node

image_1eg2ebjltk3v18o7ksl1l701jptah.png-222.2kB

  1. 至此 k8s 多节点master 集群配置完成
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注