Example of how to implement master-slave hot standby using Docker+keepalived+nginx

Example of how to implement master-slave hot standby using Docker+keepalived+nginx

Preface

To solve the single point of failure, we need to configure a master-slave hot standby solution. The number of servers is limited, so we use Docker to simulate the installation and configuration.

Docker is 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:7

2. Create a container

docker run -it -d --name centos1 -d centos:7

3. Enter container centos1

docker exec -it centos1 bash

4. Install common tools

yum updateyum install -y vimyum install -y wgetyum install -y gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl--develyum install -y popt-develyum install -y initscripts
yum install -y net-tools

5. Package the container into a new image and create a container directly from the image in the future

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

6. Delete the centos1 container created previously, re-create the container with the base image, and install keepalived+nginx

docker rm -f centos1
#The systemctl service needs to be used in the container, and /usr/sbin/init needs to be added
docker run -it --name centos_temp -d --privileged centos_base /usr/sbin/init
docker exec -it centos_temp bash

Author: Jianghu Jiujiu Link: https://juejin.im/post/5dc517386fb9a04a9272110b
Source: Nuggets. Copyright belongs to the author. For commercial reproduction, please contact the author for authorization. For non-commercial reproduction, please indicate the source.

7. Install nginx

#Use yum to install nginx. You need to include the Nginx library. Install the Nginx library 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 startup is successful. The nginx welcome interface appears, indicating that the installation is successful. curl 172.17.0.2

8. Install keepalived

1. Download keepalived wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz

2. Unzip and install: tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/

3. Download the openssl plug-in yum install -y openssl openssl-devel (need to install a software package)

4. Start compiling keepalived cd /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived

5. Make it make && make install

9. Install keepalived as a system service

mkdir /etc/keepalivedcp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/Then copy the 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/keepalived /usr/sbin/You can set it to start at boot: chkconfig keepalived on, now we have completed the installation!

#If an error occurs during startup, execute the following command cd /usr/sbin/ 
rm -f keepalived 
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 

#Common commands systemctl daemon-reload reloads systemctl enable keepalived.service sets automatic startup at boot systemctl disable keepalived.service cancels automatic startup at boot systemctl start keepalived.service starts systemctl stop keepalived.service stops systemctl status keepalived.service checks service status

10. Modify the /etc/keepalived/keepalived.conf file

#Backup configuration file 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
  }
}

11. Add heartbeat detection file

vim nginx_check.sh
#The following is the script content#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
  /usr/local/nginx/sbin/nginx
  sleep 2
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
    killall keepalived
  fi
fi

12. Give the script execution permissions

chmod +x nginx_check.sh

13. Set up startup

systemctl enable keepalived.service

#Open keepalived
systemctl start keepalived.service

14. Check whether the virtual IP is successful. Execute the following command in the host machine. If the nginx welcome interface appears, it indicates success.

curl 172.17.0.100

15. Repackage the centos_temp container into an image, and then use this new image to create two more containers to achieve hot standby effect

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

16. Delete all containers

docker rm -f `docker ps -a -q`

17. Create the main server container using the centos_kn image

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

docker exec -it centos_master bash

18. Modify the nginx welcome page in centos_master,

vim /usr/share/nginx/html/index.html 

19. Create a slave server container

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

#Modify the keepalived.conf configuration file, mainly the adjustment of the state and priority parameters. The priority value of the master node must be larger 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
  }
}

20. Reload after modification

systemctl daemon-reload
systemctl restart keepalived.service

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

vim /usr/share/nginx/html/index.html 

22. Test

A> Run the following command test on the host, centos_master, and centos_slave respectively. If the welcome page of Master is displayed, it means that the configuration is successful 1/3

curl 172.17.0.100

B> At this time, stop the centos_master container (docker stop centos_master), keep the centos_slave container, and execute the following command. If you switch to the Slave page, it means that the keepalived configuration is successful 2/3

curl 172.17.0.100

C> Restart the centos_master container and execute the following command to see if the switch is from Slave to Master. If the switch is successful, it means that our configuration has been successful.

curl 172.17.0.100

Note: During the test, after restarting the container, nginx did not start. You need to enter the container and start it. Otherwise, you cannot access the Master page, but you can ping it.

Execute 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

chkconfig nginx on

The above is the entire configuration process. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Nginx+Keepalived realizes hot standby of dual machines
  • Configuration method of keepalived dual-machine hot standby nginx
  • Nginx implements high availability cluster construction (Keepalived+Haproxy+Nginx)
  • Keepalived implements Nginx load balancing and high availability sample code
  • How to implement dual-machine master and backup with Nginx+Keepalived
  • Detailed explanation of nginx+keepalived high availability master-slave configuration
  • About using Keepalived to achieve automatic restart of Nginx and dual-active hot standby high availability

<<:  Vue implements simple image switching effect

>>:  Detailed explanation of the use of Vue card-style click-to-switch image component

Recommend

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

Summary of Spring Boot Docker packaging tools

Table of contents Spring Boot Docker spring-boot-...

Example of using javascript to drag and swap div positions

1 Implementation Principle This is done using the...

How to use JSX to implement Carousel components (front-end componentization)

Before we use JSX to build a component system, le...

React uses routing to redirect to the login interface

In the previous article, after configuring the we...

Recommend a cool interactive website made by a front-end engineer

Website link: http://strml.net/ By Samuel Reed Ti...

JavaScript setTimeout and setTimeinterval use cases explained

Both methods can be used to execute a piece of ja...

innodb_flush_method value method (example explanation)

Several typical values ​​of innodb_flush_method f...

DIV common attributes collection

1. Property List Copy code The code is as follows:...

JavaScript to achieve slow motion animation effect

This article shares the specific code for JavaScr...

Three useful codes to make visitors remember your website

Three useful codes to help visitors remember your...

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...