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

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

標籤(空格分隔): kubernetes系列node


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

一: kubernetes 高可用簡介

1.1 kubernetes 高可用介紹

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

Kubernetes做爲容器集羣系統,經過健康檢查+重啓策略實現了Pod故障自我修復能力,經過調度算法實現將Pod分佈式部署,並保持預期副本數,根據Node失效狀態自動在其餘Node拉起Pod,實現了應用層的高可用性。

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

Master節點扮演着總控中心的角色,經過不斷與工做節點上的Kubelet進行通訊來維護整個集羣的健康工做狀態。若是Master節點故障,將沒法使用kubectl工具或者API作任何集羣管理。

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

1.2 部署結構圖

image_1eg26cjs6bev1pk8ioekrg13m69.png-397.7kB

1.3 多master 結構圖

image_1eg26eoql1es9sq7r481sh61l4sm.png-1393.7kB


二:部署承接上文

以前步驟參考:
https://blog.51cto.com/flyfish225/2504511

在node04.flyfish 節點上面部署docker 

2.1 解壓二進制包

tar zxvf docker-19.03.9.tgz
mv docker/* /usr/bin

image_1eg26nm471e421q9bqt91vphs8p9.png-74.4kB

image_1eg26o800nuu5nfjoh1r0a113qm.png-43.6kB

2.2 systemd管理docker

cat > /usr/lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
2.3 建立配置文件

mkdir /etc/docker
cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF

registry-mirrors 阿里雲鏡像加速器

image_1eg26pmuj15f7161ajdn163vlfi13.png-188.2kB

2.4 啓動並設置開機啓動

systemctl daemon-reload
systemctl start docker
systemctl enable docker

image_1eg26q3j216m511bp16khurj1rit1g.png-107.9kB


部署Master2 Node(192.168.100.14)

Master2 與已部署的Master1全部操做一致。因此咱們只需將Master1全部K8s文件拷貝過來,再修改下服務器IP和主機名啓動便可。

1. 建立etcd證書目錄

在Master2建立etcd證書目錄:

mkdir -p /opt/etcd/ssl

image_1eg274r7ig4olnt1pn81gc85f1t.png-25.4kB

2. 拷貝文件(Master1操做)

拷貝Master1上全部K8s文件和etcd證書到Master2:

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

image_1eg275qf71r3tltc1t7nalebh82a.png-218.8kB

3. 刪除證書文件

刪除kubelet證書和kubeconfig文件:

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

image_1eg276cac1mm41tsvonf86i18bj2n.png-54.5kB


4. 修改配置文件IP和主機名

修改apiserver、kubelet和kube-proxy配置文件爲本地IP:

vim /opt/kubernetes/cfg/kube-apiserver.conf
...
--bind-address=192.168.100.14 \
--advertise-address=192.168.100.14 \
...

vim /opt/kubernetes/cfg/kubelet.conf
--hostname-override=node04.flyfish

vim /opt/kubernetes/cfg/kube-proxy-config.yml
hostnameOverride: node04.flyfish

image_1eg27aj5g17ld15ss1u5e1nn6ddc34.png-184.5kB

image_1eg27f6nr19fa1bgiauf176m1t003h.png-112.2kB

image_1eg27g5ehqsaphssquok4133k3u.png-100.3kB


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

image_1eg27hiq41hmjrmq1emds4kkb94b.png-258.5kB


kubectl get cs

image_1eg27iuo4hqd1mpqq7m1o3h1eer4o.png-67.3kB


7. 批准kubelet證書申請
   在node01.flyfish 節點 上面 批准受權

kubectl get csr

kubectl certificate approve node-csr-fyeyjxpS4JMpC2QvfmLOyeBbYUiMoYTSTGQETWVlqD4

image_1eg27l0c82vq1bt8rbq1n9ctbk55.png-129kB

image_1eg27oqg0oie1hd1kl6p108nb5i.png-86.4kB


kubectl get node

image_1eg27qd7v1k3p1n05pve1u6b1j1u5v.png-73.9kB


三:部署nginx 負載均衡服務器

kube-apiserver高可用架構圖:

image_1eg2ajbbsknc16sk1bml1jm01j866c.png-143.3kB

在node05.flyfish 與node07.flyfish 上面部署 nginx 與keepalive 

注意在 node06.flyfish 上面部署了vmware harbor 

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

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.100.11:6443;   # Master1 APISERVER IP:PORT
       server 192.168.100.14: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)nginx

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.100.100/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

四: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

image_1eg2de1gah2s18tv1rqdl4g1ttl6p.png-156.8kB

image_1eg2deg3pn6hkg1clb1oag9vl76.png-183.6kB


6. 查看keepalived工做狀態
ip addr 

在node05.flyfish 上面 有一個 虛擬VIP

image_1eg2djmdav50lega0n16rtjpb7j.png-139.3kB


7. Nginx+Keepalived高可用測試

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

殺掉node05.flyfish 的nginx 

pkill nginx 

查看浮動IP 是否 飄到了node07.flyfish 節點

image_1eg2doohu1njnq351dkp1bn31c1j80.png-128.3kB

能夠看到 浮動VIP 已經飄到了node07.flyfish 主機上面了

image_1eg2dpbrq58bi3q11hm10p3lj48d.png-131.6kB


去任意一個k8s 節點查看 服務器VIP 是否可以 獲取到kube-apiserver 的 信息

curl -k https://192.168.100.100:6443/version

image_1eg2duhts1jn4vrut8f12c515ap9a.png-83.9kB


檢查 nignx 日誌

image_1eg2e1u5m61ne4a57nm518ek9n.png-78.1kB

7.4 修改全部Worker Node鏈接LB VIP

雖然咱們增長了Master2和負載均衡器,可是咱們是從單Master架構擴容的,也就是說目前全部的Node組件鏈接都仍是Master1,若是不改成鏈接VIP走負載均衡器,那麼Master仍是單點故障。

所以接下來就是要改全部Node組件配置文件,由原來192.168.100.11修改成192.168.100.100(VIP):

image_1eg2e7qra1cv01soe1evd5p2s2fa4.png-82kB

全部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

image_1eg2ebjltk3v18o7ksl1l701jptah.png-222.2kB

至此 k8s 多節點master 集羣配置完成
相關文章
相關標籤/搜索