This architecture is extremely suitable for flexible and stable environments.
Possible faults of Nginx server load balancer as a server:
1. Network faults such as loose server network cable
2. Server hardware faults
3. Nginx service dead
In the first two cases, Keepalived can play the role of HA. However, in the third case, there is no way, but it can be solved through SHELL monitoring.
1. Environment description CentOS-5.9 32-bit
II. Install Nginx server load balancer 1. Add the users running Nginx and the www and Nginx groups to store logs, and install gcc and other basic libraries to avoid libtool errors.
The code is as follows: |
Copy code |
Yum-y install gcc + gcc-c ++ openssl-devel Groupadd www Useradd-g www Mkdir-p/data/logs/ Chown-R www: www/data/logs/ |
2. Install Nginx
The code is as follows: |
Copy code |
Wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz Tar zxvf pcre-8.21.tar.gz Cd pcre-8.21.tar.gz ./Configure Make & make install Cd ../ Wget http://nginx.org/download/nginx-1.4.1.tar.gz Tar zxvf nginx-1.4.1.tar.gz Cd nginx-1.4.1.tar.gz ./Configure-user = www-group = www-prefix =/usr/local/nginx-with-http_stub_status_module-with-http_ssl_module Make & make install |
Configure Nginx:
The code is as follows: |
Copy code |
Vim/usr/local/nginx/conf/nginx. conf |
Add the following content
The code is as follows: |
Copy code |
Upstream backend { # Ip_hash; the Nginx server load balancer uses the ip_hash module to ensure that the accessed client always establishes a permanent connection (session persistence) with a backend Web service period. Here we use the default RR to facilitate testing. Server 192.168.222.132; Server 192.168.222.133; # two WEB servers } Server { Listen 80; Server_name www.mylb.com; Location /{ Root/var/www/html; Index. php index.htm index.html; Proxy_redirect off; Proxy_set_header Host $ host; Proxy_set_header X-Real-IP $ remote_addr; Proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for; Proxy_pass http: // backend; } } |
Run/usr/local/Nginx/sbin/nginx on the two nginx load balancers respectively.
3. Install Keeplived for HAwget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz of WEB and Nginx respectively
The code is as follows: |
Copy code |
Tar zxvf keepalived-1.2.7.tar.gz Cd keepalived-1.2.7.tar.gz ./Configure-prefix =/usr/local/keepalived Make & make install |
After the installation is successful, the service mode is set to enable and disable the service:
The code is as follows: |
Copy code |
Cp/usr/local/keepalived/sbin/keepalived/usr/bin/ Cp/usr/local/keepalived/etc/sysconfig Cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/init. d |
Set the Keepalived configuration files on the master and slave Nginx respectively. Configure the keepalived. conf file on the primary Nginx.
The code is as follows: |
Copy code |
Mkdir/etc/keepalived Cd/etc/keepalived Vim keepalived. conf Vrrp_instance VI_1 { State MASTER Interface eth0 Virtual_router_id 51 Priority100 Advert_int 1 Authentication { Auth_type PASS Auth_pass mylb } Virtual_ipaddress { 192.168.222.131 } } |
The keepalived. conf configuration file on the slave Nginx is as follows:
The code is as follows: |
Copy code |
Vrrp_instance VI_1 { State BACKUP Interface eth0 Virtual_router_id 51 Priority 99 Advert_int 1 Authentication { Auth_type PASS Auth_pass mylb } Virtual_ipaddress { 192.168.222.131 } } |
Start Keepalived on the two load balancers respectively:
The code is as follows: |
Copy code |
Service keepalived start |