centos實現三個節點高可用

centos實現三個節點高可用

使用的資源爲keepalived和nginxphp

高可用主機IP地址
192.168.136.131
192.168.136.133
192.168.136.134html

nginx負載均衡配置
192.168.136.131
192.168.136.133
192.168.136.134
三臺主機的80端口對應nginx

192.168.136.131:8080
192.168.136.131:8083
192.168.136.134:8080
192.168.136.134:8084
192.168.136.133:8080
192.168.136.133:8081

nginx的Web服務器
192.168.136.131的8080、8083
192.168.136.134的8080、8084
192.168.136.133的8080、8081vim

一、每一個節點安裝nginx和安裝keepalived

1.1 安裝nginx 1.16.1 ,參考
http://www.javashuo.com/article/p-xvbdwzqu-er.html
1.2 安裝keepalivedcentos

yum -y install keepalived
啓動keepalived服務器

systemctl start keepalived
systemctl enable keepalived

二、修改keepalived配置

2.1 修改master節點的/etc/keepalived/keepalived.conf配置文件信息負載均衡

! Configuration File for keepalived
vrrp_script check_nginx_alive {
    script "/usr/bin/check_nginx_alive.sh"
    interval 3
    weight -10
}

global_defs {
   router_id lbs_nginx
}

vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.125
    }
    track_script {
        check_nginx_alive
    }

}


virtual_server 192.168.136.125 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
}

2.2 增長master節點的 執行腳本 /usr/bin/check_nginx_alive.sh,不管主節點仍是備節點都須要增長該文件tcp

/usr/bin/check_nginx_alive.sh

#!/bin/sh
 
PATH=/bin:/sbin:/usr/bin:/usr/sbin
 
A=`ps -C nginx --no-header |wc -l`
 
if [ $A -eq 0 ]
   then
     echo 'nginx server is died'
     killall keepalived
fi

2.3 修改backup節點的/etc/keepalived/keepalived.conf配置文件信息
注意,不一樣的備節點的優先級不同, priority 20oop

! Configuration File for keepalived
vrrp_script check_nginx_alive {
    script "/usr/bin/check_nginx_alive.sh"
    interval 3
    weight -10
}

global_defs {
   router_id lbs_nginx
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens32
    virtual_router_id 51
    priority 20
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.125
    }
    track_script {
        check_nginx_alive
    }

}


virtual_server 192.168.136.125 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
}

三、 增長nginx的配置,

3.1 修改nginx的配置文件以下網站

default.conf 8080端口
default_8084.conf 8084端口
default_lbs.conf 負載均衡配置

server {
    listen       8080;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

3.2 負載均衡端口配置,負載均很名字和要一致,這裏是nginx_lbs

upstream nginx_lbs { 
        server 192.168.136.131:8080 weight=1;
        server 192.168.136.131:8083 weight=1;
        server 192.168.136.134:8080 weight=1;
        server 192.168.136.134:8084 weight=1;
        server 192.168.136.133:8080 weight=2;
        server 192.168.136.133:8081 weight=2;
  }


server {
    listen       80;
    server_name  localhost;

    client_max_body_size 50m;
    client_body_buffer_size 256k;
    location / {
       proxy_pass        http://nginx_lbs;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_set_header Host $host;
    }
}

四、啓動keepalived

4.1 確認nginx的keepalived是否開放
通過查看能夠看到131和134的 nginx.conf中的 keepalive_timeout 65;,至關於打開了keepalive

4.2 啓動keepalived

啓動Master 192.168.136.131和Backup 192.168.136.134 和 Backup 192.168.136.133的 Keepalived節點
systemctl start keepalived

五、查看keepaliced是否已經放開

ip addr 看到地址中是否有浮動地址

http://192.168.136.125能夠正常訪問了,能夠將每一個網站下的index.html文件增長相應標識

六、異常狀況查看

6.1 每臺主機只能訪問本主機全部網站和其餘主機個別網站,後來發現緣由爲,防火牆未打開
修改iptables後正常,
命令爲:

vim /etc/sysconfig/iptables 
        添加8080和8083端口 
            -A INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT 
            -A INPUT -m state –state NEW -m tcp -p tcp –dport 8083 -j ACCEPT
        啓動IPTABLES
            systemctl enable iptables.service
            systemctl start iptables.service
        #重啓防火牆使配置文件生效 
            systemctl restart iptables.service

6.2 怎麼確認目前的通訊信息正常
查看網卡接收的信息
安裝tcpdump
yum install -y tcpdump
查看端口vrrp信息
tcpdump -i ens32 vrrp -n
正常狀況下,只有一個IP會出如今信息中

6.3 查看keepalived的運行日誌
tail -f /var/log/messages

6.4 tcpdump命令看到多個IP的信息,而且主備keepalived服務器上的虛擬IP都增長上了
通過覈實緣由keepalived須要單獨的端口進行通訊,目前使用112
經過iptables增長後正常
-A INPUT -p 112 -j ACCEPT

6.5 /etc/keepalived/keepalived.conf 設置中不能增長real_server這個參數,增長real_server後報錯,估計目前使用廣播和組播進行確認,不須要設置具體的IP地址

估計是哪一個版本開始就不用設置了或者換了參數了

相關文章
相關標籤/搜索