搭建双master-k8s集群-第一步,搭建keepalived高可用和nginx转发配置

1、准备2台lvs的虚拟机,并配置好IP地址

lvs01 192.168.136.11
lvs02 192.168.136.12
VIP 192.168.136.10

2、2台机器均安装keepalived并进行配置

1、keepalived在线安装

yum -y install keepalived

2、lvs01的keepalived.conf配置

vim /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 LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -5
    fall 3
    rise 2
}


vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.10
    }

track_script {
       chk_nginx
    }
}

注意:MASTER节点,priority 为100,权限数值尽量填高一点

3、lvs02的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 LVS_DEVEL
   vrrp_skip_check_adv_addr
#   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -5
    fall 3
    rise 2
}


vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.10
    }

track_script {
       chk_nginx
    }
}

注意:BACKUP节点,priority 为50,权限数值尽量低一点

3、编辑nginx_check.sh脚本,2台机器均需要

vim /etc/keepalived/nginx_check.sh

#/bin/bash
A=`ps -C nginx  --no-header |wc -l`
if [ $A -eq 0 ];then
    systemctl restart docker
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

4、docker下部署nginx服务

1、拉取nginx-stream的镜像

docker pull tekn0ir/nginx-stream

2、开启nginx-stream服务

docker run -d \
       -p 80:80 \
       -p 6443:6443 \
       -v /opt/nginx/http.conf.d:/opt/nginx/http.conf.d \
       -v /opt/nginx/stream.conf.d:/opt/nginx/stream.conf.d \
       --restart=always \
       --name nginx \
       tekn0ir/nginx-stream

3、编辑master.conf的配置文件

vim /opt/nginx/stream.conf.d/master.conf

upstream master {
    server 192.168.136.15:6443;
    server 192.168.136.16:6443;
}

server {
    listen 6443;
    proxy_pass master;
}

注:这里填的IP地址为master01和master02的地址

5、开启nginx和keepalived服务,并查看虚拟IP是否正常

##开启keepalived服务
systemctl start keepalived
systemctl enable  keepalived

查看nginx服务是否正常,docker ps -a
在这里插入图片描述
查看VIP

在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐