preface

In order to solve the single point of failure, we need to configure the master/slave hot backup scheme. The number of servers is limited, so Docker is used to simulate installation and configuration.

Docker has been installed by default in this configuration.

Configuration environment :centos7 64-bit

Docker version :Docker Version 17.12.1- CE, Build 7390fc6



1. Pull the centos7 image

docker pull centos:7Copy the code

2. Create container

docker run -it -d --name centos1 -d centos:7Copy the code

3. Enter the container Centos1

docker exec -it centos1 bash
Copy the code

4. Install common tools

yum update
yum install -y vim
yum install -y wget
yum install -y  gcc-c++  
yum install -y pcre pcre-devel  
yum install -y zlib zlib-devel  
yum install -y  openssl-devel
yum install -y popt-devel
yum install -y initscripts
yum install -y net-tools
Copy the code

5, package the container into a new image, and then create the container directly from that image

docker commit -a 'cfh' -m 'centos with common tools' centos1 centos_base
Copy the code

6, delete the centos1 container created earlier, re-create the container with the base image, install Keepalived +nginx

docker rm -f centos1
/usr/sbin/init = /usr/sbin/init
docker run -it --name centos_temp -d --privileged centos_base /usr/sbin/init
docker exec -it centos_temp bashCopy the code

7, install nginx

# yum yum yum yum yum yum yum yum yum yum yum yum yum yum yum yum
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Install nginx using the following command
yum install -y nginx
# start nginx
systemctl start nginx.service
# Check whether the installation is successful. If the nginx welcome screen appears, the installation is successfulThe curl 172.17.0.2Copy the code

Keepalived 8, installation

1. Download keepalived wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz 2. Decompress installation: tar -zxvf keepalived-1.2.18.tar.gz -c /usr/local/ 3. Openssl yum install -y openssl openssl-devel Start compiling Keepalivedcd  /usr/local/ keepalived - 1.2.18 / &&. / configure -- prefix = / usr /local/keepalived 5. Make make && make installCopy the code

9. Install Keepalived as system service

mkdir /etc/keepalived
cp /usr/local/ keepalived/etc/keepalived/keepalived conf/etc/keepalived/then duplicate keepalived script file: cp/usr /local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/usr/sbin/ can be set to boot upon boot: chkconfig Keepalived on to this we install complete!If an error occurs during startup, run the following command
cd /usr/sbin/ 
rm -f keepalived  
cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/ 

Common commandsSystemctl daemon-reload Reloads systemctlenableKeepalive. service sets systemctl to automatically start upon startupdisableKeepalive. service Stop keepalive. service Stop keepalive. service Stop keepalive. service Stop systemctl status Keepalive. service Checks the service statusCopy the code

10 and modify/etc/keepalived/keepalived conf file

Backup configuration files
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.backup

rm -f keepalived.conf
vim keepalived.conf
The configuration file is as follows

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"Interval 2 weight-20} vrrp_instance VI_1 {state MASTER interface eth0 virtual_router_id 121 McAst_src_ip 172.17.0.6 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } Virtual_ipaddress {172.17.0.100}}Copy the code

11. Add the heartbeat detection file

vim nginx_check.sh
# Here is the script content
#! /bin/bashA = ` ps - C nginx - no - the header | wc-l`
if [ $A -eq0];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq0];then
        killall keepalived
    fi
fi
Copy the code

12. Grant execute permission to the script

chmod +x nginx_check.shCopy the code

13. Set boot

systemctl enable keepalived.service

# open keepalived
systemctl start keepalived.service

Copy the code

14, check whether the virtual IP is successful. Run the following command on the host. If the nginx welcome screen is displayed, the virtual IP is successful

The curl 172.17.0.100Copy the code


15, repackage the Centos_temp container as an image, and then use the new image to create two more containers for hot spare effect

docker commit -a 'cfh' -m 'centos with keepalived nginx' centos_temp centos_kn
Copy the code

16, Delete all containers

docker rm -f `docker ps -a -q`
Copy the code

17, create primary server container with centos_KN image

docker run --privileged  -tid --name centos_master --restart=always  centos_kn /usr/sbin/init

docker exec -it centos_master bash
Copy the code

Alter nginx welcome page in centos_master

vim /usr/share/nginx/html/index.html
Copy the code



Create the slave server container

docker run --privileged  -tid --name centos_slave --restart=always  centos_kn /usr/sbin/init
docker exec -it centos_slave bash

Keepalive. conf configuration file is changed to state and priority. The priority of the master node must be higher than that of the slave node

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"Interval 2 weight-20} vrrp_instance VI_1 {state SLAVE interface eth0 virtual_router_id 121 McAst_src_ip 172.17.0.6 priority 80 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } Virtual_ipaddress {172.17.0.100}}Copy the code

20. Reload after modification

systemctl daemon-reload
systemctl restart keepalived.serviceCopy the code

21, Modify nginx welcome page (if nginx is not started, run systemctl start nginx.service)

vim /usr/share/nginx/html/index.html
Copy the code




22, the test

A> Run the command test on the host, centos_master, and centos_slave respectively. If the welcome page for Master is displayed, 1/3 of the configurations are successful

The curl 172.17.0.100Copy the code

B> Stop the centos_master container (docker stop centos_master) and keep the centos_slave container. Run the following command. If the Slave page is switched, keepalived configuration is successful 2/3

The curl 172.17.0.100Copy the code

C> Restart the centos_master container. Run the following command to switch from Slave to Master. If the switch is successful, the configuration is successful.

The curl 172.17.0.100Copy the code

During the test, nginx does not start after restarting the container, so you need to enter the container to start it, otherwise you cannot access the Master page, but you can Ping through it.

Run the following command to configure nginx to start randomly so that you don’t need to manually start nginx every time you restart the container

systemctl enable nginx.service
chkconfig nginx on
Copy the code

This is the configuration process