• haproxy+keepalived实现高可用


    主机名 IP地址
    VIP 192.168.200.254
    Haproxy-1 192.168.200.101
    Haproxy-2 192.168.200.102
    Nginx1 192.168.200.103
    Nginx2 192.168.200.104


    1.1 首先安装Nginx1
    [root@Nginx-1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
    [root@Nginx-1 ~]# useradd -M -s /sbin/nologin nginx
    [root@Nginx-1 ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
    [root@Nginx-1 ~]# cd /usr/src/nginx-1.6.2
    [root@Nginx-1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
    [root@Nginx-1 nginx-1.6.2]# cd /usr/local/nginx/html/
    [root@Nginx-1 html]# echo "server 192.168.200.103" > index.html
    [root@Nginx-1 html]# /usr/local/nginx/sbin/nginx
    [root@Nginx-1 html]# netstat -anpt |grep nginx
    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4503/nginx
    1.2 安装Nginx2, 同Nginx1搭建方式是一样的。
    与Nginx1唯一不同的是:
    [root@Nginx-2 html]# echo "server 192.168.200.104" > index.html

    haproxy1服务器:192.168.200.111
    [root@localhost ~]# systemctl stop firewalld
    [root@localhost ~]# iptables -F
    [root@localhost ~]# setenforce 0
    [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
    [root@localhost ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
    [root@localhost ~]# cd /usr/src/haproxy-1.4.24/
    [root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install
    [root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
    [root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
    [root@localhost haproxy-1.4.24]# vim /etc/haproxy/haproxy.cfg
    # this config needs haproxy-1.1.28 or haproxy-1.2.1

    global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice
    #log loghost local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    uid 99
    gid 99
    daemon
    #debug
    #quiet

    defaults
    log global
    mode http
    option httplog
    option dontlognull
    retries 3
    #redispatch
    maxconn 2000
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000

    listen web-cluster 0.0.0.0:80
    option httpchk GET /index.html
    balance roundrobin
    server inst1 192.168.200.113:80 check inter 2000 fall 3
    server inst2 192.168.200.114:80 check inter 2000 fall 3
    [root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
    [root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
    [root@localhost ~]# chmod +x /etc/init.d/haproxy
    [root@localhost ~]# /etc/init.d/haproxy start
    [root@localhost ~]# yum -y install keepalived
    [root@localhost ~]# vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

    global_defs {
    router_id LVS_DEVEL
    }
    vrrp_script chk_http_port {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 10
    }

    vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    track_script {
    chk_http_port
    }
    }
    [root@localhost ~]# vim /etc/keepalived/check_haproxy.sh
    #!/bin/bash
    num=`ps -C haproxy --no-header |wc -l`
    if [ $num -eq 0 ]
    then
    /etc/init.d/haproxy start
    sleep 3
    if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
    then
    systemctl stop keepalived
    fi
    fi
    [root@localhost ~]# chmod +x /etc/keepalived/check_haproxy.sh
    [root@localhost ~]# systemctl restart keepalived


    haproxy2服务器:192.168.200.112
    [root@localhost ~]# systemctl stop firewalld
    [root@localhost ~]# iptables -F
    [root@localhost ~]# setenforce 0
    [root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
    [root@localhost ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
    [root@localhost ~]# cd /usr/src/haproxy-1.4.24/
    [root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install
    [root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
    [root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
    [root@localhost haproxy-1.4.24]# vim /etc/haproxy/haproxy.cfg
    # this config needs haproxy-1.1.28 or haproxy-1.2.1

    global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice
    #log loghost local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    uid 99
    gid 99
    daemon
    #debug
    #quiet

    defaults
    log global
    mode http
    option httplog
    option dontlognull
    retries 3
    #redispatch
    maxconn 2000
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000

    listen web-cluster 0.0.0.0:80
    option httpchk GET /index.html
    balance roundrobin
    server inst1 192.168.200.113:80 check inter 2000 fall 3
    server inst2 192.168.200.114:80 check inter 2000 fall 3
    [root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
    [root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
    [root@localhost ~]# chmod +x /etc/init.d/haproxy
    [root@localhost ~]# /etc/init.d/haproxy start
    [root@localhost ~]# yum -y install keepalived
    [root@localhost ~]# vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

    global_defs {
    router_id LVS_DEVEL
    }
    vrrp_script chk_http_port {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 5
    }

    vrrp_instance VI_1 {
    state BACKUP
    interface ens32
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    track_script {
    chk_http_port
    }
    }
    [root@localhost ~]# vim /etc/keepalived/check_haproxy.sh
    #!/bin/bash
    num=`ps -C haproxy --no-header |wc -l`
    if [ $num -eq 0 ]
    then
    /etc/init.d/haproxy start
    sleep 3
    if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
    then
    systemctl stop keepalived
    fi
    fi
    [root@localhost ~]# chmod +x /etc/keepalived/check_haproxy.sh
    [root@localhost ~]# systemctl restart keepalived

  • 相关阅读:
    Android应用程序执行流程
    Android的架构与Android应用程序启动流程
    Android开发环境使用工具Eclipse IDE工程目录结构
    MySQL 的 crash-safe 原理解析
    vivo 悟空活动中台
    图解 Promise 实现原理(三)—— Promise 原型方法实现
    领域驱动设计(DDD)实践之路(三):如何设计聚合
    深入浅出开源监控系统Prometheus(上)
    你还应该知道的哈希冲突解决策略
    反应式编程 RxJava 设计原理解析
  • 原文地址:https://www.cnblogs.com/lyqlyqlyq/p/11641887.html
Copyright © 2020-2023  润新知