標籤(空格分隔): kubernetes系列node
- 一: kubernetes 高可用簡介
- 二: 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服務器相似,增長負載均衡器對其負載均衡便可,而且可水平擴容。
以前步驟參考: https://blog.51cto.com/flyfish225/2504511 在node04.flyfish 節點上面部署docker 2.1 解壓二進制包 tar zxvf docker-19.03.9.tgz mv docker/* /usr/bin
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 阿里雲鏡像加速器
2.4 啓動並設置開機啓動 systemctl daemon-reload systemctl start docker systemctl enable docker
部署Master2 Node(192.168.100.14) 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.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
3. 刪除證書文件 刪除kubelet證書和kubeconfig文件: rm -f /opt/kubernetes/cfg/kubelet.kubeconfig rm -f /opt/kubernetes/ssl/kubelet*
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
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
kubectl get cs
7. 批准kubelet證書申請 在node01.flyfish 節點 上面 批准受權 kubectl get csr kubectl certificate approve node-csr-fyeyjxpS4JMpC2QvfmLOyeBbYUiMoYTSTGQETWVlqD4
kubectl get node
kube-apiserver高可用架構圖:
在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
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 addr 在node05.flyfish 上面 有一個 虛擬VIP
7. Nginx+Keepalived高可用測試 關閉主節點Nginx,測試VIP是否漂移到備節點服務器。 殺掉node05.flyfish 的nginx pkill nginx 查看浮動IP 是否 飄到了node07.flyfish 節點
能夠看到 浮動VIP 已經飄到了node07.flyfish 主機上面了
去任意一個k8s 節點查看 服務器VIP 是否可以 獲取到kube-apiserver 的 信息 curl -k https://192.168.100.100:6443/version
檢查 nignx 日誌
雖然咱們增長了Master2和負載均衡器,可是咱們是從單Master架構擴容的,也就是說目前全部的Node組件鏈接都仍是Master1,若是不改成鏈接VIP走負載均衡器,那麼Master仍是單點故障。 所以接下來就是要改全部Node組件配置文件,由原來192.168.100.11修改成192.168.100.100(VIP):
全部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
至此 k8s 多節點master 集羣配置完成