xiunai 2020-05-15
主机名 | 角色 | IP地址 |
---|---|---|
pg60-11.k8s.host.com | 4层负载均衡 | 10.20.60.11 |
pg60-12.k8s.host.com | 4层负载均衡 | 10.20.60.12 |
注意:这里 10.20.60.11
和 10.20.60.12
使用nginx做4层负载均衡器,在keepalived上配置vip:10.20.60.10
,代理两个 kube-apiserver
,实现高可用。
详见《源码安装openresty-1.11.2.5》
shell> cat /opt/openresty/nginx/conf/nginx.conf stream { upstream kube-apiserver { server 10.20.60.21:6443 max_fails=3 fail_timeout=30s; server 10.20.60.22:6443 max_fails=3 fail_timeout=30s; server 10.20.60.23:6443 max_fails=3 fail_timeout=30s; } server { listen 7443; proxy_connect_timeout 2s; proxy_timeout 900s; proxy_pass kube-apiserver; } }
shell> yum -y install keepalived
shell> cat /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
shell> cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id 10.20.60.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 51 priority 150 advert_int 1 mcast_src_ip 10.20.60.11 nopreempt authentication { auth_type PASS auth_pass 11111111 } track_script { chk_nginx } virtual_ipaddress { 10.20.60.10 } }
shell> cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id 10.20.60.12 } vrrp_script chk_nginx { script "/etc/keepalived/check_port.sh 7443" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 mcast_src_ip 10.20.60.12 authentication { auth_type PASS auth_pass 11111111 } track_script { chk_nginx } virtual_ipaddress { 10.20.60.10 } }
shell> systemctl start keepalived shell> systemctl enable keepalieve shell> /opt/openresty/nginx/sbin/nginx -s reload