The background,
Nginx high availability via Keepalived, because at home do not want to get multiple hosts, so the runtime environment with Docker package to simulate cross-host
Docker base image: centos
Before I say that, a brief introduction:
Keepalived is a highly available software based on VRRP protocol. Keepailived has a primary server and multiple backup servers. The same service configuration is deployed on the primary server and backup server, and a virtual IP address is used to provide external services. When the primary server fails, the virtual IP address is automatically migrated to the backup server.
There are two types of two-node high availability methods:
- Dual-system active/standby (also called dual-system hot backup)
- Dual master (also called dual master)
As described below, the configuration of high availability in dual master/slave mode, dual master/master mode, mainly keepalive. conf will be different, the method is the same.
Two, specific operation
1. Install a centos image
docker pull centos
Copy the code
By using centos image to install all the environments required by ha, and then start two containers, and then real simulation of the scenario across the host
Install the required environment on centos (nginx and other tools)
Run centos containers
docker run -it centos /bash/bin
Copy the code
Install dependencies and required packages
#使用yum安装nginx需要包括Nginx的库,安装Nginx的库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 使用下面命令安装nginx
#yum install nginx
#安装网络包(需要使用ifconfig和ping命令)
yum install net-tools
#安装vim
yum install vim
Copy the code
3. Install Keepalvied on centos
# yum install keepalived GCC openssl-devel popt-devel # yum install keepalived # yum install keepalived http://124.205.69.132/files/90630000053A2BB4/www.keepalived.org/software/keepalived-1.3.4.tar.gz tar ZXVF Keepalived-1.3.4.tar. gz CD keepalived-1.3.4. /configure --prefix=/usr/local/keepalived make && make install Copy several files to CentOS7: Cp keepalived 1.3.4 / keepalived/etc/init. D/keepalived/etc/init. D/mkdir/etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp Keepalived 1.3.4 / keepalived/etc/sysconfig/keepalived/etc/sysconfig/cp/usr/local/keepalived/sbin/keepalived/usr/sbin /Copy the code
4, modify the/etc/keepalived/keepalived. Conf file
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server mail.example.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -5 fall 3 rise 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 2 priority 101 advert_int 2 authentication { auth_type PASS auth_pass 1111 } Virtual_ipaddress {172.17.0.210} track_script {chk_nginx}}Copy the code
The/etc/keepalived/check_nginx. Sh file
A=`ps -ef | grep nginx | grep -v grep | wc -l` if [ $A -eq 0 ]; then nginx sleep 2 if [ `ps -ef | grep nginx | grep -v grep | wc -l` -eq 0 ]; then #killall keepalived ps -ef|grep keepalived|grep -v grep|awk '{print $2}'|xargs kill -9 fi fiCopy the code
Grant execute permission to check_nginx.sh:
chmod +x check_nginx.sh
Copy the code
Note: Keepalived is used to determine whether the server is down by detecting whether the Keepalived process exists. If the Keepalived process is present but the Nginx process is not present, keepalived will not switch between main and standby. Therefore, we need to write a script to monitor whether the Nginx process exists. Kill keepalived process if nginx does not exist.
If the nginx process does not exist, the keepalived process will be killed and the VIP will be migrated to the backup machine
5. Set boot
Chkconfig Keepalived on or systemctl enable Keepalive. service Sets keepalive. service to automatically start upon startupCopy the code
To start keepalived service:
Systemctl start keepalive. service StartsCopy the code
6. After installing all required dependencies and environments, resubmit the container additions
docker commit 5d112 centos_keepalived_nginx:v1
Copy the code
Note: 5D112 is the container ID corresponding to the above installation software
6. Start a container containing keepalived+nginx
docker run --privileged -tid --name keepalived_master centos_keepalived_nginx:v1 /usr/sbin/init
Copy the code
Enter the keepalived_master container:
docker exec -it keepalived_master bash
Copy the code
Go to /usr/share/nginx/html and modify the index.html file
Modify the title to:
Welcome to nginx Master!
7. Start keepaliveD_salve
Docker run --privileged -- tid --name keepalived_slave centos_keepalived_nginx:v1 /usr/sbin/init # run --privileged -- tid --name keepalived_slave centos_keepalived_nginx:v1 /usr/sbin/init -it keepalived_slave bashCopy the code
8, modify the keepalived_salve container nginx index.html file
vim /usr/share/nginx/html/index.html
Copy the code
Modify the title to:
Welcome to nginx Slave!
9, modify keepalived.conf file in keepaliveD_salve container (master container, keep the same as the image Settings, do not need to change)
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server mail.example.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -5 fall 3 rise 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 2 priority 100 advert_int 2 authentication { auth_type PASS auth_pass 1111 } Virtual_ipaddress {172.17.0.210} track_script {chk_nginx}}Copy the code
The state and priority parameters for the master node must be higher than those for the backup node.
The principle is as follows: 1. Each Keepalived VRRP group strives for the master through VRRP broadcast. 2. Keep the same virtual_Router_id for keepalived as a VIP service
Virtual_router_id = “master”; virtual_router_id = “backup”
When you’re done, reload
systemctl daemon-reload
systemctl restart keepalived.service
Copy the code
10, validation,
Check keepalived service status in both containers
systemctl status keepalived.service
Copy the code
Keepalived_master Service status effect:
Keepalived_slave Service status rendering:
As you can see, the Keepalived service works fine
Start the nginx: nginx
Delete 172.17.0.210 from the master container
Curl 172.17.0.210:
172.17.0.210 is used to access nginx data on both the master and slave containers. The requested data is the nginx configured data in the master container: welcome to nginx master
To continue, turn off the Keepalived service for the master container:
Curl 172.17.0.210 returns data from slave, welcome to nginx slave when keepalived is disabled
To further verify, disable keepalived service for the master container and then enable it:
As you can see, when keepalived is enabled in the Master container, the data returned by the request is forwarded to the Master again.
So far, all the verification is as expected, and we have implemented a whole set of highly available solutions based on Nginx+Keepalived with the help of Docker.
Keepalived service order
- Systemctl daemon-reload Reload
- Systemctl enable Keepalive. service Enables automatic startup upon startup
- Systemctl disable keepalive. service Disables automatic startup upon startup
- Systemctl start keepalive. service Starts
- Systemctl stop Keepalive. service Stops
- Systemctl status Keepalive. service Displays the service status