安装部署主控节点L4反代服务

根据我们架构图,在11/12机器上做反代

# 11/12机器
~]# yum install nginx -y
# 添加在最下面
~]# vi /etc/nginx/nginx.conf
stream {
    upstream kube-apiserver {
        server 10.4.7.21:6443     max_fails=3 fail_timeout=30s;
        server 10.4.7.22: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
# keepalived 监控端口脚本
~]# 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
~]# chmod +x /etc/keepalived/check_port.sh

由于7443端口未监听,Nginx 启动报 [emerg] bind() failed的可以参考这个方法(感谢https://gitee.com/wangming91/)

yum install -y :安装并自动yes

nginx -t :确定nginx.conf有没有语法错误

systemctl start :启动服务

systemctl enable :开机自启

# 仅以下分主从操作:
# 把原有内容都删掉,命令行快速按打出dG
# 注意,下面的vrrp_instance下的interface,我的机器是eth0配置了网卡,有的版本是ens33配置网卡,可以用ifconfig查看,第一行就是,如果你是ens33,改这个interface ens33
# keepalived 主(即11机器):
11 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id 10.4.7.11
}
vrrp_script chk_nginx {
    script "/etc/keepalived/check_port.sh 7443"
    interval 2
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 251
    priority 100
    advert_int 1
    mcast_src_ip 10.4.7.11
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 11111111
    }
    track_script {
         chk_nginx
    }
    virtual_ipaddress {
        10.4.7.10
    }
}
keepalived从(即12机器):
12 ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
	router_id 10.4.7.12
}
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.4.7.12
	priority 90
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass 11111111
	}
	track_script {
		chk_nginx
	}
	virtual_ipaddress {
		10.4.7.10
	}
}
# 11/12机器
~]# systemctl start keepalived
~]# systemctl enable keepalived
# 在11机器
11 ~]# ip add

小实验(可不做):

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

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

# 11/12机器执行:
~]# systemctl restart keepalived
# 11机器:
~]# ip add

生产中,人工确定机器没问题了,再手动回来