Article Directory
[ Hide ]
This article describes the use of simple keepalived software, to achieve the nginx high availability servers, namely the failure to achieve automatic switching. Assuming you have installed nginx , following the installation and use of keepalived.
keepalived installation
- yum install openssl-devel
- cd /tmp
- wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
- tar xzf keepalived-1.2.2.tar.gz
- cd keepalived-1.2.2
- ./configure
- make && make install
- cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
- cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
- chmod +x /etc/init.d/keepalived
- chkconfig --add keepalived
- chkconfig keepalived on
- mkdir /etc/keepalived
- ln -s /usr/local/sbin/keepalived /usr/sbin/
keepalived configuration
More detailed instructions can be executed keepalived profile man keepalived.conf view.
We assume that the primary server IP: 192.168.1.103, from the server ip: 192.168.1.101 virtual ip: 192.168.1.110
below to keepalived master server to configure:
- we /etc/keepalived/keepalived.conf
- 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_script chk_http_port {
- script "/opt/nginx_pid.sh"
- interval 2
- weight 2
- }
- vrrp_instance VI_1 {
- state MASTER ############ auxiliary as BACKUP
- interface eth0
- virtual_router_id 51
- mcast_src_ip 192.168.1.103
- priority 102 ########### weight higher than back
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- Monitor the implementation of chk_http_port ### service
- }
- virtual_ipaddress {
- 192.168.1.110
- }
- }
From the server:
- 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_script chk_http_port {
- script "/opt/nginx_pid.sh"
- interval 2
- weight 2
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 51
- mcast_src_ip 192.168.1.101
- priority 101 ########## weights lower than the master. .
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- Monitor the implementation of chk_http_port ### service
- }
- virtual_ipaddress {
- 192.168.1.110
- }
- }
It was established after the nginx server monitoring scripts from the master:
- we /opt/nginx_pid.sh
- #!/bin/bash
- A=`ps -C nginx --no-header |wc -l`
- if [ $A -eq 0 ];then
- /usr/local/nginx/sbin/nginx
- sleep 3
- if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
- killall keepalived
- be
- be
Then start keepalived were the primary server:
- service keepalived start
keepalived test
We execute the command ip a server on the primary display as follows:
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- link / ether 00: 0c: 29: aa: a1: e4 br ff: ff: ff: ff: ff: ff
- inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
- inet 192.168.1.110/32 scope global eth0
Prove the primary server is already bound virtual ip 192.168.1.110
in the implementation of the server from the command ip a, shown below:
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- link / ether 00: 0c: 29: 2b: 94: 3b brd ff: ff: ff: ff: ff: ff
- inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
Display shows that from the server is not bound vip 192.168.1.110, only native real ip192.168.1.101
Let's stop the main server nginx process, look at ip binding information:
when the main server:
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- link / ether 00: 0c: 29: aa: a1: e4 br ff: ff: ff: ff: ff: ff
- inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
From the server:
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- link / ether 00: 0c: 29: 2b: 94: 3b brd ff: ff: ff: ff: ff: ff
- inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
- inet 192.168.1.110/32 scope global eth0
Thus vip been pointing from the server.