Amazing Arch

docker内安装keepalived模拟高可用

2019-03-19  本文已影响81人  ThinkJava

目标&效果图

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

实战

宿主机环境配置

yum -y install docker #安装Docker
systemctl start docker #启动Docker服务
docker pull centos #pull centos镜像,这里可以和git操作类比
#安装网络包(需要使用ifconfig和ping命令)
yum install net-tools
#安装vim
yum install vim
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容器环境配置

yum install -y gcc openssl-devel popt-devel #安装依赖环境
yum install keepalived #安装keepalived
#使用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
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是否存活的脚本
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
systemctl daemon-reload  #重新加载配置
systemctl start keepalived.service #启动keepalived服务
systemctl status keepalived.service #查看当前状态
Master容器 systemctl status keepalived.service
vim /usr/share/nginx/html/index.html
Nginx默认页面修改效果图
nginx #启动nginx,如果报端口占用,请看下边「遇到的问题」模块
ifconfig #查看当前ip
ifconfig效果图
curl 172.17.0.3 #请求nginx
curl 172.17.0.3效果图

保存Docker Master镜像

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

Slave容器环境配置

keepalived_slave容器进程
#进入 c5ea97ccc82d [Slave]这个容器
docker exec -it c5ea97ccc82d bash 
slave容器的keepalived配置
  state BACKUP #keepalived主服务
  priority 100 #选举的优先级,Master设置的101
  virtual_ipaddress #虚拟地址ip,多个ip可以以多行的方式添加
  track_script #指定检测脚本,此处为/etc/keepalived/nginx_check.sh
Slave容器 systemctl status keepalived.service效果图
nginx #启动nginx,如果报端口占用,请看下边「遇到的问题」模块
ifconfig #查看ip
Slave容器ip地址
 curl 172.17.0.2
Slave容器 Nginx默认页面

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

验收测试Keepalived、VIP

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

其他测试用例

遇到的问题

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

上一篇 下一篇

猜你喜欢

热点阅读