Nginx+keepalived implements a load-balanced, highly available WEB Services cluster,Nginx as a load balancer,keepalived as highly available when one of the load balancers ( Nginx) can quickly switch to an alternate load balancer (nginx) in the event of a failure tomaintain business continuity.
1 , Server environment configuration and IP Distribution
Operating System: CentOS Release 6.7 (Final)
Nginx version:nginx/1.8.0
keepalived version:keepalived v1.2.13
IP Allocation Table for Nginx + keepalived server
Server name IP Role
DR1 192.168.171.10 provides load balancing
DR2 192.168.171.20 provides load balancing
VIP address of 192.168.171.100 website
RS1 192.168.171.30 provides web services
RS2 192.168.171.40 provides web services
2 , respectively, in DR1 and DR2 install Nginx on load balancer and related scripts
1), add the user group running nginxd www and nginx storage log location, and install gcc Base library (using yum installation)
Yum install-y make apr* autoconf automakecurl curl-devel gcc gcc-c++ gtk+-devel zlib-devel OpenSSL Openssl-develpcre-deve L GD kernel keyutils patch perl kernel-headers compat* cpp glibc libgomp libstdc++-develkeyutils-libs-devel Libsepol-deve L libselinux-devel krb5-devel libxpm* freetype freetype-devel freetype*fontconfig fontconfig-devel libjpeg*libpng* php- Common PHP-GD gettext gettext-devel ncurses* libtool* libxml2libxml2-devel patch policycoreutils Bison pcre pece-devel
Groupadd www # creating groups
Groupadd–g www www # create www user and join www group
mkdir–p/data/logs/# directory where logs are stored
Chown R www.www/data/logs/# Modify owner and owning group
2), download and install ngixn-1.8.0, download to /usr/local/src/ directory by default
Cd/usr/local/src
wget http://nginx.org/download/nginx-1.8.0.tar.gz # Download nginx
Tar zxvf nginx-1.8.0.tar.gz
CD nginx-1.8.0
./configure--prefix=/usr/local/nginx--with-http_realip_module--with-http_sub_module--with-http_gzip_static_ Module--with-http_stub_status_module--with-pcre--with-http_ssl_module
Make && make install
3), configuration nginx, the default configuration file is in the/usr/local/nginx/conf/nginx.conf
Vim/usr/local/nginx/conf/nginx.conf
User www www; # users to use
Worker_processes 2;
Error_log/usr/local/nginx/logs/nginx_error.log Crit;
Pid/usr/local/nginx/logs/nginx.pid;
Worker_rlimit_nofile 51200;
Events
{
Use Epoll;
Worker_connections 6000;
}
http
{
Include Mime.types;
Default_type Application/octet-stream;
Server_names_hash_bucket_size 3526;
Server_names_hash_max_size 4096;
Log_format combined_realip ' $remote _addr $http _x_forwarded_for[$time _local] '
' $host ' $request _uri "$status"
' "$http _referer" "$http _user_agent"; # format of the log
Sendfile on;
Tcp_nopush on;
Keepalive_timeout 30;
Client_header_timeout 3m;
Client_body_timeout 3m;
Send_timeout 3m;
Connection_pool_size 256;
Client_header_buffer_size 1k;
Large_client_header_buffers 8 4k;
Request_pool_size 4k;
Output_buffers 4 32k;
Postpone_output 1460;
Client_max_body_size 10m;
Client_body_buffer_size 256k;
Client_body_temp_path/usr/local/nginx/client_body_temp;
Proxy_temp_path/usr/local/nginx/proxy_temp;
Fastcgi_temp_path/usr/local/nginx/fastcgi_temp;
Fastcgi_intercept_errors on;
Tcp_nodelay on;
gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 8k;
Gzip_comp_level 5;
Gzip_http_version 1.1;
Gzip_types text/plain application/x-javascript text/css text/htmapplication/xml;
# Define a Load Balancer configuration module
Upstream MyServer {
Ip_hash; #测试的时候建议注释掉
Server 192.168.171.30:80 weight=1 max_fails=3 fail_timeout=20s;
Server 192.168.171.40:80 weight=1 max_fails=3 fail_timeout=20s;
}
server {
Listen 80;
server_name www.balichlb.org;
Index index.htm index.html;
Location/{
Proxy_pass http://MyServer;
Proxy_set_header Host $Host;
Proxy_next_upstream errortimeout http_500 http_502 http_504;
Proxy_read_timeout 10s;
Proxy_set_header x-real-ip$remote_addr;
Proxy_set_header x-forwarded-for$proxy_add_x_forwarded_for;
}
Access_log/data/lgos/access.log Combined_realip; # access to logs
}
}
Executed in two nginx(DR1 and DR2):/usr/local/nginx/sbin/nginx command to start nginx service, Then use the lsof–i:80 command to check (and of course you can write nginx startup scripts)
Vim/etc/init.d/nginx # Writing nginx startup, shutdown, script
#!/bin/bash
# Chkconfig:-30 21
# Description:http Service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
Nginx_sbin= "/usr/local/nginx/sbin/nginx"
nginx_conf= "/usr/local/nginx/conf/nginx.conf"
Nginx_pid= "/usr/local/nginx/logs/nginx.pid"
Retval=0
Prog= "Nginx"
Start () {
Echo-n $ "Starting $prog:"
Mkdir-p/dev/shm/nginx_temp
Daemon $NGINX _sbin-c $NGINX _conf
Retval=$?
Echo
Return $RETVAL
}
Stop () {
Echo-n $ "Stopping $prog:"
Killproc-p $NGINX _pid $NGINX _sbin-term
Rm-rf/dev/shm/nginx_temp
Retval=$?
Echo
Return $RETVAL
}
Reload () {
Echo-n $ "Reloading $prog:"
Killproc-p $NGINX _pid $NGINX _sbin-hup
Retval=$?
Echo
Return $RETVAL
}
Restart () {
Stop
Start
}
Configtest () {
$NGINX _sbin-c $NGINX _conf-t
return 0
}
Case "$" in
Start
Start
;;
Stop
Stop
;;
Reload
Reload
;;
Restart
Restart
;;
Configtest)
Configtest
;;
*)
echo $ "Usage: $0{start|stop|reload|restart|configtest}"
Retval=1
Esac
Exit $RETVAL
: Wq Save exit.
chmod 755/etc/init.d/nginx # Modify Permissions
Chkconfig--add Nginx # Add system Services list
/etc/init.d/nginx Start # launch nginx service
3 , installation keepalived , make it a web and nginx the HA (High availability)
Yum Install keepalived
1), keepalived configuration files, respectively, configured on the master and the Nginx , by default in the/etc/keepalived/keepalived.conf
Configure on The main nginx first :
Vim/etc/keepalived/keepalived.conf
! Configuration File for Keepalived
# Global Definition Section
Global_defs {
Notification_email {
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
router_id Lvs_devel
}
Vrrp_instance Vi_1 {
State Master # master server is Master, standby server is backup
Interface eth0
VIRTUAL_ROUTER_ID 51
MCAST_SRC_IP 192.168.171.10 # The IP address of the main nginx Service
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.171.100 # Set a virtual IP address, can be multiple, one line at a
}
}
The configuration of the alternate server row is basically the same, and the place to be modified is as follows:
State MASTER >> State BACKUP
Priority >> Priority # modified to less than the primary server
Mcast_src_ip 192.168.171.10 >>mcast_src_ip 192.168.171.20 # Alternate ngixn IP
Start the keepalived Service (Guthrie) on two load balancers, respectively
/etc/init.d/keepalived start
You can view the startup of keepalived through log information, at which point a VIP is bound to eth0:0 on the main nginx .
now Nginx can indeed load balance the backend web services, but if the Nginx service fails,the keepalived service is still running, unable to The VIP is transferred to the standby server. This can be accomplished using the following script:
vim/etc/keepalived/nginx_pid.sh
#!/bin/bash
While:
Do
Nginxpid= ' ps-c nginx--no-header |wc-l '
if[$nginxpid-eq 0];then
/etc/init.d/nginxstart
Sleep 3
if[$nginxpid-eq 0];then
/etc/init.d/keepalivedstop
Fi
Fi
Sleep 5
Done
put it in the background to run:nohup/bin/bash/etc/keepalived/nginx_pid.sh &
4 , analog failure
1), close The service of the main nginx, test whether the script can let the normal operation. Turn off The keepalived service between the switch is OK.
2), after the main nginx service repair, restart the keepalived service After the start, can re-take over the service.
3), directly disconnect the network, view the service.
This article is from the "Balich" blog, make sure to keep this source http://balich.blog.51cto.com/6641781/1716138
nginx+keepalived configuration of a high-availability Web cluster for CentOS Linux load Balancing