美文网首页Amazing Arch
docker内安装keepalived模拟高可用

docker内安装keepalived模拟高可用

作者: ThinkJava | 来源:发表于2019-03-19 14:07 被阅读81次

目标&效果图

一台服务器(Centos7),通过docker+keepalived模拟搭建一套web server层面的高可用系统。 docker+keepalived模拟搭建一套web server层面的高可用系统

实战

宿主机环境配置

  • 安装Docker
yum -y install docker #安装Docker
systemctl start docker #启动Docker服务
  • 拉取Centos镜像
docker pull centos #pull centos镜像,这里可以和git操作类比
  • 安装其他辅助工具
#安装网络包(需要使用ifconfig和ping命令)
yum install net-tools
#安装vim
yum install vim
  • 安装Keepalived
yum install -y gcc openssl-devel popt-devel #安装依赖环境
yum install keepalived #安装keepalived

运行容器&&进入容器

docker run -it keepalived_master /bin/bash #通过pull的镜像,运行容器
docker ps #查看docker进程
docker exec -it 54243978aa28 bash #进入 54243978aa28 [Master]这个容器
查看docker进程

Master容器环境配置

  • 安装Keepalived[Master容器内]
yum install -y gcc openssl-devel popt-devel #安装依赖环境
yum install keepalived #安装keepalived
  • 安装Nginx[Master容器内]
#使用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 nginx
#安装网络包(需要使用ifconfig和ping命令)
yum install net-tools
#安装vim
yum install vim
  • 修改keepalived配置
vim  /etc/keepalived/keepalived.conf 
Master keepalived.conf
state MASTER #keepalived主服务
priority 101 #选举的优先级
virtual_ipaddress #虚拟地址ip,多个ip可以以多行的方式添加
track_script #指定检测脚本,此处为/etc/keepalived/nginx_check.sh
  • 添加nginx是否存活脚本
#检测nginx是否存活的脚本
A=`ps -ef | grep nginx | grep -v grep | wc -l`
if [ $A -eq 0 ];then
  nginx
  sleep 2
  if [ `ps -ef | grep nginx | grep -v grep | wc -l` -eq 0 ];then
      #killall keepalived
      ps -ef|grep keepalived|grep -v grep|awk '{print $2}'|xargs kill -9
  fi

fi
  • 启动keepalived&nginx
systemctl daemon-reload  #重新加载配置
systemctl start keepalived.service #启动keepalived服务
systemctl status keepalived.service #查看当前状态
Master容器 systemctl status keepalived.service
  • 修改Nginx默认页面
vim /usr/share/nginx/html/index.html
Nginx默认页面修改效果图
  • 启动nginx
nginx #启动nginx,如果报端口占用,请看下边「遇到的问题」模块
  • Master测试
ifconfig #查看当前ip
ifconfig效果图
curl 172.17.0.3 #请求nginx
curl 172.17.0.3效果图
  • 以上,Master容器配置完成,接下来操作Salve环境。

保存Docker Master镜像

exit; #退出当前容器
 docker ps|grep "keepalived_master"; #查看刚运行的容器ID
查看keepalived_master容器ID
docker commit 54243978aa28 keepalived_master:v1 #提交容器镜像到本地 54243978aa28 为容器ID
docker images; #查看当前镜像
查看当前镜像

Slave容器环境配置

  • 在Master镜像的基础上,运行Docker Slave容器
    #基于keepalived_master:v1镜像,启动keepalived_slave容器
    docker run --privileged  -tid --name  keepalived_slave   keepalived_master:v1 /usr/sbin/init
    
    #查看keepalived_slave容器进程ID
    docker ps
    
keepalived_slave容器进程
#进入 c5ea97ccc82d [Slave]这个容器
docker exec -it c5ea97ccc82d bash 
  • 修改Slave容器的keepalived配置
    vim /etc/keepalived/keepalived.conf
    
slave容器的keepalived配置
  state BACKUP #keepalived主服务
  priority 100 #选举的优先级,Master设置的101
  virtual_ipaddress #虚拟地址ip,多个ip可以以多行的方式添加
  track_script #指定检测脚本,此处为/etc/keepalived/nginx_check.sh
  • 重新加载keepalived配置、重启、查看状态【Slave容器】
    systemctl daemon-reload  #重新加载配置
    systemctl start keepalived.service #启动keepalived服务
    systemctl status keepalived.service #查看当前状态
    
Slave容器 systemctl status keepalived.service效果图
  • 修改Nginx默认页面


    Slave容器 Nginx默认页面
  • 启动nginx
nginx #启动nginx,如果报端口占用,请看下边「遇到的问题」模块
  • Slave测试
ifconfig #查看ip
Slave容器ip地址
 curl 172.17.0.2
Slave容器 Nginx默认页面

以上,slave容器配置完成,进入验收测试阶段。

验收测试Keepalived、VIP

exit #退出salve容器
curl 172.17.0.210 #curl vip,我们配置的虚拟ip地址
最终效果图

其他测试用例

  • 关掉主的keepalived - 测试主从高可用
  • 关掉主的nginx - 测试检测nginx存活脚本

遇到的问题

  • 容器里边启动Nginx,提示端口占用,run容器的时候,使用了--net=host,该方式使用宿主机的网络
  • 启动keepalived之后,自动停止,查看状态systemctl status keepalived.service,报错
    IPVS: Can’t initialize ipvs: Protocol not available
    Unable to load module xt_set - not using ipsets,
    解决办法:
lsmod | grep ip_vs #返回空
lsmod | grep xt_set #返回空
vi /etc/sysconfig/modules/ip_vs.modules
#保存退出
#!/bin/sh
/sbin/modinfo -F filename ip_vs > /dev/null 2>&1
if [ $? -eq 0 ]; then
    /sbin/modprobe ip_vs
fi
#保存退出
chmod 755 /etc/sysconfig/modules/ip_vs.modules
vi /etc/sysconfig/modules/xt_set.modules
#!/bin/sh
/sbin/modinfo -F filename xt_set > /dev/null 2>&1
if [ $? -eq 0 ]; then
    /sbin/modprobe xt_set
fi
chmod 755 /etc/sysconfig/modules/xt_set.modules
reboot #注意,这是重启服务器
检查:
lsmod | grep ip_vs
lsmod | grep xt_set

参考链接:
[## Docker+Nginx+Keepalived实现架构高可用
](https://www.cnblogs.com/jinjiangongzuoshi/p/9313438.html

相关文章

网友评论

    本文标题:docker内安装keepalived模拟高可用

    本文链接:https://www.haomeiwen.com/subject/vmswmqtx.html