k8s 簡易HA

在前一章的基礎上搭建k8s HA

環境

192.168.201.45
另一臺master(apiserver、scheduler、controller-manager)
192.168.201.48
LB_master(nginx、keepalived)
192.168.201.46
LB_backup(nginx、keepalived)
192.168.201.50
VIP

另一臺master

#scp -r /opt/kubernetes/ root@master2_ip:/opt #在單master基礎上繼續,將master1上的文件拷貝到master2上
#scp /usr/lib/systemd/system/{kube-apiserver,kube-controller-manager,kube-scheduler}.service root@master2_ip:/usr/lib/systemd/system/ #將服務一併拷貝過去
#scp -r /opt/etcd/ -r root@master2_ip:/opt
#vi kube-apiserver #將master1_ip修改爲master2_id
#ntpdate time.windows.com #需要注意虛擬機裏的時間要同步
#systemctl start kube-apiserver
#systemctl start kube-controller-manager
#systemctl start kube-scheduler
#kubectl get node #確認是否能夠正常獲取到node

LB_master

#nginx.sh #文件中第一部分($要轉義)。生成安裝yum源
#yum install nginx -y 
#vi /etc/nginx/nginx.conf #將nginx.conf加入http前,並修改相關ip爲master1_ip與master2_ip,添加server。其中http是七層配置,加入的stream是四層配置。
#systemctl start nginx
#yum install keepalived -y
#cp keepalived.conf /etc/keepalived/keepalived.conf #上傳配置文件覆蓋自帶配置文件
#vi /etc/keepalived/keepalived.conf #check_nginx文件,用於故障轉移;配置文件中IP地址未被使用,會使用此IP作爲VIP
#bash -x /etc/nginx/check_nginx.sh
#systemctl start keepalived

nginx.sh

cat > /etc/yum.repos.d/nginx.repo << EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
EOF

nginx.conf


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 10.0.0.3:6443; 
        server 10.0.0.8:6443;
    }
    server {
                listen 6443;
                proxy_pass k8s-apiserver;
    }
    }

LB_backup

#nginx.sh #文件中第一部分($要轉義)。生成安裝yum源,與LB_master一致。
#yum install nginx -y 
#yum install keepalived -y #在 LB_backup安裝
#scp /etc/keepalived/keepalived.conf root@LB_backup_ip:/etc/keepalived/ #在LB上將配置文件複製到LB_backup上,並修改文件,state、interface、priority
#scp /etc/nginx/check_nginx.sh root@ LB_backup_ip:/etc/nginx/
#systemctl start keepalived #在LB_backup上執行
#vi /opt/kubernetes/cfg/bootstrap.kubeconfig #在node上修改將master_ip修改爲VIP
#vi /opt/kubernetes/cfg/kubelet.kubeconfig #在node上修改將master_ip修改爲VIP
#vi /opt/kubernetes/cfg/kube-proxy.kubeconfig #在node上修改將master_ip修改爲VIP
#systemctl restart kubelet
#systemctl restart kube-proxy #重啓服務,node2也同樣

知識點1
配置文件中一個實例vrrp_instance就包括LB_master與LB_backup,所以virtual_router_id值,在一個實例中是一樣的;配置文件中LB_master的state配置爲master,LB_BACKUP配置爲BACKUP;priority是優先級,備服務器要比主服務器數小。
在這裏插入圖片描述

知識點2
在LB_master上配置的VIP,會綁定到配置文件中網絡接口上。此時此IP就是可正常PING通使用IP。
在這裏插入圖片描述

知識點3
驗證VIP是否生效,就在LB_master上停掉nginx,查看VIP是否飄移到LB_backup(使用ip addr查看) 。

知識點4
VIP地址應該包含到server.pem與server-key.pem文件中。也就是在server-csr.json中hosts中。如果未包括重新編輯server-csr.json,然後重新生成server.pem與server-key.pem,並將文件放到master對應的ssl目錄下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章