docker build keepalived + nginx high availability

 Foreword

  Recent work useful to keepalived, to think about building a verification environment-related functions locally. Because the virtual machine is too much trouble, they help to build such a docker environment, incidentally, learning to consolidate the relevant command docker;

1. Preparations

  Local installed docker environment, win10 my local use, has been installed docker environment;

 

2. Download centos7.6 base image

docker  pull  centos:7.6.1810

 

I have installed, so the results are as follows

 

3. In centos7.6 installation keepalived and nginx as well as other software

Start container:

docker run -it  f1cb7c7d58b7 /bin/bash

 

 

 

Use ip and the ifconfig command to install both of the following

yum install iproute

yum  install  net-tools

 

 

4. Use yum mounted keepalived

yum install keepalived

 

Has been confirmed y can be, the installation is complete 

 

Keepalived detection nginx script

/ etc / keepalived in New check_nginx.sh

Script as follows : Note grep nginx: colon, because the name of the script execution time is also nginx , will not lead to the number calculated, so use nginx:

#!/bin/bash

A=$(ps -ef  | grep nginx: | grep -v  grep |  wc  -l)

if [ $A -eq 0 ];then

  nginx

  echo  "restart nginx, sleep 2 s"

  sleep  2

  num=$(ps -ef  | grep  nginx:  |  grep -v grep | wc  -l)

  if [ $num -eq  0  ];then

    ps -ef | grep keepalived  |  grep  -v grep  | awk '{print $2}'| xargs kill -9

    echo  "start nginx failed,kill keepalived"

  fi

else

  echo  "nginx not  dead"

fi

 

 

Check this machine IP 

ip a

 

Keepalived Configuration 

Notes vrrp_strict   otherwise it will lead to VIP inaccessible

 

Increase the detection script

Add VIP , attention VIP and native IP in the same network segment, otherwise it can not be accessed

The machine 172.17.0.6/16 virtual IP set 172.17.0.8/16

 

5. Use yum install nginx

Reference Links: https://www.cnblogs.com/opsprobe/p/10773582.html

yum install yum-utils

 

Add Source

To  cd /etc/yum.repos.d/ directory

New 

vim nginx.repo

 

 file

Enter the following information

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

 

 

 

yum install nginx

 

The installation is complete

Start to see success

nginx

 

curl localhost:80

 

 

6. Quit  , making mirror

docker ps  -a

 

Just find a container

 

docker commit 5f781fbe483e keepavled_nginx:v1

 

 

7. Start standby keepalived container

docker  run --privileged  -it --name keepalived_master  8dfe8c83bfae /usr/sbin/init

docker  run --privileged  -it --name keepalived_salve  8dfe8c83bfae /usr/sbin/init

 

 

8. into the container

docker ps

 

  Find the start of the container

Respectively, into the container

Main : 172.17.0.7

Preparation: 172.17.0.6

Preparation keepalived modify the configuration 

9. Verify keepalived

Start the main keepalived , found that the virtual IP is already bound

systemctl status keepalived

 

 View startup state

Preparation keepalived start , the virtual IP is not bound

Kill off the main keepalived After the process, the master is no longer bound VIP

 

Revisit equipment , found bound VIP

 

And then start the main keepalived , you will find VIP rebind the main keepalived server

 

 10. Verify nginx

  Kill off the nginx after the process again to see'll find nginx be restarted because keepalived detection script automatically checks, no process will automatically restart, start unsuccessful put keepalived kill off.

  See script check_nginx.sh

  Note: check_nginx.sh need to add permissions, otherwise they would not perform;

  chmod  +x  check_nginx.sh

VIP access nginx test

We were prepared to modify the primary nginx page and restart

we /usr/share/nginx/html/index.html

 

 

Each start standby keepalived, this case VIP binding in the main server;

Access nginx: curl 172.17.0.8:80 , visit this time is the master server

The primary server 's keepalived kill -verification off, this time VIP binding in a standby server, the standby server of the access nginx

 

11. encountered several problems

VIP inaccessible problem:

  Vrrp_strict commented

  VIP in a network segment

 

Check_nginx.sh script problem:

   grep nginx: colon to add or implementation process will grep to determine the number of processes leading to the script name has not 0;

   The script must add permissions, or can not execute

 

 

Reference links:

https://www.cnblogs.com/jinjiangongzuoshi/p/9313438.html

 

Guess you like

Origin www.cnblogs.com/zhhx/p/12656813.html