概述
文章目录
- 1、在服务器中安装 centos 镜像
- 2、进入centos_01容器安装keepalived+nginx
前言环境:
前提条件 -> 已在 linux 中安装好docker环境。
其实早在9月份已有过项目上的实践,但在本机(只有一台虚拟机 linux )不想多装虚拟机的情况下如何实验呢?那就用docker 吧!!
本次实验架构图:
1、在服务器中安装 centos 镜像
下载镜像:
docker pull centos:7.7.1908
查看镜像:
docker images
安装第一个centos容器:centos_01
#f1cb7c7d58b7 就是上图中的镜像ID
sudo docker run -it --name centos_01 f1cb7c7d58b7
#然后退出容器
exit;
这时我们再看一下启动的容器:
docker ps
2、进入centos_01容器安装keepalived+nginx
进入容器:
#efbad978d17f 是容器ID
docker exec -it efbad978d17f /bin/bash
进入容器后开始安装keepalived 和 nginx
安装keepalived :
#安装依赖环境
yum install -y gcc openssl-devel popt-devel
#安装keepalived
yum install -y keepalived
#或者本地安装指定版本
wget https://www.keepalived.org/software/keepalived-1.3.4.tar.gz
安装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
#安装网络包(需要使用ifconfig和ping命令)
yum install -y net-tools
修改/etc/keepalived/keepalived.conf文件:
#进入到 容器 centos_01 的根目录
cd /
#进入 keepalived 安装目录
cd /etc/keepalived
# 打开配置文件
vi keepalived.conf
修改/etc/keepalived/keepalived.conf文件
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.210
}
track_script {
chk_nginx
}
}
修改 hosts 文件
vi /etc/hosts
创建 /etc/keepalived/check_nginx.sh 脚本文件
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
再对check_nginx.sh赋于执行权限:
chmod +x check_nginx.sh
注:keepalived是通过检测keepalived进程是否存在判断服务器是否宕机,如果keepalived进程在但是nginx进程不在了那么keepalived是不会做主备切换,所以我们需要写个脚本来监控nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉。
在主nginx上需要编写nginx进程检测脚本(check_nginx.sh),判断nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉,并将vip漂移到备份机器上
设置 keepalived 开机启动
#设置开机自动启动
systemctl enable keepalived.service
启动keepalived服务:
# 启动
systemctl start keepalived.service
安装所有需要的依赖和环境后,将容器新增的内容重新提交到镜像文件,制作成新的镜像以供使用
#查看容器ID
docker ps
#把这个容器打包为一个镜像,5d112 就是容器 centos_01 的ID
docker commit 5d112 centos_keepalived_nginx:v1
执行完成之后,我们用 docker images 命令看一下
docker images
这个镜像就是我们前面打包后的镜像。
启动含有(keepalived+nginx)的容器。
docker run --privileged=true -tid --name keepalived_master centos_keepalived_nginx:v1 /usr/sbin/init
进入keepalived_master容器:
docker exec -it keepalived_master bash
进入/usr/share/nginx/html,修改index.html文件
修改标题为:
Welcome to nginx Master!
启动keepalived_salve容器:
#启动一个容器
docker run --privileged=true -tid --name keepalived_slave centos_keepalived_nginx:v1 /usr/sbin/init
#进入容器
docker exec -it keepalived_slave bash
修改keepalived_salve容器中nginx index.html文件
vim /usr/share/nginx/html/index.html
修改标题为:
Welcome to nginx Slave!
修改keepalived_salve容器中keepalived.conf文件 (master容器中,保持和镜像中设置一样即可,不需要更改)
! Configuration File for keepalived
global_defs {
notification_email {
762357658@qq.com
}
notification_email_from itsection@example.com
smtp_server mail.example.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 2
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.210
}
track_script {
chk_nginx
}
}
其实,由配置中可以看出,主要是state和priority两个参数的调整,其中master节点的priority值一定要比backup大才行!
原理说明:
1、 通过vrrp协议广播,每个keepalived vrrp都去争取master
2、 以virtual_router_id为组队标识。 同为一个vip服务的keepalived的virtual_router_id要保持相同
3、 以priority 为权值,同一个virtual_router_id下那个priority大那个就是master,其它为backup
改完之后,重新加载
systemctl daemon-reload
systemctl restart keepalived.service
验证
查看两个容器中keepalived服务状态
systemctl status keepalived.service
最后
以上就是默默耳机为你收集整理的一台Linux服务器通过docker安装两个centos镜像,实现 keepalived+nginx 的(VRRP)高可用代理环境的全部内容,希望文章能够帮你解决一台Linux服务器通过docker安装两个centos镜像,实现 keepalived+nginx 的(VRRP)高可用代理环境所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复