安装部署主控节点4层反向代理服务,部署在hdss-1-11 hdss-1-12机器上,用VIP 192.168.1.10的7443端口,反代hdss-1-21、hdss-1-22的apiserver6443端口
nginx配置(四层反向代理)
HDSS-1-11和HDSS-1-12上同时操作,安装nginx,在配置文件最后追加反向代理的配置
[root@hdss7-11 ~]# yum install -y nginx
[root@hdss7-11 ~]# vi /etc/nginx/nginx.conf
stream {
upstream kube-apiserver {
server 192.168.1.21:6443 max_fails=3 fail_timeout=30s;
server 192.168.1.22:6443 max_fails=3 fail_timeout=30s;
}
server {
listen 7443;
proxy_connect_timeout 2s;
proxy_timeout 900s;
proxy_pass kube-apiserver;
}
}
检查配置文件并启动
[root@hdss-1-11 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hdss-1-11 nginx]# systemctl start nginx
[root@hdss-1-11 nginx]# systemctl enable nginx
keepalived安装配置
[root@hdss-1-11 nginx]# yum install keepalived -y
编写监听脚本
[root@hdss-1-11 nginx]# vim /etc/keepalived/check_port.sh
#!/bin/bash
#keepalived 监控端口脚本
#使用方法:
#在keepalived的配置文件中
#vrrp_script check_port {#创建一个vrrp_script脚本,检查配置
# script "/etc/keepalived/check_port.sh 6379" #配置监听的端口
# interval 2 #检查脚本的频率,单位(秒)
#}
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
赋执行权
[root@hdss-1-12 nginx]# chmod +x /etc/keepalived/check_port.sh
配置keepalived keepalived 主:
[root@hdss-1-11 keepalived]# vim keepalived.conf
global_defs {
router_id 192.168.1.11
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_port.sh 7443"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens160 # 根据实际网卡更改
virtual_router_id 251
priority 100
advert_int 1
mcast_src_ip 192.168.1.11
nopreempt
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.10
}
}
keepalived从:
[root@hdss-1-12 nginx]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id 192.168.1.12
script_user root
enable_script_security
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_port.sh 7443"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 251
mcast_src_ip 192.168.1.12
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.10
}
}
启动keepalived
[root@hdss7-11 ~]# systemctl start keepalived
[root@hdss7-11 ~]# systemctl enable keepalived
查看虚拟ip地址10
关闭nginx在11上,查看粗呢ip是否发生漂移