溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

K8S多master部署二:部署LoadBlance

發(fā)布時(shí)間:2020-02-28 04:06:10 來源:網(wǎng)絡(luò) 閱讀:482 作者:qq5d47f509174fe 欄目:云計(jì)算

前情提要

以下所有操作均在單master群集已完成部署的情況下進(jìn)行。

所有服務(wù)器均保證防火墻常閉,核心功能selinux關(guān)閉。

服務(wù)器角色分配

角色 地址 安裝組件
master 192.168.142.220 kube-apiserver kube-controller-manager kube-scheduler etcd
master02 192.168.142.120 kube-apiserver kube-controller-manager kube-scheduler
node1 192.168.142.136 kubelet kube-proxy docker flannel etcd
node2 192.168.142.132 kubelet kube-proxy docker flannel etcd
nginx1 192.168.142.130 nginx keepalived
nginx2 192.168.142.140 nginx keepalived
VIP 192.168.142.20 虛擬地址

一、nginx端部署

建立nginx的YUM源

[root@lb-ma ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx-repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0

EOF

安裝nginx并進(jìn)行配置

[root@lb-ma ~]# yum install nginx -y

#添加stream模塊實(shí)現(xiàn)四層轉(zhuǎn)發(fā)
[root@lb-ma ~]# vim /etc/nginx/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 192.168.142.220:6443;
        server 192.168.142.120:6443;
    }
    server {
                listen 6443;
                proxy_pass k8s-apiserver;
    }
    }

#開啟服務(wù)
[root@lb-ma ~]# systemctl start nginx
[root@lb-ma ~]# systemctl enable nginx

安裝Keppalived服務(wù)

[root@lb-ma ~]# yum -y install keepalived

##修改keepalived配置文件
[root@lb-ma ~]# vim /etc/keepalived/keepalived.conf
###原本的全部刪除,按下面新建
! Configuration File for keepalived

global_defs {
  # 接收郵件地址
     notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   # 郵件發(fā)送地址
   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 "/usr/local/nginx/sbin/check_nginx.sh"
}

vrrp_instance VI_1 {
    state MASTER   #備服務(wù)器改為BACKUP
    interface ens33   #監(jiān)控ens33網(wǎng)卡
    virtual_router_id 51 # VRRP 路由 ID實(shí)例,每個(gè)實(shí)例是唯一的
    priority 100    # 優(yōu)先級,備服務(wù)器設(shè)置 90
    advert_int 1    # 指定VRRP 心跳包通告間隔時(shí)間,默認(rèn)1秒
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.142.20/24   #VIP地址
    }
    track_script {
        check_nginx
    }
}

建立監(jiān)控腳本

一旦nginx處于down狀態(tài),將會(huì)自動(dòng)關(guān)閉keeplived

[root@lb-ma ~]# mkdir -p /usr/local/nginx/sbin/
[root@lb-ma ~]# vim /usr/local/nginx/sbin/check_nginx.sh
##手動(dòng)進(jìn)行編寫
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    systemctl stop keepalived
fi
[root@lb-ma ~]# chmod +x /usr/local/nginx/sbin/check_nginx.sh

#開啟keeplived
[root@lb-ma ~]# systemctl start keepalived
[root@lb-ma ~]# systemctl enable keepalived

此時(shí),前面的負(fù)載均衡已經(jīng)配置完畢,但是并不能起到實(shí)際的作用,原因就是后方node節(jié)點(diǎn)中負(fù)責(zé)進(jìn)行身份識別的kubeconfig文件中的地址沒有改變,無法識別。

二、node端修改

更改kubeconfig中的地址

[root@node1 ~]# vim /opt/kubernetes/cfg/bootstrap.kubeconfig
[root@node1 ~]# vim /opt/kubernetes/cfg/kube-proxy.kubeconfig
[root@node1 ~]# vim /opt/kubernetes/cfg/kubelet.kubeconfig
##三個(gè)文件全部改為
    server: https://192.168.142.20:6443    #指向VIP地址

重啟kubelet & kube-proxy服務(wù)

[root@node1 ~]# systemctl restart kubelet
[root@node1 ~]# systemctl restart kube-proxy

以上,就是nginx做負(fù)載均衡,keppalived做雙機(jī)熱備的全部部署過程


DEMO:建立POD進(jìn)行檢測

master端建立測試pod

[root@master ~]# kubectl run nginx --image=nginx
##建立pod
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created

#查看建立打的pod
[root@master ~]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-7tdvp   1/1     Running   0          21s

此時(shí),pod只能進(jìn)行簡單的查看,一旦查看日志會(huì)報(bào)錯(cuò)。為了解決這個(gè)問題,可采用下面的辦法解決。

#注意日志問題
[root@master ~]# kubectl logs nginx-dbddb74b8-7tdvp
Error from server (Forbidden): Forbidden (user=system:anonymous, verb=get, resource=nodes, subresource=proxy) ( pods/log nginx-dbddb74b8-7tdvp)

###解決辦法:
[root@master ~]# kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous
clusterrolebinding.rbac.authorization.k8s.io/cluster-system-anonymous created
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI