欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

程序员文章站 2022-05-07 14:23:07
...

服务器

主:172.17.0.2 master   

备:172.17.0.3 slave1

lvs虚拟IP:172.17.0.100

 

#nginx下载地址 http://nginx.org/download/

本地文件路径

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

1.dockerfile构建nginx

FROM centos:7
ADD nginx-1.6.0.tar.gz /usr/local
COPY nginx_install.sh /usr/local
RUN sh /usr/local/nginx_install.sh
EXPOSE 80

2.#安装nginx的shell脚本 nginx_install.sh

#! /bin/bash
yum install -y gcc -c++ make pcre pcre-devel zlib-devel
cd /usr/local/nginx-1.16.0
./configure --prefix=/usr/local/nginx && make && make install

3.构建镜像

#master: 
docker build -t mycentos:master-nginx .
#slave
docker build -t mycentos:slave1-nginx .

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 4.查看构建的镜像

docker images

 基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 5.启动容器

#master
docker run -itd --privileged=true -p 80:80 mycentos:master-nginx /usr/sbin/init
#slave1
docker run -itd --privileged=true -p 81:80 mycentos:slave1-nginx /usr/sbin/init

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

6.安装 keepalived

#keepalived 下载地址
http://nginx.org/download/nginx-1.9.10.tar.gz

7.复制本地keepalived包到docker容器中

#master 容器ID:cf2efc79462d  /usr/local/:复制到容器对应的路径下
    docker cp keepalived-1.2.18.tar.gz cf2efc79462d:/usr/local/

#slave1 容器ID: bf0ef0ead87c  /usr/local/:复制到容器对应的路径下
    docker cp keepalived-1.2.18.tar.gz bf0ef0ead87c:/usr/local/

8.分别进入master slave1容器中

1.进入master slave1 容器中
#master
docker exec -it cf2efc79462d /bin/bash

#slave1
docker exec -it bf0ef0ead87c /bin/bash


2.master slave1分别执行以下操作
    2.1解压安装:
    tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
    2.2下载插件openssl
    yum install -y openssl openssl-devel
    2.3开始编译keepalived
    cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
    2.4进入keepalived-1.2.18目录 make一下
    make && make install

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 

9.keepalived安装成Linux系统服务

将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local)
创建文件夹,将keepalived配置文件进行复制:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
复制keepalived脚本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
#如果提示文件已经存在先做rm操作 时入sbin目录
rm -rf keepalived
ln -s /usr/local/keepalived/sbin/keepalived /sbin/

10.配置keepalived

# maser  cat keepalived.conf 
vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2 
    weight -20
}
vrrp_instance VI_1 {
    state MASTER              #指定master
    interface eth0            #网卡信息 进入相应的容器 输入命令ip a 可以查看相应的网卡信息
    virtual_router_id 100     #所有服务器指定一致
    mcast_src_ip 172.17.0.2   #容器Ip地址 输入命令ip a 可以查看相应IP地址
    priority 100              
    nopreempt 
    advert_int 1
    authentication {          
        auth_type PASS     
        auth_pass 1111
    }
    
    track_script {
        chk_nginx 
    }

    virtual_ipaddress {     #设置LVS 虚拟VIP
        172.17.0.100
    }
}


#cat nginx_check.sh  重启nginx脚本
#!/bin/bash
A=`ps -C nginx ?Cno-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx              #启动Nginx命令
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

########################################################################

# slave1  cat keepalived.conf 

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2 
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP              #指定BACKUP
    interface eth0            #网卡信息 进入相应的容器 输入命令ip a 可以查看相应的网卡信息
    virtual_router_id 100     #所有服务器指定一致
    mcast_src_ip 172.17.0.3   #容器Ip地址 输入命令ip a 可以查看相应IP地址
    priority 100              
    nopreempt 
    advert_int 1
    authentication {          
        auth_type PASS     
        auth_pass 1111
    }
    
    track_script {
        chk_nginx 
    }

    virtual_ipaddress {     #设置LVS 虚拟VIP
        172.17.0.100
    }
}

#cat nginx_check.sh  重启nginx脚本
#!/bin/bash
A=`ps -C nginx ?Cno-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx              #启动Nginx命令
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi


##############################################################
#分别将这些宿主机的配置文件复制到相应的docker容器中
#master
docker cp keepalived.conf cf2efc79462d:/etc/keepalived
docker cp nginx_check.sh cf2efc79462d:/etc/keepalived

#slave1
docker cp keepalived.conf bf0ef0ead87c:/etc/keepalived
docker cp nginx_check.sh bf0ef0ead87c:/etc/keepalived

11.分别启动master,slave1 中keepalived

#启动
service keepalived start
#关闭
service keepalived stop

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群 

12进入容器中nginx中修改 /nginx/html/index.html

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

进入master 容器 ip a

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 

进入slave1 容器 ip a 

 基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

13测试

#进入master slave1容器中输入
curl 172.17.0.100

 master:

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 slave1:

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 正常情况下只会访问master服务器 -- master 挂掉的情况下才会访问slave1 服务器

master容器中执行

#模拟挂掉
service keepalived stop

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 

 请求转移到slave1 服务器中 (curl 172.17.0.100)基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

slave1 容中执行 ip a

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

重启master

#模拟重启
service keepalived start

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 进入master 输入 ip a

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 输入curl 172.17.0.100

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 slave1容中 输入ip a

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 slave1容器中输入 curl 172.17.0.100

基于Docker搭建lvs+keepalived+nginx实现高性能负载均衡集群

 

相关标签: keepalived