前言:
本文是在docker上安装四台centos下做测试,两台nginx服务器做负载,两台nginx服务器做后台应用服务器,keepalived安装在nginx负载上做主从切换。(nginx负载服务器的高可用。方式一:两台服务器(始终只有一台在用,另外一台处于待命状态;方式二:两台都在用,但是要绑定两个公网IP),个人学习了方式一并实践了一下。
最终实现:nginx-proxy-server1下的nginx关闭了,自动切换到nginx-proxy-server2上做web服务器的负载服务器使用
服务器 | IP |
nginx-web5 | 172.17.0.4 |
nginx-web6 | 172.17.0.5 |
nginx-proxy-server1 | 172.17.0.2 |
nginx-proxy-server2 | 172.17.0.3 |
虚拟IP(漂移IP) | 172.17.0.18 |
1:NGINX的安装(分别在nginx-web5,nginx-web6,nginx-proxy-server1,nginx-proxy-server2)下安装nginx
yum update #更新一下系统补丁 yum install -y gcc gcc-c++ automake make libpcre3-dev libssl-dev openssl libnl libnl-3 libnfnetlink-devel openssl-devel
cd /data/software
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget http://nginx.org/download/nginx-1.13.0.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
tar -zxvf pcre-8.40.tar.gz
tar -zxvf nginx-1.13.0.tar.gz
cd nginx-1.13.0
mkdir -p /data/service/nginx
./configure --prefix=/data/service/nginx/ --with-pcre=/data/software/pcre-8.40 --with-zlib=/data/software/zlib-1.2.11 --with-openssl=/usr/lib64/openssl
make && make install
启动nginx /data/service/nginx/sbin/nginx
2:NGINX(WEB配置)
vi /data/service/nginx/html/index (nginx-web5) <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1> 172.17.0.4 ( nginx-web5 ) </h1> </body> </html> vi /data/service/nginx/html/index (nginx-web6) <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1> 172.17.0.5 ( nginx-web6 ) </h1> </body> </html>
3:NGINX(proxy配置)
vi /data/service/nginx/conf/nginx.conf (nginx-proxy-server1:172.17.0.2)
在http{
upstream testHA {
server 172.17.0.4;
server 172.17.0.5;
}
server {
location / {
proxy_pass http://testHA;
}
}
}
4:keepalived安装
yum -y install libnl libnl-devel
yum install keepalived
5:keepalived配置
master(主): vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { # notification_email { # dxh20012012001@sina.com # } # notification_email_from dengxiaohui2001@163.com # smtp_server smtp.163.com # smtp_connect_timeout 30 router_id LVS_DEVEL # vrrp_skip_check_adv_addr # vrrp_strict # vrrp_garp_interval 0 # vrrp_gna_interval 0 } vrrp_script chk_nginx { script "/data/service/keepalived/scripts/check_nginx.sh" #script "[[ -e /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight -2 rise 3 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 101 advert_int 1 mcast_src_ip 172.10.0.2 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.17.0.18/32 } track_script { chk_nginx } }
slave:
脚本:/data/service/keepalived/scripts/check_nginx.sh
#!/bin/bash nginxPidNum=`ps -C nginx --no-header |wc -l` keepalivedPidNum=`ps -C keepalived --no-header |wc -l` if [ $nginxPidNum -eq 0 ]; then if [ $keepalivedPidNum -ne 0 ]; then killall keepalived fi elif [ $keepalivedPidNum -eq 0 ]; then /usr/sbin/keepalived fi
开启 nginx-proxy-server1和nginx-proxy-server2上的nginx和keepalived
/data/service/nginx/sbin/nginx
/usr/sbin/keepalived
查看虚拟IP(172.17.0.18)是否绑定在nginx-proxy-server1上面
ip addr show
6:keepalived测试
在(nginx-proxy-server1)关闭掉nginx看ip是否漂移到nginx-proxy-server2上(ip addr show)
或者LINUX下直接访问:curl http://172.17.0.18 还是继续可以访问到web5,web6下的页面
参考文档: