Environment: Main: Linux-node1 110.0.0.137: Linux-node2 110.0.0.138 vip:110.0.0.120 nginx Installation: # RPM-IVH http://nginx.org/packages/ centos/6/noarch/rpms/nginx-release-centos-6-0.el6.ngx.noarch.rpm# Yum install nginx 1. Installation keepalived <pre>< code># yum-y install kernel-devel make gcc openssl-devel libnl* popt*
# yum Install-y OpenSSL Openssl-devel
# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz# tar zxf keepalived-1.2.2.tar.gz # CD keepalived-1.2.2#./configure--prefix=/usr/local/keepalived# make && make install# cp/usr/local/keepalived/ etc/rc.d/init.d/keepalived/etc/init.d/# cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig//bin/cp/usr/local/etc/keepalived/keepalived.conf/etc/keepalived/# chmod +x/etc/init.d/keepalived# mkdir/etc/keepalived#/bin/cp/usr/local/sbin/keepalived/usr/sbin/# ll/usr/sbin/ </code></pre> 2. Configuring keepalived2.1. Configuring the main Nginx (LINUX-NODE1) [[email protected ] ~]# cat/etc/keepalived/keepalived.conf ! Configuration File for keepalived 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 2weight 2} vrrp_instance vi_1 { State master Interface eth0 & nbsp virtual_router_id 51 MCAST_SRC_IP 110.0.0.137 Priority 100 Advert_int 1 &nbs P Authentication { Auth_type pass Auth_pass 1111 } Track_script {chk_http_port } virtual_ipaddress { 110.0.0.120 }} 2.2 configuration from Nginx (LINUX-NODE2) [[email protected] keepalived]# cat/etc/keepalived/keepalived.conf ! Configuration File for keepalived 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 2weight 2} vrrp_instance vi_1 { State backup Interface eth0 & nbsp virtual_router_id 51 MCAST_SRC_IP 110.0.0.138 Priority 99 Advert_int 1   ; Authentication { Auth_type pass Auth_pass 1111 } Track_script {chk_http_port } virtual_ipaddress { 110.0.0.120 }} 3. Writing nginx Check scripts (required for Master and standby): #!/bin/basha= ' ps-c nginx--no-header |wc-l ' If [$A-eq 0];then/etc/init.d/nginx start sleep 3if [' Ps-c ngi NX--no-header |wc-l '-eq 0];then/etc/init.d/keepalived stopfifi 4. Start Nginx and Keepalived/etc/init.d/nginx start/ Etc/init.d/keepalived start 5. Check that the virtualized IP is bound to a successful primary node: You can see that the VIP 110.0.0.120 is already bound to the main Nginx server. 6. Test: Stop nginx#/etc/init.d/nginx stop found the main nginx can not stop, because/opt/nginx_pid.sh will check the Nginx state, if Nginx stopped, Then the script will automatically start Nginx. If Nginx fails to start, it will stop keepalived immediately, transfer VIP to backup nginx We set the Nginx configuration file wrong, then stop Nginx, the main nginx will not start. Check the main nginx machine IP status: Check the backup Nginx host status: See VIP has been transferred to the backup host.
From for notes (Wiz)
Nginx uses keepalived for high availability