This article Description: nginx achieves high Availability Based on keepalived, proxy web server based on its own proxy function, and implements load balancing.
Lab environment: redhat5.8
Experiment topology: Two nginx + keepalived servers and two apache servers are used to build a simple high-availability cluster.
650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/02201631Z-0.png "/>
Ip Address Allocation:
- nginx1.weiyang.org 192.168.3.22 nginx1
- nginx2.weiyang.org 192.168.3.33 nginx2
- apache1.weiyang.org 192.168.3.205 apache205
- apache2.weiyang.org 192.168.3.206 apache206
I. installation and configuration of the experiment environment
It works from 205 and it works from 206, respectively. This helps us determine whether Server Load balancer has been implemented. Note: This is just a simple experiment in a virtual machine, files must be consistent in the actual production environment)
Ii. Install keepalived on the front-end nginx
Keepalived: http://www.keepalived.org/download.html#here I use the latest 1.2.7.
Note: The Development package group "Development Tools" and "Development Libraries" has been installed in advance by yum.
<1> unzip the downloaded package and install it:
- # Tar xvf keepalived-1.2.7.tar.gz
- # Cd keepalived-1.2.7
- #./Configure -- prefix =/usr/local/keepalived # specify the keepalived installation directory
- # Make & make install
<2> after the installation is complete, provide the configuration file and service script for keepalived.
- # Mkdir/etc/keepalived # create a configuration file directory for keepalived. The default configuration file is read from/etc/keepalived.
- # Cp/usr/local/keepalived/sbin/keepalived/usr/sbin/# copy the Binary command of keepalived
- # Cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/init. d/# provide the Startup Script
- # Cp/usr/local/keepalived/etc/keepalived. conf/etc/keepalived/# Master configuration file of keepalived
<3> edit the/etc/keepalived. conf file. The modification content is as follows:
- ! Configuration File for keepalived
-
- Global_defs {
- Notification_email {
- Acassen@firewall.loc
- Failover@firewall.loc
- Sysadmin@firewall.loc # these three are mailbox addresses, meaning to send mail to these three mailboxes in case of a fault
- }
- Icationication_email_from Alexandre.Cassen@firewall.loc # Which mailbox to send mail to the three mailboxes defined above
- Smtp_server 127.0.0.1
- Smtp_connect_timeout 30 # smtp connection timeout
- Router_id LVS_DEVEL # server identifier
- }
-
- Vrrp_instance VI_1 {
- State MASTER # master server, slave server is set to BACKUP; when the MASTER node fails, it is automatically transferred to the master node as the MASTER node. When the master node recovers, it is automatically transferred to the defined MASTER node.
- Interface eth0 # specify the network card from which VRRP information is sent
- Virtual_router_id 51 # vro group id. The master and slave nodes must be in the same group.
- Priority 100 # priority, from 80
- Advert_int 1
- Authentication {
- Auth_type PASS # authentication method
- Auth_pass 1111 # Authentication Key
- }
- Virtual_ipaddress {
- 192.168.3.18 # the ip address of VRRP, that is, the ip address opened to the client, that is, the vip address.
- }
- }
- Configuration ends here, but not here;
<4> Configure the parameter in the same way as the parameter in another method. You only need to change priority 100 to 80, as long as it is smaller than 100 defined by the master );
Start the keepalived service on both servers: service keepalived start
<5> test the keepalived effect.
Run the following command on nginx1:
- [Root @ nginx1 ~] # Ip addr
- 1: lo: <LOOPBACK, UP, LOWER_UP> mtu 16436 qdisc noqueue
- Link/loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
- Inet 127.0.0.1/8 scope host lo
- Inet6: 1/128 scope host
- Valid_lft forever preferred_lft forever
- 2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- Link/ether 00: 0c: 29: e7: cb: a0 brd ff: ff
- Inet 192.168.3.22/24 brd 192.168.2.255 scope global eth0
- Inet 192.168.3.18/32 scope global eth0
- Inet6 fe80: 20c: 29ff: fee7: cba0/64 scope link
- Valid_lft forever preferred_lft forever
- 3: sit0: <NOARP> mtu 1480 qdisc noop
- Link/sit 0.0.0.0 brd 0.0.0.0
- ### The vip is bound to nginx1. Now we have stopped keepalived on nginx1.
- # Service keepalived stop
- ### Run the ip addr command on nginx2. the following result is displayed:
- [Root @ nginx2 ~] # Ip addr
- 1: lo: <LOOPBACK, UP, LOWER_UP> mtu 16436 qdisc noqueue
- Link/loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
- Inet 127.0.0.1/8 scope host lo
- Inet6: 1/128 scope host
- Valid_lft forever preferred_lft forever
- 2: eth0: <BROADCAST, MULTICAST, UP, LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- Link/ether 00: 0c: 29: 4f: 9a: 9d brd ff: ff
- Inet 192.168.3.33/24 brd 192.168.2.255 scope global eth0
- Inet 192.168.3.18/32 scope global eth0
- Inet6 fe80: 20c: 29ff: fe4f: 9a9d/64 scope link
- Valid_lft forever preferred_lft forever
- 3: sit0: <NOARP> mtu 1480 qdisc noop
- Link/sit 0.0.0.0 brd 0.0.0.0
- So far, nginx high availability is achieved based on keepalived ..
III. The following is a reverse proxy for nginx to build a simple high-availability server Load balancer cluster:
Make the same changes on the front-end two nginx servers:
Edit the nginx configuration file:
- # Vim/etc/nginx. conf
- User nginx; # the user and user group to use to run nginx. Before installing nginx, you must create this group and user.
- Worker_processes 2; # Start several processes
- Events {
- Use epoll; # nginx Working Mechanism
- Worker_connections 1024; # maximum number of connections of a single process. This is the default value and can be changed to 65535.
- }
-
-
- Http {
- Include mime. types;
- Default_type application/octet-stream;
- Sendfile on;
- # Tcp_nopush on;
-
- # Keepalive_timeout 0;
- Keepalive_timeout 65; # nginx persistent connection duration
-
- # Gzip on;
- Upstream weiyang {# define the cluster of the backend web Server
- Server 192.168.3.205: 80 max_fails = 3 fail_timeout = 10 s; # the ip address of the backend server, and the maximum number of errors and the length of time
- Server 192.168.3.206: 80 max_fails = 3 fail_timeout = 10 s;
- }
-
-
- Server {
- Listen 80;
- Server_name 192.168.3.18; # IP address of vrrp
- Location /{
- Root html;
- Index index.html;
- Proxy_pass http: // weiyang/; # Which backend performs reverse proxy?
- Proxy_redirect off;
- Proxy_set_header X-Real-IP $ remote_addr;
- Proxy_set_header X-Forwarded-For Proxy_add_x_forwarded_for;
- }
- Location/nginx {# nginx status page
- Access_log off;
- Stub_status on;
- }
-
- # Error_page 404/404 .html;
-
- # Redirect server error pages to the static page/50x.html
- #
- Error_page 500 502 503 x.html;
- Location =/50x.html {
- Root html;
- }
- }
- }
-
- # Restart the nginx service after the configuration file is changed for nginx, and enable the httpd service for the backend two web servers at the same time
-
- # Service nginx restart
- # Service httpd start
4. Implement nginx + keepalived + apache to build a high-availability cluster. We also need to modify the following information:
Imagine that if the nginx service on the nginx1 server fails, but the keepalived service runs normally, a single point of failure will still be formed;
To check whether the nginx service is running normally in real time, write a small script to check its running status.
Script implementation idea: When the nginx service fails, we will directly kill the keepalived process to achieve failover:
The script is as follows:
- # Vim ngpid. sh
- #! /Bin/bash
- N = 'ps-C nginx -- no-header | wc-l' # check whether there is an nginx process, perform row Calculation on it, and assign the calculation value to Variable N
- If [$ N-eq 0]; then # if statement, if N is not 0, the script is executed. if N is 0, restart the nginx service, sleeping for 2 seconds,
- Service nginx start
- Sleep 2
- If ['ps-C nginx -- no-header | wc-l'-eq 0]; then # judge again, if it is still 0,
- Killall keepalived # Kill all keepalived processes and implement failover.
- Fi
- Fi
- # Chmod + x ng_pid.sh
-
- Put the script under/root/bash/and write it into the task plan. The script is executed every hour, as shown below:
- Echo-e '* 1 ***/root/bash/ngpid. Sh'>/etc/crontab
5. Access Test
Enter the following address in the local host Browser:
Http: // 192.168.3.18 check whether the result is polling,
At this time, nginx1 is a highly available master: Stop the nginx service. Wait for 3 seconds ..
Visit http: // 192.168.3.18 again to check whether the page shows normal polling
Nginx-based high-availability server Load balancer has been configured successfully. For more information, see ..
This article from the "when the soul of the body" blog, please be sure to keep this source http://19920601.blog.51cto.com/6110108/1154392