preface
I’ve been looking at distributed and highly available cluster architectures and deploying highly available clusters lately, so I want to summarize these experiences.
As we all know, when the business of the company starts to develop, the traffic we bear will increase accordingly. From the beginning, a single Nginx server as a proxy request forwarding is not enough to cope with our daily traffic, so we need to carry more traffic.
Although some people will say, now are on the cloud products, which also need to build their own server cluster, or loadbalance but still that sentence, development is definitely to make the technology more and more easy to use, master difficult, maybe you will click a few buttons can set up the entrance of the whole cluster, when others asked its principle how to do?
Keepalived
To bind THE VRRP protocol to VIP, you are advised to disable the iptables serviceCopy the code
Keepalived is what? Keepalived is a server health check tool that can be used to kick a server out of the cluster if it crashes/malfunctions, and to add it back to the cluster when it recovers. That’s what Keepalived is all about, leaving it all to the machine.
The concepts of layer 4 and layer 7 of load balancing are not outlined here. Because this time it’s all about summary deployment.
There are two options for Keepalived: master/slave mode the difference between the two modes is that the former standby machine does not work and the latter standby machine works together.
Before configuring KeepAlived, let’s prepare 2 Linux virtual machines as an example
- 192.168.1.100 master
- 192.168.1.101 salve
Configure Keepalived – master slave mode
First we need to install Keepalived, here I use the simplest way to install, compile and install you can refer to other ways. I just want to be convenient
yum install keepalived
Copy the code
Remember that both hosts need to be installed and then entered into the configuration file
vim /etc/keepalived/keepalive.conf
Copy the code
So if you go into configuration, you’ll see a lot of configuration, and I have a lot of features here that I don’t need to use like health detection email and so on. So just skip this part right here, and we can get rid of all that stuff and keep this stuff.
global_defs {
# Route ID, the identifier of the host where the keepalived node is currently installed
router_id keep_100
}
# Computer node
vrrp_instance VI_1 {
# status, current
state MASTER
# Network card bound to the current instance
interface eth0
# Ensure that the active and standby nodes are consistent
virtual_router_id 51
# Priority/weight
When the master dies, it becomes master
The current master node is therefore the highest
priority 100
# Interval for checking synchronization between the active and standby nodes
advert_int 1
# Authentication and authorization password to prevent the entry of illegal nodes
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# VIP192.168.1.161}}Copy the code
Now that you’ve basically done the configuration of the master node, let’s do the configuration of the Salve, which is basically the same
global_defs {
# Route ID, the identifier of the host where the keepalived node is currently installed
router_id keep_101
}
# Computer node
vrrp_instance VI_1 {
BACKUP is required
state BACKUP
# Network card bound to the current instance
interface eth0
# Ensure that the active and standby nodes are consistent
virtual_router_id 51
# Priority/weight
Set this side to 50 because it's salve
priority 50
# Interval for checking synchronization between the active and standby nodes
advert_int 1
# Authentication and authorization password to prevent the entry of illegal nodes
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# VIP192.168.1.161}}Copy the code
Don’t forget to reboot when you’re done
systemctl restart keepalived.service
Copy the code
And then we type on the master node
ip addr
Copy the code
At this point we should see that our configured network adapter has 192.168.1.161 bound to this network adapter. Easy, right?
Configure service heartbeat detection
Next we need to configure the check nginx server state, write a simple script to help us check nginx
#! /bin/bash
A=`ps -C nginx --no-header |wc -l`
# Determine if nginx is down and try to restart it
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
# Wait and check again. If not, close Keepalive and let it start the standby machine
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
Copy the code
We saved it in /etc/keepalived/check_nginx_alive.sh, remember to set executable permissions for the file once it is saved
chmod +x check_nginx_alive.sh
Copy the code
Then go back to the keepalive. conf file on the master node to configure the script to execute periodically and open the configuration file to add
// Add script to master keepalive.conf vrrp_script check_nginx_alive {script"/etc/keepalived/check_nginx_alive.sh"Interval 2 // Weight 10 is executed every two secondsIf the script runs successfully, the weight of the current node is +10
}
Copy the code
Then configure the script into our instance
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
Add heartbeat detection script
track_script {
The name here needs to be handled by the name above the rootCheck_nginx_alive} virtual_ipaddress {192.168.1.161}}Copy the code
After the configuration is complete, we can also try to stop all the master nodes by keepalived. Then go back to the IP addr on the salve machine and you will find that the IP address 192.168.1.161 is bound to the network card of the Salve machine. This is also known as IP drift
This completes the master-slave configuration. However, you will find that all requests will only be made to the master node, and salve is completely a backup machine. How can we allow such waste? Therefore, the standby machine should also join in the service of users can ah
Configuration Keepalived – Master mode (dual Master hot Backup)
After configuring the master/slave mode, it is very easy to configure the dual master mode. It literally means dual master mode is basically two machines, both master and salve at the same time what does that mean? Let’s configure it and you’ll get the idea pretty quickly
Let’s open the master configuration file and add the standby configuration file below
Change instance to vi_2
vrrp_instance VI_2 {
state BACKUP
interface eth0
# to 52
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# VIP192.168.1.161}}Copy the code
Then we will go to the Salve node to configure a master CONF
# change the instance name
vrrp_instance VI_2 {
state MASTER
interface eth0
# to 52
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# VIP192.168.1.161}}Copy the code
That completes the configuration. It’s very simple to configure the salve on the master, configure the master on the salve so that it becomes a bidirectional node. Master each other, and also successfully applied salve machine.
Keepalived +LVS is usually used together with keepalived. Let me write a separate summary of keepalived+LVS.