1、在兩臺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