部署一套完整的Kubernetes高可用叢集(二進位制,最新版v1.18)下

李振良的技術部落格發表於2020-09-09

七、高可用架構(擴容多Master架構)

Kubernetes作為容器叢集系統,通過健康檢查+重啟策略實現了Pod故障自我修復能力,通過排程演算法實現將Pod分散式部署,並保持預期副本數,根據Node失效狀態自動在其他Node拉起Pod,實現了應用層的高可用性。

針對Kubernetes叢集,高可用性還應包含以下兩個層面的考慮:Etcd資料庫的高可用性和Kubernetes Master元件的高可用性。 而Etcd我們已經採用3個節點組建叢集實現高可用,本節將對Master節點高可用進行說明和實施。

Master節點扮演著總控中心的角色,通過不斷與工作節點上的Kubelet和kube-proxy進行通訊來維護整個叢集的健康工作狀態。如果Master節點故障,將無法使用kubectl工具或者API做任何叢集管理。

Master節點主要有三個服務kube-apiserver、kube-controller-manager和kube-scheduler,其中kube-controller-manager和kube-scheduler元件自身通過選擇機制已經實現了高可用,所以Master高可用主要針對kube-apiserver元件,而該元件是以HTTP API提供服務,因此對他高可用與Web伺服器類似,增加負載均衡器對其負載均衡即可,並且可水平擴容。

多Master架構圖:

7.1 安裝Docker

同上,不再贅述。

7.2 部署Master2 Node(192.168.31.74)

Master2 與已部署的Master1所有操作一致。所以我們只需將Master1所有K8s檔案拷貝過來,再修改下伺服器IP和主機名啟動即可。

1. 建立etcd證書目錄

在Master2建立etcd證書目錄:

mkdir -p /opt/etcd/ssl

2. 拷貝檔案(Master1操作)

拷貝Master1上所有K8s檔案和etcd證書到Master2:

scp -r /opt/kubernetes root@192.168.31.74:/opt
scp -r /opt/cni/ root@192.168.31.74:/opt
scp -r /opt/etcd/ssl root@192.168.31.74:/opt/etcd
scp /usr/lib/systemd/system/kube* root@192.168.31.74:/usr/lib/systemd/system
scp /usr/bin/kubectl  root@192.168.31.74:/usr/bin

3. 刪除證書檔案

刪除kubelet證書和kubeconfig檔案:

rm -f /opt/kubernetes/cfg/kubelet.kubeconfig 
rm -f /opt/kubernetes/ssl/kubelet*

4. 修改配置檔案IP和主機名

修改apiserver、kubelet和kube-proxy配置檔案為本地IP:

vi /opt/kubernetes/cfg/kube-apiserver.conf 
...
--bind-address=192.168.31.74 \
--advertise-address=192.168.31.74 \
...

vi /opt/kubernetes/cfg/kubelet.conf
--hostname-override=k8s-master2

vi /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: k8s-master2

5. 啟動設定開機啟動

systemctl daemon-reload
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy
systemctl enable kube-apiserver
systemctl enable kube-controller-manager
systemctl enable kube-scheduler
systemctl enable kubelet
systemctl enable kube-proxy

6. 檢視叢集狀態

kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-1               Healthy   {"health":"true"}   
etcd-2               Healthy   {"health":"true"}   
etcd-0               Healthy   {"health":"true"}

7. 批准kubelet證書申請

kubectl get csr
NAME                                                   AGE   SIGNERNAME                                    REQUESTOR           CONDITION
node-csr-JYNknakEa_YpHz797oKaN-ZTk43nD51Zc9CJkBLcASU   85m   kubernetes.io/kube-apiserver-client-kubelet   kubelet-bootstrap   Pending

kubectl certificate approve node-csr-JYNknakEa_YpHz797oKaN-ZTk43nD51Zc9CJkBLcASU

kubectl get node
NAME          STATUS   ROLES    AGE   VERSION
k8s-master    Ready    <none>   34h   v1.18.3
k8s-master2   Ready    <none>   83m   v1.18.3
k8s-node1     Ready    <none>   33h   v1.18.3
k8s-node2     Ready    <none>   33h   v1.18.3

如果你在學習中遇到問題或者文件有誤可聯絡阿良~ 微信: init1024

7.3 部署Nginx負載均衡器

kube-apiserver高可用架構圖:

  • Nginx是一個主流Web服務和反向代理伺服器,這裡用四層實現對apiserver實現負載均衡。
  • Keepalived是一個主流高可用軟體,基於VIP繫結實現伺服器雙機熱備,在上述拓撲中,Keepalived主要根據Nginx執行狀態判斷是否需要故障轉移(偏移VIP),例如當Nginx主節點掛掉,VIP會自動繫結在Nginx備節點,從而保證VIP一直可用,實現Nginx高可用。

1. 安裝軟體包(主/備)

 yum install epel-release -y
 yum install nginx keepalived -y

2. Nginx配置檔案(主/備一樣)

cat > /etc/nginx/nginx.conf << "EOF"
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

# 四層負載均衡,為兩臺Master apiserver元件提供負載均衡
stream {

    log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';

    access_log  /var/log/nginx/k8s-access.log  main;

    upstream k8s-apiserver {
       server 192.168.31.71:6443;   # Master1 APISERVER IP:PORT
       server 192.168.31.74:6443;   # Master2 APISERVER IP:PORT
    }
    
    server {
       listen 6443;
       proxy_pass k8s-apiserver;
    }
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    server {
        listen       80 default_server;
        server_name  _;

        location / {
        }
    }
}
EOF

3. keepalived配置檔案(Nginx Master)

cat > /etc/keepalived/keepalived.conf << EOF
global_defs { 
   notification_email { 
     acassen@firewall.loc 
     failover@firewall.loc 
     sysadmin@firewall.loc 
   } 
   notification_email_from Alexandre.Cassen@firewall.loc  
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id NGINX_MASTER
} 

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state MASTER 
    interface ens33  # 修改為實際網路卡名
    virtual_router_id 51 # VRRP 路由 ID例項,每個例項是唯一的 
    priority 100    # 優先順序,備伺服器設定 90 
    advert_int 1    # 指定VRRP 心跳包通告間隔時間,預設1秒 
    authentication { 
        auth_type PASS      
        auth_pass 1111 
    }  
    # 虛擬IP
    virtual_ipaddress { 
        192.168.31.88/24
    } 
    track_script {
        check_nginx
    } 
}
EOF
  • vrrp_script:指定檢查nginx工作狀態指令碼(根據nginx狀態判斷是否故障轉移)

  • virtual_ipaddress:虛擬IP(VIP)

檢查nginx狀態指令碼:

cat > /etc/keepalived/check_nginx.sh  << "EOF"
#!/bin/bash
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    exit 1
else
    exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh

4. keepalived配置檔案(Nginx Backup)

cat > /etc/keepalived/keepalived.conf << EOF
global_defs { 
   notification_email { 
     acassen@firewall.loc 
     failover@firewall.loc 
     sysadmin@firewall.loc 
   } 
   notification_email_from Alexandre.Cassen@firewall.loc  
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id NGINX_BACKUP
} 

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state BACKUP 
    interface ens33
    virtual_router_id 51 # VRRP 路由 ID例項,每個例項是唯一的 
    priority 90
    advert_int 1
    authentication { 
        auth_type PASS      
        auth_pass 1111 
    }  
    virtual_ipaddress { 
        192.168.31.88/24
    } 
    track_script {
        check_nginx
    } 
}
EOF

上述配置檔案中檢查nginx執行狀態指令碼:

cat > /etc/keepalived/check_nginx.sh  << "EOF"
#!/bin/bash
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    exit 1
else
    exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh

注:keepalived根據指令碼返回狀態碼(0為工作正常,非0不正常)判斷是否故障轉移。

5. 啟動並設定開機啟動

systemctl daemon-reload
systemctl start nginx
systemctl start keepalived
systemctl enable nginx
systemctl enable keepalived

6. 檢視keepalived工作狀態

ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:04:f7:2c brd ff:ff:ff:ff:ff:ff
    inet 192.168.31.80/24 brd 192.168.31.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.31.88/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe04:f72c/64 scope link 
       valid_lft forever preferred_lft forever

可以看到,在ens33網路卡繫結了192.168.31.88 虛擬IP,說明工作正常。

7. Nginx+Keepalived高可用測試

關閉主節點Nginx,測試VIP是否漂移到備節點伺服器。

在Nginx Master執行 pkill nginx
在Nginx Backup,ip addr命令檢視已成功繫結VIP。

8. 訪問負載均衡器測試

找K8s叢集中任意一個節點,使用curl檢視K8s版本測試,使用VIP訪問:

curl -k https://192.168.31.88:6443/version
{
  "major": "1",
  "minor": "18",
  "gitVersion": "v1.18.3",
  "gitCommit": "2e7996e3e2712684bc73f0dec0200d64eec7fe40",
  "gitTreeState": "clean",
  "buildDate": "2020-05-20T12:43:34Z",
  "goVersion": "go1.13.9",
  "compiler": "gc",
  "platform": "linux/amd64"
}

可以正確獲取到K8s版本資訊,說明負載均衡器搭建正常。該請求資料流程:curl -> vip(nginx) -> apiserver

通過檢視Nginx日誌也可以看到轉發apiserver IP:

tail /var/log/nginx/k8s-access.log -f
192.168.31.81 192.168.31.71:6443 - [30/May/2020:11:15:10 +0800] 200 422
192.168.31.81 192.168.31.74:6443 - [30/May/2020:11:15:26 +0800] 200 422

到此還沒結束,還有下面最關鍵的一步。

7.4 修改所有Worker Node連線LB VIP

試想下,雖然我們增加了Master2和負載均衡器,但是我們是從單Master架構擴容的,也就是說目前所有的Node元件連線都還是Master1,如果不改為連線VIP走負載均衡器,那麼Master還是單點故障。

因此接下來就是要改所有Node元件配置檔案,由原來192.168.31.71修改為192.168.31.88(VIP):

角色 IP
k8s-master1 192.168.31.71
k8s-master2 192.168.31.74
k8s-node1 192.168.31.72
k8s-node2 192.168.31.73

也就是通過kubectl get node命令檢視到的節點。

在上述所有Worker Node執行:

sed -i 's#192.168.31.71:6443#192.168.31.88:6443#' /opt/kubernetes/cfg/*
systemctl restart kubelet
systemctl restart kube-proxy

檢查節點狀態:

kubectl get node
NAME          STATUS   ROLES    AGE    VERSION
k8s-master    Ready    <none>   34h    v1.18.3
k8s-master2   Ready    <none>   101m   v1.18.3
k8s-node1     Ready    <none>   33h    v1.18.3
k8s-node2     Ready    <none>   33h    v1.18.3

至此,一套完整的 Kubernetes 高可用叢集就部署完成了!

PS:如果你是在公有云上,一般都不支援keepalived,那麼你可以直接用它們的負載均衡器產品(內網就行,還免費~),架構與上面一樣,直接負載均衡多臺Master kube-apiserver即可!

如果你在學習中遇到問題或者文件有誤可聯絡阿良~ 微信: init1024

部署一套完整的Kubernetes高可用叢集(二進位制,最新版v1.18)下

相關文章