centos7 nginx双主+keepalived

1、安装

server1、server2分别使用yum安装nginx和keepalived

yum install nginx

yum install keepalived


server3解压tomcat(apache-tomcat-7.0.75.tar.gz)


2、配置

2.1、server3配置:

具体配置不详说,单台机器多tomcat实例,分别配置三个实例端口为8080、8081、8082


页面index.html输入内容分别为hello 8080、hello 8081、hello 8082


2.2、server1、server2配置nginx:


# vi /etc/nginx/nginx.conf

# 在server {}修改location为下面的状态,在http {}添加upstream的内容


http {


    server {

        location / {

        proxy_pass http://test-pa.com;

        proxy_set_header HOST $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

    }


upstream test-pa.com {

  server 192.168.11.175:8080;

  server 192.168.11.175:8081;

  server 192.168.11.175:8082;

}


2.3、server1配置keepalived:

# vi /etc/keepalived/keepalived.conf


! Configuration File for keepalived


global_defs {

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}


vrrp_script chk_http_port {


   script "/etc/keepalived/scripts/check_nginx.sh"

   interval 2

   weight 2

}


vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

       track_script {

       chk_http_port

    }

    virtual_ipaddress {

        192.168.11.181

    }

}


vrrp_instance VI_2 {

    state BACKUP

    interface eth0

    virtual_router_id 52

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

       track_script {

       chk_http_port

    }

    virtual_ipaddress {

        192.168.11.182

    }

}


# mkdir /etc/keepalived/scripts 

# vi /etc/keepalived/scripts/check_nginx.sh

#!/bin/bash

d=`date --date today +%Y%m%d_%H:%M:%S`

n=`ps -C nginx --no-heading|wc -l`

if [ $n -eq 0 ]; then

        echo "`date "+%Y-%m-%d--%H:%M:%S"` nginx stop" >> /var/log/check_ng.log

        /usr/sbin/nginx

        n2=`ps -C nginx --no-heading|wc -l`

        if [ $n2 -eq "0"  ]; then

                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log

                systemctl stop  keepalived.service

        fi

fi


2.4、server2配置keepalived:

# vi /etc/keepalived/keepalived.conf 


! Configuration File for keepalived


global_defs {

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}


vrrp_script chk_http_port {


   script "/etc/keepalived/scripts/check_nginx.sh"

   interval 2

   weight 2

}


vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

       track_script {

       chk_http_port

    }

    virtual_ipaddress {

        192.168.11.181

    }

}


vrrp_instance VI_2 {

    state MASTER

    interface eth0

    virtual_router_id 52

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

       track_script {

       chk_http_port

    }

    virtual_ipaddress {

        192.168.11.182

    }

}


# mkdir /etc/keepalived/scripts 

# vi /etc/keepalived/scripts/check_nginx.sh

#!/bin/bash

d=`date --date today +%Y%m%d_%H:%M:%S`

n=`ps -C nginx --no-heading|wc -l`

if [ $n -eq 0 ]; then

        echo "`date "+%Y-%m-%d--%H:%M:%S"` nginx stop" >> /var/log/check_ng.log

        /usr/sbin/nginx

        n2=`ps -C nginx --no-heading|wc -l`

        if [ $n2 -eq "0"  ]; then

                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log

                systemctl stop  keepalived.service

        fi

fi


3、测试

3.1、server1:

# curl http://192.168.11.181

轮训返回hello 8080、hello 8081、hello 8082


停止nginx服务,会重新启动nginx服务,如果nginx服务启动不了,则关闭keepalived服务,关闭keepalived服务,则VIP消失。


3.2、server2:

# curl http://192.168.11.182

轮训返回hello 8080、hello 8081、hello 8082


停止nginx服务,会重新启动nginx服务,如果nginx服务启动不了,则关闭keepalived服务,关闭keepalived服务,则VIP消失。


4、注意

如果环境安装方式不一样,配置也要依照实际情况做相应的修改,因为启动服务的脚本、配置文件的路径都发生了改变。


参考:

https://blog.csdn.net/lexang1/article/details/52386909

猜你喜欢

转载自blog.51cto.com/xiaoxiaozhou/2161828