安装 nginx 主备,在 master1 和 master2 上做 nginx 主备安装,在master1、master2上操作
yum install nginx keepalived -y
修改 nginx 配置文件。在master1、master2上操作
vi /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
四层负载均衡,为两台Master apiserver组件提供负载均衡
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
access_log /var/log/nginx/k8s-access.log main;
upstream k8s-apiserver {
server 10.10.1.11:6443; # xianchaomaster1 APISERVER IP:PORT
server 10.10.1.12:6443; # xianchaomaster2 APISERVER IP:PORT
server 10.10.1.13:6443; # xianchaomaster3 APISERVER IP:PORT
}
server {
listen 16443; # 由于nginx与master节点复用,这个监听端口不能是6443,否则会冲突
proxy_pass k8s-apiserver;
}
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80 default_server;
server_name _;
location / {
}
}
}
keepalive 配置,主 keepalived,在master1上操作
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_MASTER
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
}
vrrp_instance VI_1 {
state MASTER
interface ens33 # 修改为实际网卡名
virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
priority 100 # 优先级,备服务器设置 90
advert_int 1 # 指定VRRP 心跳包通告间隔时间,默认1秒
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟IP
virtual_ipaddress {
10.10.1.99/24
}
track_script {
check_nginx
}
}
#vrrp_script:指定检查 nginx 工作状态脚本(根据 nginx 状态判断是否故障转移)
#virtual_ipaddress:虚拟 IP(VIP),在master1上操作
vi /etc/keepalived/check_nginx.sh
!/bin/bash
1、判断Nginx是否存活
counter=ps -C nginx --no-header | wc -l
if [ $counter -eq 0 ]; then
#2、如果不存活则尝试启动Nginx
systemctl start nginx
sleep 2
#3、等待2秒后再次获取一次Nginx状态
counter=`ps -C nginx --no-header | wc -l`
#4、再次进行判断,如Nginx还不存活则停止Keepalived,让地址进行漂移
if [ $counter -eq 0 ]; then
systemctl start keepalived
fi
fi
添加可执行权限,在master1上操作
chmod +x /etc/keepalived/check_nginx.sh
#keepalive 配置,备 keepalived,在master2上操作
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_MASTER
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 # 修改为实际网卡名
virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的
priority 90 # 优先级,备服务器设置 90
advert_int 1 # 指定VRRP 心跳包通告间隔时间,默认1秒
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟IP
virtual_ipaddress {
10.10.1.99/24
}
track_script {
check_nginx
}
}
#vrrp_script:指定检查 nginx 工作状态脚本(根据 nginx 状态判断是否故障转移)
#virtual_ipaddress:虚拟 IP(VIP),在master2上操作
vi /etc/keepalived/check_nginx.sh
!/bin/bash
1、判断Nginx是否存活
counter=ps -C nginx --no-header | wc -l
if [ $counter -eq 0 ]; then
#2、如果不存活则尝试启动Nginx
systemctl start nginx
sleep 2
#3、等待2秒后再次获取一次Nginx状态
counter=`ps -C nginx --no-header | wc -l`
#4、再次进行判断,如Nginx还不存活则停止Keepalived,让地址进行漂移
if [ $counter -eq 0 ]; then
systemctl start keepalived
fi
fi
添加可执行权限,在master2上操作
chmod +x /etc/keepalived/check_nginx.sh
#注:keepalived 根据脚本返回状态码(0 为工作正常,非 0 不正常)判断是否故障转移。
#启动服务,在master1、master2上操作
systemctl daemon-reload
yum install nginx-mod-stream -y
systemctl start nginx
systemctl start keepalived
systemctl enable nginx keepalived
#测试 vip 是否绑定成功master1操作
ip add
#测试 keepalived
停掉 master1 上的 nginx。vip 会漂移到 master2
五、把node节点上的单节点ip改为vip地址
原来 10.10.1.11 修改为 10.10.1.99(VIP)。,在node1、上操作
sed -i 's#10.10.1.11:6443#10.10.1.99:16443#' /etc/kubernetes/kubelet-bootstrap.kubeconfig
sed -i 's#10.10.1.11:6443#10.10.1.99:16443#' /etc/kubernetes/kubelet.json
sed -i 's#10.10.1.11:6443#10.10.1.99:16443#' /etc/kubernetes/kubelet.kubeconfig
sed -i 's#10.10.1.11:6443#10.10.1.99:16443#' /etc/kubernetes/kube-proxy.yaml
sed -i 's#10.10.1.11:6443#10.10.1.99:16443#' /etc/kubernetes/kube-proxy.kubeconfig
systemctl restart kubelet kube-proxy
这样高可用集群就安装好了
![image.png](https://ucc.alicdn.com/pic/developer-ecology/b53adc598a334c6db4975f67c3be92a9.png)