我是靠谱客的博主 平淡斑马,最近开发中收集的这篇文章主要介绍在docker安装nginx+keepalived实现高可用在docker安装nginx+keepalived实现高可用,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
在docker安装nginx+keepalived实现高可用
部分参考:https://www.cnblogs.com/jinjiangongzuoshi/p/9313438.html
1. 编辑dockerfile
提前下载好nginx和keepalived,并放到dockerfile同一目录
keepalived:https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
nginx:http://nginx.org/download/nginx-1.18.0.tar.gz
# Docker image for springboot file run
# VERSION 0.0.1
# Author: jhf
# 基础镜像使用java
FROM centos:8
# 作者
MAINTAINER jhf <jianghuafei12@163.com>
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
# VOLUME F://docker//tmp
# 将jar包添加到容器中并更名为app.jar
# ADD js_auth-0.1.jar app.jar
# 运行命令
# 安装基础依赖
RUN bash -c 'yum install -y openssh-server vim lrzsz wget gcc-c++ pcre pcre-devel zlib zlib-devel ruby openssl openssl-devel patch bash-completion zlib.i686 libstdc++.i686 lsof unzip zip net-tools psmisc'
# 安装service
RUN bash -c 'yum install -y initscripts'
#
RUN bash -c 'yum install -y net-tools.x86_64'
# 安装openssh
RUN bash -c 'yum install -y openssh-server'
# 启动ssh
# RUN bash -c 'service sshd restart'
# 安装passwd
RUN bash -c 'yum install -y passwd'
# 使用yum安装nginx需要包括Nginx的库,安装Nginx的库
RUN bash -c 'rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm'
# 安装网络包(需要使用ifconfig和ping命令)
RUN bash -c 'yum install -y net-tools'
#安装keepalived环境依赖
RUN bash -c 'yum install -y gcc openssl-devel popt-devel'
#安装make环境依赖
RUN bash -c 'yum -y install gcc automake autoconf libtool make'
# 使用下面命令安装nginx(ADD命令会自动解压缩,如果不需要应换成COPY)
COPY nginx-1.17.10.tar.gz /data/nginx-1.17.10.tar.gz
RUN bash -c 'cd /data && tar zxvf nginx-1.17.10.tar.gz'
# 指定工作目录
WORKDIR /data/nginx-1.17.10
# 编译安装
RUN bash -c './configure --prefix=/usr/local/nginx --with-http_ssl_module && make && make install'
#安装keepalived
# https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
# RUN bash -c 'wget -P /data https://www.keepalived.org/software/keepalived-2.0.20.tar.gz'
# 将下载好的包放入/data下
COPY keepalived-2.0.20.tar.gz /data/keepalived-2.0.20.tar.gz
RUN bash -c 'cd /data && tar zxvf keepalived-2.0.20.tar.gz'
# 指定工作目录
WORKDIR /data/keepalived-2.0.20
# 编译安装
RUN bash -c './configure --prefix=/usr/local/keepalived && make && make install'
# 复制文件到对应目录
RUN bash -c 'cp keepalived/etc/init.d/keepalived /etc/init.d/ && mkdir /etc/keepalived && cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ && cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/ && cp /usr/local/keepalived/sbin/keepalived /usr/sbin/'
# 设置为开机启动
RUN bash -c 'systemctl enable keepalived.service'
# 设置密码
RUN echo "root:root" | chpasswd
在dockerfile目录下执行docker命令,构建镜像
docker bulid -t nginx_keepalived:0.0.1 .
构建镜像完成后,执行命令
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_keepalived latest a57cacd6613a 2 hours ago 835MB
启动dockers镜像
# 挂载nginx配置文件,master
docker run -itd -p 22:22 -p 80:80 --name nginx_master --privileged=true -v /f/docker/nginx_master/nginx:/etc/nginx nginx_master:0.0.1 /usr/sbin/init
# 挂载nginx配置文件,slaver(同一台机器上的配置)
docker run -itd -p 23:22 -p 81:80 --name nginx_master --privileged=true -v /f/docker/nginx_master/nginx:/etc/nginx nginx_slaver:0.0.1 /usr/sbin/init
修改keepalived配置文件,master
! Configuration File for keepalived
global_defs {
router_id lvs_1
script_user root
enable_script_security
notification_email_from jianghuafei12@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
}
vrrp_script chk_nginx
{
# 健康检查脚本,当脚本返回值不为0时认为失败
script "/etc/keepalived/nginx_check.sh"
# 检查频率,以下配置每2秒检查1次
interval 3
# 当检查失败后,将vrrp_instance的priority减小5
weight -5
# 连续监测失败3次,才认为真的健康检查失败。并调整优先级
fall 3
# 连续监测1次成功,就认为成功。但不调整优先级
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.200
}
track_script {
chk_nginx ## 执行 Nginx 监控的服务
}
}
修改keepalived配置文件,slaver
! Configuration File for keepalived
global_defs {
router_id lvs_2
script_user root
enable_script_security
notification_email_from jianghuafei12@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
}
vrrp_script chk_nginx
{
#健康检查脚本,当脚本返回值不为0时认为失败
script "/etc/keepalived/nginx_check.sh"
#检查频率,以下配置每2秒检查1次
interval 3
#当检查失败后,将vrrp_instance的priority减小5
weight -5
#连续监测失败3次,才认为真的健康检查失败。并调整优先级
fall 3
#连续监测1次成功,就认为成功。但不调整优先级
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.200
}
track_script {
chk_nginx ## 执行 Nginx 监控的服务
}
}
2. 启动服务
更改完配置文件后执行
systemctl daemon-reload
systemctl restart keepalived.service
设置开机启动
systemctl enable keepalived.service
启动nginx
/usr/local/nginx/sbin/nginx
查看keepalived状态
服务启动后在master服务器执行命令,ip add
找到配置的虚拟ip,则服务启动成功
ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
link/tunnel6 :: brd ::
250: eth0@if251: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
inet 172.17.0.210/32 scope global eth0
valid_lft forever preferred_lft forever
3. keepalived基本命令
systemctl daemon-reload 重新加载
systemctl enable keepalived.service 设置开机自动启动
systemctl disable keepalived.service 取消开机自动启动
systemctl start keepalived.service 启动
systemctl stop keepalived.service停止
systemctl status keepalived.service 查看服务状态
4. checknginx
#!/bin/bash
A=`ps -ef | grep nginx | grep -v grep | wc -l`
if [ $A -eq 0 ];then
echo "Nginx is running"
else
echo "Nginx is down,Starting..."
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -ef | grep nginx | grep -v grep | wc -l` -eq 0 ];then
killall keepalived
fi
fi
注意:如果无法执行脚本,需要vi编辑脚本文件,:set ff=unix
注意点
在master和backup的keepalived的配置文件中,网卡名字为当前机器的网卡名称
VIP的选择为 可用的一个ip,通常在做高可用,局域网环境比较多, 所以这个vip是和两台机器同网段的一个内网ip。 如果用在外网环境下,无所谓在不在一个网段,只要客户端能访问到。
chmod +x nginx_check.sh
停掉nginx服务,启动keepalived服务,会看到keepalived拉动nginx服务启动
最后
以上就是平淡斑马为你收集整理的在docker安装nginx+keepalived实现高可用在docker安装nginx+keepalived实现高可用的全部内容,希望文章能够帮你解决在docker安装nginx+keepalived实现高可用在docker安装nginx+keepalived实现高可用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复