我是靠谱客的博主 寂寞大米,最近开发中收集的这篇文章主要介绍Centos 7+Docker+Nginx+KeepAlived实现高可用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


Docker安装全过程:

docker的安装教程网上到处都是,随便贴个地址: https://www.cnblogs.com/huangjinyong/p/11834835.html

拉取centos7镜像:

docker pull centos:7

创建容器:

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

进入容器:

docker exec -it centos1 bash

安装一些常用工具:

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

打包镜像,方便以后使用:

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

删除以前创建的镜像:

docker rm -f centos1

创建新的镜像:

#容器内需要使用systemctl服务,需要加上/usr/sbin/init
docker run -it --name centos7_1 -d --privileged centos_base /usr/sbin/init
docker exec -it centos7_1 bash

安装nginx:

#使用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 -y nginx
#启动nginx
systemctl start nginx.service
#查看是否启动成功,出现nginx欢迎界面表示安装成功
curl 172.17.0.2

下载并安装keepalived:

wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
# 解压到/usr/local下
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
# 安装https需要的包
yum install -y openssl openssl-devel
# 编译keepalived
cd /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
make && make install

# 将keepalived 安装成系统服务
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
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/
#可以设置开机启动
chkconfig keepalived on

systemctl start keepalived.service 
# 如果启动报错则执行下面的命令
cd /usr/sbin/
rm -f keepalived 
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

修改keepalived配置文件:

# 备个份
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.backup
vim /etc/keepalived/keepalived.conf
# 文件配置如下:
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
  }
}

编辑检测脚本:

vim /etc/keepalived/nginx_check.sh
# 脚本如下:
#!/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

添加执行权限:

chmod +x /etc/keepalived/nginx_check.sh

开启keepalived(开启过则需要重启启动一下):

systemctl enable keepalived.service
# 开启keepalived
systemctl start keepalived.service
# 检查虚拟IP是否绑定成功
curl 172.17.0.100(确保nginx运行) 或者 ip a(现在是在容器里面)

在这里插入图片描述

重新打包镜像,环境都已经配置好了,方便以后使用:

docker commit -a 'cfh' -m 'centos with keepalived and nginx' centos7_1 centos_nginx_keepalived

删除所有容器:

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

创建master:

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

# 修改index.html让他们看起来不太一样:
vim /usr/share/nginx/html/index.html

在这里插入图片描述

创建slave:

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

# 更改keepalived的配置文件,把priority改小一点,当前是从机
vim /etc/keepalived/keepalived.conf,当前图是 master的(偷个懒),slave改到80

在这里插入图片描述

# 修改index.html让他们看起来不太一样,当前是salve节点:
vim /usr/share/nginx/html/index.html

重新加载(nginx没重启的重启一下,主从机都执行一下):

systemctl daemon-reload
systemctl restart keepalived.service

测试:

curl 172.17.0.100

在这里插入图片描述
进入master,主动停掉keepalived,再执行:

curl 172.17.0.100

在这里插入图片描述
现在返回的就是slave的信息了

最后

以上就是寂寞大米为你收集整理的Centos 7+Docker+Nginx+KeepAlived实现高可用的全部内容,希望文章能够帮你解决Centos 7+Docker+Nginx+KeepAlived实现高可用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部