Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use keepalived+nginx to realize Master-Slave Hot standby in Docker

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use keepalived+nginx to implement master-slave hot backup in Docker? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

1. Pull centos7 image

Docker pull centos:7

2. Create a container

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

3. Enter the 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 initscriptsyum install-y net-tools

5. Package the container into a new image, and create the container directly with this image later.

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

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

The systemctl service needs to be used in the docker rm-f centos1# container, and / usr/sbin/initdocker run-it-- name centos_temp-d-privileged centos_base / usr/sbin/initdocker exec-it centos_temp bash author: jianghu Emergency Link: https://juejin.im/post/5dc517386fb9a04a9272110b Source: Nuggets copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

7, install nginx

# installing nginx using yum requires libraries that include Nginx. Install the library rpm-Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm# of Nginx by using the following command to install nginxyum install-y nginx# and start nginxsystemctl start nginx.service# to check whether it has been started successfully. The nginx Welcome screen indicates 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

two。 Extract and install: tar-zxvf keepalived-1.2.18.tar.gz-C / usr/local/

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

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

5.make make & & make install

9, install keepalived as a system service

Mkdir / etc/keepalivedcp / usr/local/keepalived/etc/keepalived/keepalived.conf / etc/keepalived/ and 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/ can be set to boot: chkconfig keepalived on, at this point we have installed! # error report if startup Then execute the following command cd / usr/sbin/ rm-f keepalived cp / usr/local/keepalived/sbin/keepalived / usr/sbin/ # Common command systemctl daemon-reload reload systemctl enable keepalived.service settings boot auto start systemctl disable keepalived.service cancel boot auto start systemctl start keepalived.service start systemctl stop keepalived.service stop systemctl status keepalived.service check service status

10, modify / etc/keepalived/keepalived.conf file

# backup configuration file cp / etc/keepalived/keepalived.conf / etc/keepalived/keepalived.conf.backuprm-f keepalived.confvim keepalived.conf# configuration file 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

The following is the content of the script #! / bin/bashA= `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 fifi

12, give execution permission to the script

Chmod + x nginx_check.sh

13, set boot up

Systemctl enable keepalived.service# enables keepalivedsystemctl start keepalived.service

14. Check whether the virtual IP is successful, and execute the following command in the host. If the nginx welcome interface appears, it is successful.

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 the hot backup 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 a master server container with a centos_kn image

Docker run-- privileged-tid-- name centos_master-- restart=always centos_kn / usr/sbin/initdocker 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/initdocker exec-it centos_slave bash# modifies the keepalived.conf configuration file, mainly for the adjustment of state and priority parameters The priority value of the master node must be higher than slave before 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 121mcast_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 the modification is completed

Systemctl daemon-reloadsystemctl restart keepalived.service

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

Vim / usr/share/nginx/html/index.html

22, testing

A > perform a command test in the host and centos_master,centos_slave respectively. If the welcome page of Master is displayed, it indicates that the configuration is successful.

Curl 172.17.0.100

B > stop the centos_master container (docker stop centos_master) at this time, keep the centos_slave container, and execute the following command. If you switch to the Slave page, the keepalived configuration is successful.

Curl 172.17.0.100

C > restart the centos_master container. Execute the following command at this time to see if you have switched from Slave to Master. If the switch is successful, it means that we have successfully configured it.

Curl 172.17.0.100

Note: during the test, nginx did not start after restarting the container, so you need to enter the container to start it, otherwise you will not be able to access the Master page, but you can Ping it.

Execute the following command to configure nginx to start randomly, so that you do not need to start nginx manually every time you restart the container

Chkconfig nginx on's answer to the master-slave hot backup question on how to use keepalived+nginx in Docker is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report