• Nginx HA高可用实现记录


    nginx高可用性能部署
    HA高可用
    先制作镜像再创建两个容器
    根据第三条来创建两个容器,你会发现有大量的重复操作,我们可以先制作一个镜像,再根据该镜像创建两个容器,从而避免了大量重复操作。
    1、配置模板容器
    (1)、下载centos:7.6.1810镜像
    docker pull centos:7.6.1810
    (2)、启动并进入容器
    docker run -it centos:7.6.1810 /bin/bash
    (3)、安装基础工具
    yum install -y iproute yum install -y net-tools
    (4)、安装nginx的依赖库和安装nginx
    先安装nginx依赖
    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    再安装nginx
    yum install -y nginx
    (5)、修改index.html的标题
    cd /usr/share/nginx/html vi index.html
    vi index.html
    title改为Master

    Welcome to nginx Master! (6)、启动nginx cd /usr/sbin/ ./nginx • 查看nginx进程id ps -ef | grep nginx • kill所有进程 killall nginx (7)、检测master容器中nginx是否启动 curl localhost (8)、容器内安装keepalived 安装keepalived依赖环境 yum install -y gcc openssl-devel popt-devel 通过源码安装keepalived yum -y install wget cd /root wget http://www.keepalived.org/software/keepalived-2.0.8.tar.gz --no-check-certificate tar -zxvf keepalived-2.0.8.tar.gz rm -f keepalived-2.0.8.tar.gz cd keepalived-2.0.8 ./configure --prefix=/usr/local/keepalived make && make install 将keepalived.conf文件拷贝到/etc/keepalived mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ (9)、在/etc/keepalived目录下创建Keepalived检测nginx的脚本check_nginx.sh,并赋权 cd /etc/keepalived touch check_nginx.sh vi check_nginx.sh A=`ps -ef | grep nginx | grep -v grep | wc -l` if [ $A -eq 0 ];then nginx sleep 2 if [ `ps -ef | grep nginx | grep -v grep | wc -l` -eq 0 ];then #killall keepalived ps -ef|grep keepalived|grep -v grep|awk '{print $2}'|xargs kill -9 fi fi 通过脚本检测nginx有没有挂,一旦挂了,就杀掉keepalived。这样备节点就成为了主节点。 给check_nginx.sh赋于执行权限 chmod +x /etc/keepalived/check_nginx.sh 10)、编辑keepalived.conf 先编辑master的keepalived.conf文件 vi keepalived.conf ! Configuration File for keepalived

    global_defs {
    notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 192.168.200.1
    smtp_connect_timeout 30
    router_id LVS_MASTER
    vrrp_skip_check_adv_addr

    vrrp_strict

    vrrp_garp_interval 0
    vrrp_gna_interval 0
    }

    vrrp_script chk_nginx {
    script “/etc/keepalived/check_nginx.sh”
    interval 2
    weight -20
    fall 3
    rise 2
    user root
    }

    vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 2
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.0.66
    }
    track_script {
    chk_nginx
    }
    }

    (11)、退出容器,制作镜像
    执行exit退出容器,查看容器
    docker ps -a
    结果
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 07c519d6244b centos:7.6.1810 “/bin/bash” 25 minutes ago Exited (1) 2 minutes ago peaceful_fermi
    使用commit命令将容器保存为镜像
    docker commit peaceful_fermi keepalived_nginx:v1
    此镜像的内容就是当前容器的内容,接下来你可以用此镜像再次运行新的容器
    启动主容器
    docker run -it --privileged=true --name keepalived_master --restart=always --net mynet --ip 192.168.0.2 keepalived_nginx:v1 /usr/sbin/init

    docker run -it --privileged=true --name keepalived_slave --restart=always --net mynet --ip 192.168.0.3 keepalived_nginx:v1 /usr/sbin/init
    进入备机中修改 keepalived配置
    docker exec -it keepalived_slave bash cd /etc/keepalived vi keepalived.conf
    ! Configuration File for keepalived

    global_defs {
    notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 192.168.200.1
    smtp_connect_timeout 30
    router_id LVS_SLAVE
    vrrp_skip_check_adv_addr

    vrrp_strict

    vrrp_garp_interval 0
    vrrp_gna_interval 0
    }

    vrrp_script chk_nginx {
    script “/etc/keepalived/check_nginx.sh”
    interval 2
    weight -20
    fall 3
    rise 2
    user root
    }

    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 2
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.0.66
    }
    track_script {
    chk_nginx
    }
    }
    修改router_id、state、priority即可。
    修改完后重启keepalived
    cd /usr/local/keepalived/sbin/ ./keepalived -f /etc/keepalived/keepalived.conf
    关闭keepalived
    ps -ef | grep keepalived kill -9 3747
    修改nginx中index.html的标题
    cd /usr/share/nginx/html vi index.html
    启动nginx
    cd /usr/sbin/
    ./nginx
    测试
    进入主容器启动nginx和keepalived
    查看主容器IP ip a

    [root@b3a23e52b268 sbin]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 188: eth0@if189: mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.0.2/16 brd 192.168.255.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.0.66/32 scope global eth0 valid_lft forever preferred_lft forever

    进入从容器查看IP ip a

    [root@ad57dfb60f80 /]# ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 190: eth0@if191: mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.0.3/16 brd 192.168.255.255 scope global eth0 valid_lft forever preferred_lft forever

    主容器中通过curl命令访问192.168.0.66

    [root@b3a23e52b268 sbin]# curl 192.168.0.66 Welcome to nginx Master!

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

    从容器中通过curl命令访问192.168.0.66

    [root@ad57dfb60f80 /]# curl 192.168.0.66 Welcome to nginx Master!

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

    关闭主容器的keepalived
    [root@b3a23e52b268 sbin]# ps -ef | grep keepalived root 3771 0 1 09:19 ? 00:00:04 ./keepalived -f /etc/keepalived/keepalived.conf root 3773 3771 0 09:19 ? 00:00:00 ./keepalived -f /etc/keepalived/keepalived.conf root 15815 3744 0 09:23 pts/1 00:00:00 grep --color=auto keepalived
    杀死keepalived
    kill -9 3771
    在主容器中查看IP ip a

    [root@b3a23e52b268 sbin]# ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 188: eth0@if189: mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.0.2/16 brd 192.168.255.255 scope global eth0 valid_lft forever preferred_lft forever

    在从容器中查看IP ip a

    [root@ad57dfb60f80 /]# ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 190: eth0@if191: mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.0.3/16 brd 192.168.255.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.0.66/32 scope global eth0 valid_lft forever preferred_lft forever

    发现VIP绑定到从容器中了。
    在主容器中通过curl命令访问192.168.0.66

    [root@b3a23e52b268 sbin]# curl 192.168.0.66 Welcome to nginx Slave!

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

    在从容器中通过curl命令访问192.168.0.66

    [root@ad57dfb60f80 sbin]# curl 192.168.0.66 Welcome to nginx Slave!

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

    主容器中启动keepalived
    主容器中查看ip ip a

    [root@b3a23e52b268 sbin]# ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 192: eth0@if193: mtu 1500 qdisc noqueue state UP group default link/ether 02:42:c0:a8:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.0.2/16 brd 192.168.255.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.0.66/32 scope global eth0 valid_lft forever preferred_lft forever

    发现VIP又绑定到主容器上了。
    在主容器中通过curl命令访问192.168.0.66

    [root@b3a23e52b268 sbin]# curl 192.168.0.66 Welcome to nginx Master!

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

    说明keepalived实现了nginx的高可用。

  • 相关阅读:
    【ES6】js中的__proto__和prototype
    视频特效制作软件 After Effects 2024 mac中文版新增功能
    个推漫话数据智能:解读《天才基本法》中的贝叶斯网络及原理
    一本通1057;简单计算器(2)
    贝叶斯视角下的机器学习
    文本中物流获取易语言代码
    从优橙教育5G网络优化就业班出去之后,这好找工作嘛?
    Spring中Bean注入的几种方式
    Python复习笔记3——测试与调试技巧
    LabVIEW中通过编程获取前面板的屏幕截图
  • 原文地址:https://blog.csdn.net/weixin_38738049/article/details/127614624