This article is based on other blogs + your own practice, not just paste and copy.Most of them are their own content, if you find infringement please inform
keepalived+nginx dual-machine warm-up
1 Configure VIP
2 Install keepalived
3 Configure keepalived
4 Tests
Server preparation
172.16.0.91 master
172.16.0.93 Ready
VIP: 172.16.0.16
1.1 Configure VIP
1.1.1 Method 1
This configuration will fail after a server or network restart
Increase: ifconfig eth0:0 192.168.0.99 netmask 255.255.255.0
Remove: ip addr del 192.168.0.99 dev eth0:0
1.1.2 Method 2
Modify the configuration file so that the configuration takes effect automatically after the host restarts
vi /etc/sysconfig/network-scripts/ifcfg-eth0:0 E=eth0:0 TYPE=Ethernet ONBOOT=yes BOOTPROTO=static DNS1=192.168.0.255 IPADDR=192.168.0.99 NETMASK=255.255.255.0 GETWAY=192.168.0.255
Effective after network service restart after configuration
service network restart
1.2 After configuring VIP
ifconfig view
Result
eth0 Link encap:Ethernet HWaddr 52:54:00:07:52:0A inet addr:172.16.0.91 Bcast:172.16.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:21108603 errors:0 dropped:0 overruns:0 frame:0 TX packets:19325869 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5067698587 (4.7 GiB) TX bytes:3457653139 (3.2 GiB) eth0:1 Link encap:Ethernet HWaddr 52:54:00:07:52:0A inet addr:172.16.0.16 Bcast:172.16.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:722997 errors:0 dropped:0 overruns:0 frame:0 TX packets:722997 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:352078592 (335.7 MiB) TX bytes:352078592 (335.7 MiB)
Configuration Successful
/I am a dividing line*/
2.1 Install keepalived
This project uses keepalived-1.2.21.tar.gz
2.2 Unzip keepalived-1.2.21.tar.gz
2.3 ./configure
2.4 make & make install
2.5 Replication and Creation
cp /usr/local/sbin/keepalived /usr/sbin/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ mkdir /etc/keepalived cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
/I am a dividing line*/
3.1 172.16.0.91 keepalived Main Profile
! Configuration File for keepalived
global_defs { notification_email { #acassen@firewall.loc #failover@firewall.loc #sysadmin@firewall.loc } #notification_email_from Alexandre.Cassen@firewall.loc #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_http_port { script "/usr/local/nginx/sbin/check_nginx.sh" #script to monitor interval 1 #Execute script every second weight -20 #Weight minus 20 } vrrp_instance VI_1 { state MASTER #Host interface eth0 #Network Card where vip is configured virtual_router_id 51 #default priority 100 #weight advert_int 1 #default authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.0.16 #Important VIP } track_script { chk_http_port } }
Bring it to use in Baidu
State specifies the state in which instances are initialized, and if they are all backup s, the value of priority determines who is master.Priority should be 50% different
interface is an instance-bound network card, which provides services to the outside world
track_interface sets up additional monitoring, and if any of the network cards in it fails, it will enter the FAULT state.
The address at which mcast_src_ip sends multicast packets, if not set, defaults to the primary IP of the bound network card.
Virtual_router_id VRID tag (0-255)
Priority has a high priority of master, preferably with a difference of more than 50
advert_int check interval, default 1s
virtual_ipaddress specifies VIPs, which are added when switching to master and deleted when switching to backup.These VIPs can be seen through ip addr show.
lvs_sync_daemon_interface lvs syncd-bound network card, similar to sending a heartbeat
Authentication This section sets authentication
Auh_type authentication method, supporting PASS and HA (said HA has problems)
auth_pass authentication password
The nopreempt setting is not preemptive, note that this setting can only be set on backup state hosts, and that the priority of this host must be higher than that of other hosts
reempt_delay preemption delay, default 5 minutes
3.2 172.16.0.93 Standby Profile Not Interpreted
! Configuration File for keepalived
global_defs { notification_email { #acassen@firewall.loc #failover@firewall.loc #sysadmin@firewall.loc } #notification_email_from Alexandre.Cassen@firewall.loc #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_http_port { script "/usr/local/nginx/sbin/check_nginx.sh" interval 1 weight -2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port } virtual_ipaddress { 172.16.0.16 } }
3.3 check_nginx.sh script content
Monitor if nginx is working, keep alived service shuts down if nginx has no process
#!/bin/bash nginxpid=`ps -C nginx --no-header | wc -l` if [ $nginxpid -eq 0 ]; then service keepalived stop echo "nginxpid is ${nginxpid}" fi
3.4 Startup
Main standby startup
service keepalived start
/I am a dividing line*/
4.1 View the keepalived log
172.16.0.92 main Jun 21 10:33:27 cloud0 Keepalived_vrrp[27866]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 10:33:32 cloud0 Keepalived_vrrp[27866]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 10:33:32 cloud0 Keepalived_vrrp[27866]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.16.0.16 Jun 21 10:33:32 cloud0 Keepalived_vrrp[27866]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 10:33:32 cloud0 Keepalived_vrrp[27866]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 10:33:32 cloud0 Keepalived_vrrp[27866]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 10:33:32 cloud0 Keepalived_vrrp[27866]: Sending gratuitous ARP on eth0 for 172.16.0.16
4.2 Accessing nginx http://172.16.0.16 Show 91 Addresses
4.3 Execute action. /nginx-s stop
Backup Switch
172.16.0.93 keep alived log
Jun 21 17:41:20 cloud2 Keepalived_vrrp[5502]: VRRP_Instance(VI_1) Transition to MASTER STATE Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: VRRP_Instance(VI_1) Entering MASTER STATE Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: VRRP_Instance(VI_1) setting protocol VIPs. Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.16.0.16 Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:21 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:21 cloud2 Keepalived_healthcheckers[5501]: Netlink reflector reports IP 172.16.0.16 added Jun 21 17:41:26 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:26 cloud2 Keepalived_vrrp[5502]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 172.16.0.16 Jun 21 17:41:26 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:26 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:26 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.16 Jun 21 17:41:26 cloud2 Keepalived_vrrp[5502]: Sending gratuitous ARP on eth0 for 172.16.0.1