Centos7+Docker+Nginx+KeepAlived高可用环境搭建

咱要干什么

        用虚拟机环境搭建一台高可用负载均衡架构出来。

        centos+keepalived+docker+nginx

咱打算怎么干

        画的示意,便于理解,暂时先干这么多,以后再补充web集群或者大数据引擎以及数据库集群容灾。

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第1张图片

环境:

VMware Workstation创建虚拟机

CentOS7.9.2009镜像:https://pan.baidu.com/s/1DiayRMVoMtVWpfnlSvBpmg?pwd=5hzi

虚拟机名称 系统 IP 内存 CPU 存储  网络 备注
C7-3 CentOS7.9 192.168.31.132 1G 1*2 20G NAT 高可用负载服务器(主)

一、基础环境配置

        1.1、查看IP

[root@localhost ~]# ip a

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第2张图片

        由于是最小化安装,没有ifconfig命令。这有关系吗?没有关系。我们用ip a命令,问题不大。

        知道IP后,可以用XSHELL连接这台虚拟机进行后续操作,毕竟vmware上直接操作,实在太呆了。

        1.2、检查外网通讯

[root@localhost ~]# ping www.baidu.com

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第3张图片

        不通的要看下网络配置是不是有问题,随便度娘下,一大堆,挨个试就完事了。

        放个参考:https://www.likecs.com/show-201768.html 

        1.3、更换国内yum源(阿里)

        下载wget命令(我用的自己的repo,所以这里先不下载wget也可以,不过后面也用得上,就先干下来吧)

[root@localhost ~]# yum install -y wget

       把repo文件下载传到/etc/yum.repos.d目录(建议先备份源文件,铁头娃直接覆盖)

       repo文件:https://pan.baidu.com/s/1KuTGzh5QKSOoZOPVJCDV6A?pwd=syte

      重建缓存并更新

yum clean all
yum makecache
yum update -y

二、安装docker

        安装yum-utils管理yum源

[root@localhost ~]# yum install -y yum-utils

        添加yum-docker源(阿里),并建立缓存

[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum makecache fast

        安装新版docker

[root@localhost ~]# yum -y install docker-ce

        修改配置文件,添加镜像加速器地址,指定docker存储目录 

[root@localhost ~]# mkdir -p /data/docker
[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# vim /etc/docker/daemon.json
{
  "registry-mirrors":["https://docker.mirrors.ustc.edu.cn"],"graph":"/data/docker"
}

         启动docker并检查安装状态

[root@localhost ~]# systemctl enable docker
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker run hello-world

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第4张图片

 三、docker内安装系统

        下载镜像

[root@localhost ~]# docker pull centos:7

        创建容器

[root@localhost ~]# docker run -it -d --name centos7 -d centos:7

       设置容器自启动

[root@localhost keepalived]# docker update --restart=always centos7_1

        进入容器

[root@localhost ~]# docker exec -it centos7 bash

        安装基础包

[root@localhost ~]# yum update
[root@localhost ~]# yum install -y vim wget gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl--develyum popt-develyum initscripts net-tools

        打包镜像方便复用

[root@localhost ~]# docker commit -a 'nidaye' -m 'i will give you some coller to see see' centos1 centos_template

        停止容器、删除容器、从镜像创建容器

[root@localhost ~]# docker stop centos7
[root@localhost ~]# docker rm centos7
[root@localhost ~]# docker run -it --name centos7_1 -d --privileged centos_template /usr/sbin/init

        附:

#查看所有容器
docker ps -a
#删除容器(需要先停止)
docker rm 容器名称
#启动停止容器
docker run/stop 容器名称
#查看镜像
docker images 或 docker image ls -a
#删除镜像
docker rmi 镜像ID

四、docker内安装nginx

        进入容器

[root@localhost ~]# docker exec -it centos7_1 bash

        安装NGINX库

[root@5277faa64871 /]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

        安装NGINX、配置自启动、启动NGINX

[root@5277faa64871 /]# yum install -y nginx
[root@5277faa64871 /]# systemctl enable nginx
[root@5277faa64871 /]# systemctl start nginx

        测试是否安装成功、启动完成

[root@5277faa64871 /]# curl localhost

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第5张图片

         此时可以查下docker容器内系统的ip,并用宿主机去访问试试是否可通。

[root@5277faa64871 /]# ifconfig
[root@5277faa64871 /]# exit
[root@localhost ~]# curl 172.17.0.2

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第6张图片

五、宿主机安装keepalived

        安装keepalived

[root@localhost ~]# yum install -y wget make gcc gcc-c++ openssl-devel
[root@localhost ~]# wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz
[root@localhost ~]# tar zxvf keepalived-2.0.7.tar.gz
[root@localhost ~]# cd keepalived-2.0.7
[root@localhost ~]# ./configure --prefix=/data/keepalived

        如果有WARNING,只要确保Use VRRP Framework 、Use VRRP VMAC 、Use VRRP authentication 这三项是yes就可以忽略。

[root@localhost ~]# make
[root@localhost ~]# make install

        配置服务方式启动,以及设置自启动

[root@localhost keepalived-2.0.7]# mkdir /etc/keepalived
[root@localhost keepalived-2.0.7]# cp /data/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost keepalived-2.0.7]# systemctl enable keepalived

        修改keepalived配置文件(可以清空源文件,把下面内容复制进去再改吧改吧)

[root@localhost keepalived-2.0.7]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server mail.xuad.com
   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"   # 检查nginx状态的脚本
    interval 2
    weight 3
}

vrrp_instance VI_1 {
    state MASTER     #备份服务器上将MASTER改为SLAVE
    interface ens32 #查看自己的网络信息
    virtual_router_id 51
    priority 100       #备份服务上将100改为小于100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.242 #vip设置(虚拟ip)
    }
    track_script {
        chk_nginx
    }
}

        创建nginx状态检查脚本(检查nginx进程不存在就停掉keeplived)

[root@localhost keepalived-2.0.7]# vim /etc/keepalived/nginx_check.sh
#!/bin/bash
A=`ps -C nginx ?no-header |wc -l`
if [ $A -eq 0 ];then
    systemctl stop keepalived
fi

        *上面的脚本只是为了测试方便,生产环境的逻辑应该是检测到nginx不存在后,先重启docker(其实是重启nginx),如果这时候再检测不到,才会停掉宿主机的keepalived。

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

        授权可执行

[root@localhost keepalived-2.0.7]# chmod +x /etc/keepalived/nginx_check.sh

        启动keepalived

[root@localhost keepalived-2.0.7]# systemctl start keepalived

        查看vip是否设置成功

[root@localhost keepalived-2.0.7]# ip a

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第7张图片

         来波骚操作,用防火墙把docker里这个容器nginx端口捆绑到宿主机的8081

        #172.17.0.2是容器ip,80是容器端口,8081是宿主机端口

[root@localhost keepalived-2.0.7]#  firewall-cmd --add-forward-port=port=8081:proto=tcp:toaddr=172.17.0.2:toport=80 --permanent

        允许防火墙伪装

[root@localhost keepalived-2.0.7]# firewall-cmd --add-masquerade --permanent

        开放端口

[root@localhost keepalived-2.0.7]#  firewall-cmd --add-port=3000/tcp --permanent

        重载防火墙生效

[root@localhost keepalived-2.0.7]# firewall-cmd --reload

        现在打开你的浏览器,访问宿主机ip和vip(虚拟ip)试试吧

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第8张图片

六、克隆上面操作的C7-3虚拟机,创建新的C7-4

       克隆时已关闭C7-3虚拟机

        查看宿主机ip为:192.168.31.133

[root@localhost ~]# ip a

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第9张图片

         进入容器

[root@localhost ~]# docker exec -it centos7_1 bash

        修改nginx欢迎页,方便后面访问时区分流向。

[root@5277faa64871 /]# vim /usr/share/nginx/html/index.html

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第10张图片

         启动并测试

[root@5277faa64871 /]# systemctl restart nginx
[root@5277faa64871 /]# curl localhost

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第11张图片

         退出容器,回到宿主机,修改keepalived配置(下图中两处)

[root@5277faa64871 /]# exit
[root@localhost ~]# vim /etc/keepalived/keepalived.conf

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第12张图片

         重启keepalived

[root@localhost ~]# systemctl restart keepalived

        再打开你的浏览器,输入C7-4的ip和vip

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第13张图片

 七、最后一哆嗦

        启动C7-3虚拟机,检查下keepalived、docker、nginx的状态,确保都启动了。

        这时再访问vip,会发现我们是访问到了C7-3上。

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第14张图片

         下面模拟nginx宕机情况,我们把C7-3容器里的nginx停掉。再访问,发现这次分发到了C7-4上,打完收工。

Centos7+Docker+Nginx+KeepAlived高可用环境搭建_第15张图片

你可能感兴趣的:(linux,负载均衡,系统架构)