Nginx+Keepalived

Nginx+Keepalived

  • 1. 架构图
  • 1. Web服务器
  • 2. Keep服务器
    • 2.1 Nginx配置文件
    • 2.2 Keepalived配置文件
      • 2.2.1 Keep-01
      • 2.2.2 Keep-01
    • 2.3 健康检查脚本
    • 2.4 keepalived切换脚本
  • 3 启动并测试

1. 架构图

Nginx+Keepalived_第1张图片

Hostname IPaddress
keep-01 192.168.31.17
keep-02 192.168.31.27
web-01 192.168.31.37
web-02 192.168.31.47
vip 192.168.31.100

1. Web服务器

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y nginx
echo "host in `hostname -I`" > /usr/share/nginx/html/index.html
systemctl enable --now nginx

此时在keep-01上可以访问到这2个web的页面
Nginx+Keepalived_第2张图片

2. Keep服务器

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y nginx keepalived

2.1 Nginx配置文件

cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    upstream nginx {
       server 192.168.31.37:80;
       server 192.168.31.47:80;
    }
    server {
        listen 80;
        server_name 192.168.31.100;
        location / {
           proxy_pass http://nginx;
        }
    }
}

2.2 Keepalived配置文件

2.2.1 Keep-01

cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id Ka1sr06
}
vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx.sh"
   interval 1
   weight -30
   fall 3
   rise 5
   timeout 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 61
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.100 dev eth0 label vip1
    }
    notify_master "/etc/keepalived/keepalived_notify.sh master"
    notify_backup "/etc/keepalived/keepalived_notify.sh backup"
    notify_fault "/etc/keepalived/keepalived_notify.sh fault"
    track_script {
        check_nginx
    }
}

2.2.2 Keep-01

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id Ka1sr06
}
vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx.sh"
   interval 5
   weight -30
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 61
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.31.100 dev eth0 label vip1
    }
    notify_master "systemctl restart nginx"
    notify_backup "systemctl restart nginx"
    track_script {
        check_nginx
    }
}

2.3 健康检查脚本

cat /etc/keepalived/check_nginx.sh 
#!/bin/bash
count=$(ps -ef |grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    systemctl restart nginx
    count=$(ps -ef |grep nginx |egrep -cv "grep|$$")
    if [ "$count" -eq 0 ];then
    exit 1
else
    exit 0
fi
fi

2.4 keepalived切换脚本

cat /etc/keepalived/keepalived_notify.sh
#!/bin/bash
#
notify() {
  mailsubject="$(hostname) to be $1, vip 转移"
  mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
  echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
  notify master
  ;;
backup)
  notify backup
  ;;
fault)
  notify fault
  ;;
*)
  echo "Usage: $(basename $0) {master|backup|fault}"
  exit 1
  ;;
esac

3 启动并测试

systemctl enable --now keepalived nginx

Nginx+Keepalived_第3张图片

你可能感兴趣的:(Linux,linux,rhce)