Apache installation and automatic startup under centos 7
keepalived installation tutorial
Install the above tutorial to install apache and keepalived on two machines respectively.
Configure keepalived for the first machine
The configuration file content is as follows:
Edit/etc/keepalived/keepalived.conf
# vi /etc/keepalived/keepalived.conf, add the following
! Configuration File for keepalived
global_defs {
notification_email {
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id linux01
}
vrrp_instance VI_1 { #Define an instance
state BACKUP #Set it to backup, and then use priority to control which one is promoted to primary
interface eth0
virtual_router_id 51
priority 100
nopreempt #Set this parameter on the one with high priority to make it easier after primary recovery
advert_int 1 #Can automatically take over
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.199 dev eth0 label eth0:0 #Use the label label here to enable eth0:0
}
}
virtual_server 10.0.0.199 80 { #Below is the definition of LVS
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 10.0.0.191 80 { #One of them is a local web service
weight 1
TCP_CHECK {
connect_timeout 3
}
}
real_server 10.0.0.192 80 { #Another web service
weight 1
TCP_CHECK {
connect_timeout 3
}
}
}
The keepalived file for the second machine is as follows
Edit/etc/keepalived/keepalived.conf
# vi /etc/keepalived/keepalived.conf, add the following
! Configuration File for keepalived
global_defs {
notification_email {
admin@example.com
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id linux02
}
vrrp_instance VI_1 { #Define an instance
state BACKUP #Set it to backup, and then use priority to control which one is promoted to primary
interface eth0
virtual_router_id 51
priority 50
advert_int 1 #Can automatically take over
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.199 dev eth0 label eth0:0 #Use the label label here to enable eth0:0
}
}
virtual_server 10.0.0.199 80 { #Below is the definition of LVS
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 10.0.0.191 80 { #One of them is a local web service
weight 1
TCP_CHECK {
connect_timeout 3
}
}
real_server 10.0.0.192 80 { #Another web service
weight 1
TCP_CHECK {
connect_timeout 3
}
}
}
After the keepalived configuration file is modified, start to modify apache's default page vi/opt/apache/apache-2.4.29/htdocs/index.html as long as the contents of the two default files are different.
About local startup loopback address
As we all know, if you uselvsIn dr mode, the loopback address must be activated on the realserver. Therefore, to build a Load Balancer between two machines, of course, the corresponding loopback address must be activated on the local machine to achieve the desired function. The script is as follows: (The following two scripts are original by Mosquito, and the Mosquito website address is http://www.wenzizone.cn)
# vi /usr/local/bin/startlo
#!/ bin/sh
VIP=10.0.0.199
case “$1″ in
stop)
# close lo:0 interface
echo $”Close lo:0 interface”
/sbin/route del -host $VIP dev lo:0
/sbin/ifconfig lo:0 down
echo “0”>/proc/sys/net/ipv4/conf/all/arp_announce
echo “0”>/proc/sys/net/ipv4/conf/all/arp_ignore
echo “0”>/proc/sys/net/ipv4/conf/lo/arp_announce
echo “0”>/proc/sys/net/ipv4/conf/lo/arp_ignore
;;
start)
# start lo:0 interface
echo $”Start lo:0 interface”
/sbin/ifconfig lo:0 $VIP/32 broadcast $VIP up
/sbin/route add -host $VIP dev lo:0
echo “2”>/proc/sys/net/ipv4/conf/all/arp_announce
echo “1”>/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2”>/proc/sys/net/ipv4/conf/lo/arp_announce
echo “1”>/proc/sys/net/ipv4/conf/lo/arp_ignore
;;
*)
echo $”Usage: $0 (start|stop)”
exit 1
;;
esac
But since keepalived does not have ldirectord to control the start and stop of the loopback address like heartbeat, we need humans to determine which machine to start the loopback address, so Mosquito wrote the following script.
# vi /usr/local/bin/check_interface
#!/ bin/bash
#
#Author: Mosquito
#
#Script Description:
#This script is used to determine whether the current server is the master
#If the current server is not master, start loopback address
#Otherwise stop looping back the address
#
#Please set VIP_interface according to your own situation. This is the VIP interface
#lo_interface Please set it according to your own situation. It is a loopback address interface
VIP_interface=”eth0:0″
lo_interface=”lo:0″
i=10
while [ $i != 0 ]
do
/sbin/ifconfig |grep $VIP_interface &>/dev/null
retval=$?
if [ ! $retval -eq 0 ];then
/sbin/ifconfig |grep $lo_interface &>/dev/null
retval=$?
if [ ! $retval -eq 0 ];then
/usr/local/bin/startlo start
fi
else
/sbin/ifconfig |grep $lo_interface &>/dev/null
retval=$?
if [ $retval -eq 0 ];then
/usr/local/bin/startlo stop
fi
fi
i=10
sleep 10
done
After both machines have created two new scripts, everything is ready for testing
First, set executable permissions for all required scripts. Start the corresponding services in turn
A. If apache is started randomly, you don't need to start it
B、# /usr/local/bin/check_interface
C、#service keepalived start
If there are no errors, it means that the service has been started, and the log of keepalived is stored in/var/log/messages. If there are any errors, please check it yourself
Test 1: Test the Load Balancer Condition in the Current State
Method: Open the ie browser, enter http://192.168.211.110, and then use ctrl+F5 to force the refresh. You can see the page content switching between linux01 and linux02
Test 2: Stop the keepalive of linux01 and test the Load Balancer situation
Method:
A、/etc/init.d/keepalived stop
Then wait a few seconds to see that the lo:0 address on linux01 has been activated and the lo:0 address on linux02 has stopped
B. Open the ie browser, enter http://192.168.211.110, and then use ctrl+F5 to force the refresh. You can see the page content switching between linux01 and linux02
Test 3: Turn on keepalived on linux01 and observe whether linux01 switches back to master
Method: Start the keepalived program, use ifconfig to observe that the eth0:0 interface has been started on linux01, and the lo:0 interface has stopped at the same time. The eth0;0 interface on linux02 is stopped, and the lo:0 interface has started at the same time.
The tutorial on building a highly available Load Balancer with apache has been completed. If you have any problems, you can leave a message below to discuss and exchange them together