我是靠谱客的博主 标致过客,最近开发中收集的这篇文章主要介绍【nginx 配合 keepalived实现高可用】1、docker 部署2、linux直接部署,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、docker 部署

  1.1、docker部署nginx

(1) 搜索nginx版本

	docker search nginx
docker 搜索nginx 版本结果图 !

(2)下拉对应版本的nginx
          默认为最新版本

	docker pull nginx

(3)部署容器

	[root@localhost /] docker run --name nginx_test -p 80:80 
		-v ~/nginx/conf/conf.d:/ect/nginx/conf.d 
		-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 
		-v ~/nginx/html:/usr/share/nginx/html 
		-v ~/nginx/logs:/var/log/nginx 
		-d nginx

挂载文件:conf.d、nginx.conf、html文件、log/nginx日志文件
挂载地址:~/nginx/conf/(自定义位置方便修改)

(4)容器操作

	#启动容器
	docker start nginx_test
	#关闭容器
	docker stop nginx_test
	#重启容器
	docker restart nginx_test
	# 删除容器
	docker rm nginx_test # 一定要关闭容器后才能删除
	# 容器改名
	docker rename nginx_text nginx01 # 原名 nginx_test 改名 nginx01 

(5)修改配置文件


1.2、docker 部署keepalived

(1) 搜索keepalived版本

	docker search keepalived

(2) 拉取keepalived版本
        有不同的版本,根据需求选择,推荐使用人数较多的一个版本 alterway/keepalived

	docker pull alterway/keepalived

(3)部署容器
        可以通过 ip address 查看网卡 ,设置绑定的网卡

		docker run --cap-add=NET_ADMIN --cap-add=NET_BROADCAST --cap-add=NET_RAW --net=host --name keepalive_test 
		-v /etc/localtime:/etc/localtime 
		-e KEEPALIVED_INTERFACE='ens33' 
		-e KEEPALIVED_STATE='MASTER' 
		-e KEEPALIVED_ROUTER_ID='51' 
		-e KEEPALIVED_UNICAST_PEERS="#PYTHON2BASH['192.168.137.3','192.168.137.4']" 
		-e KEEPALIVED_VIRTUAL_IPS='192.168.137.150' 
	 	-d alterway/keepalived

       KEEPALIVED_INTERFACE='ens33' # 绑定网卡 ens33
       192.168.137.3,192.168.137.4 # 服务器 IP
       192.168.137.150 # 虚拟ip
        以上操作需要在多台服务器部署


2、linux直接部署

2.1、linux部署nginx

    nginx官网下载地址

(1)通过xftp发送至linux服务器中
xftp 截图 !
(2) 解压缩nginx.tar.gz文件
    在当前目录,将 nginx-1.21.6.tar.gz 文件解压为 nginx 文件

	tar -zcvf nginx-1.21.6.tar.gz nginx

(3)yum安装nginx依赖
    1.基础依赖: gcc 、openssl-devel、pcre-devel、zlib-devel

	 yum install -y gcc openssl-devel pcre-devel zlib-devel

    Nginx的依赖包介绍安装

    2.安装nginx的相关依赖

	yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

    3.在nginx安装目录下执行如下指令:

	./configure --prefix=/usr/local/nginx
	make && make install

(4)nginx操作

	# 启动nginx
	/usr/local/nginx/sbin/nginx
	# 关闭	此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
	/usr/local/nginx/sbin/nginx -s stop
	# 重启
	/usr/local/nginx/sbin/nginx -s reload
	# 停止 此方式停止步骤是待nginx进程处理任务完毕进行停止。
	/usr/local/nginx/sbin/nginx -s quit

2.2、linux部署keepalived

(1)下载 keepalived
   keepalived官网下载
(2) 解压缩 .tar.gz文件

	tar -zcvf keepalived-2.2.7.tar.gz keepalived-2.2.7
在 keepalived-2.2.7 文件夹中执行 
./configure --prefix=/usr/local/keepalived
make && make install
文件会安装到 /usr/local/keepalived 文件夹下面

源文链接:Linux下Keepalived安装与配置

完成后会在以下路径生成:
/usr/local/etc/keepalived/keepalived.conf
/usr/local/etc/sysconfig/keepalived
/usr/local/sbin/keepalived

(3) 初始化以及启动

# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 
 
# 将keepalived主程序加入到环境变量(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
 
# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用
[root@localhost /]# cp /usr/local/src/keepalived-2.0.7/keepalived/etc/init.d/keepalived  /etc/init.d/keepalived
 
# 将配置文件放到默认路径下
[root@localhost /]# mkdir /etc/keepalived
[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

加为系统服务:chkconfig –add keepalived
启动、关闭、重启:service keepalived start/stop/restart

启动时,若报错显示:

	Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.

解决方法: 源文链接 解决keepalived启动错误
    /usr/sbin/ 中的keepalived软链接有问题,需要删除,然后重新从keepalived/sbin/目录中
    将keepalived文件创建软链接到/usr/sbin目录

	rm /usr/sbin/keepalived
	ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

再次启动就能成功服务 systemctl start keepalived

(4) 修改配置文件
keepalived.conf

global_defs {               
   router_id Nginx_01
}
vrrp_script check_nginx {
	script "/etc/keepalived/check_nginx.sh"
	interval 2
    weight -5
    fall 3
    rise 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     192.168.90.200
    }
    track_script {
    	check_nginx
    }
}

注意:主、从服务器配置文件有部分差异,虚拟vip必须在 网卡网段内 如果是双网卡,可以将内外网分离,将vip绑定外网 ip, nginx使用内网ip
相同: virtual_router_id 51
不同: router_id、priority 150

服务器virtual_router_idrouter_idpriority
主服务器51Nginx_01100
从服务器51Nginx_0290

最后

以上就是标致过客为你收集整理的【nginx 配合 keepalived实现高可用】1、docker 部署2、linux直接部署的全部内容,希望文章能够帮你解决【nginx 配合 keepalived实现高可用】1、docker 部署2、linux直接部署所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部