K8S集群安装 之 安装部署主控节点L4反代服务

一、在两台proxy服务器上安装反代服务

# 211/212机器
~]# yum install nginx -y
~]# vi /etc/nginx/nginx.conf
# 添加在最下面
stream {
    upstream kube-apiserver {
        server 10.3.153.221:6443     max_fails=3 fail_timeout=30s;
        server 10.3.153.222:6443     max_fails=3 fail_timeout=30s;
    }
    server {
        listen 7443;
        proxy_connect_timeout 2s;
        proxy_timeout 900s;
        proxy_pass kube-apiserver;
    }
}

~]# nginx -t
~]# systemctl start nginx
~]# systemctl enable nginx
~]# yum install keepalived -y

~]# vi /etc/keepalived/check_port.sh
#!/bin/bash
CHK_PORT=$1
if [ -n "$CHK_PORT" ];then
        PORT_PROCESS=`ss -lnt|grep $CHK_PORT|wc -l`
        if [ $PORT_PROCESS -eq 0 ];then
                echo "Port $CHK_PORT Is Not Used,End."
                exit 1
        fi
else
        echo "Check Port Cant Be Empty!"
fi

#以下不需要粘贴
#keepalived 监控端口脚本
#使用方法:
#在keepalived的配置文件中
#vrrp_script check_port {#创建一个vrrp_script脚本,检查配置
#    script "/etc/keepalived/check_port.sh 6379" #配置监听的端口
#    interval 2 #检查脚本的频率,单位(秒)

~]# chmod +x /etc/keepalived/check_port.sh
~~~

~~~
# 仅以下分主从操作:
# 把原有内容都删掉
# 注意,下面的vrrp_instance下的interface,我的机器是ens33配置了网卡,有的版本是eth0配置网卡,可以用ifconfig查看,第一行就是
keepalived 主(即11机器):
11 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id 10.3.153.211

}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_port.sh 7443"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 251
    priority 100
    advert_int 1
    mcast_src_ip 10.3.153.211
    nopreempt

    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_script {
         chk_nginx
    }
    virtual_ipaddress {
        10.3.153.240
    }
}

keepalived从(即212机器):
12 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id 10.3.153.212
}
vrrp_script chk_nginx {
    script "/etc/keepalived/check_port.sh 7443"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 251
    mcast_src_ip 10.3.153.212
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        10.3.153.240
    }
}
~~~

~~~
# 211/212机器
~]# systemctl start keepalived
~]# systemctl enable keepalived
# 在11机器
11 ~]# ip add
~~~

~~~
# 实验:在211机器关掉nginx
11 ~]# nginx -s stop
11 ~]# netstat -luntp|grep 7443
# 代理会跑到12机器
~~~

~~~
11 ~]# nginx
11 ~]# netstat -luntp|grep 744
# 再起来,但也不会跑回来,因为我们配置了
    nopreempt
~~~

~~~
# 生产中,人工确定机器没问题了,再手动回来
# 211/212机器执行:
~]# systemctl restart keepalived
# 11机器:
~]# ip add
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章