Keepaliev+Nginx+http
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,Ngxin作为一个强大的开源软件是可以先做为高可用集群服务的,这篇博文就介绍一下nginx+Keepalived是如何实现高性能+高可用集群服务的环境介绍:硬件: 4台虚拟服务器系统:CentOS 7
千家信息网最后更新 2025年12月01日Keepaliev+Nginx+http
Ngxin作为一个强大的开源软件是可以先做为高可用集群服务的,这篇博文就介绍一下nginx+Keepalived是如何实现高性能+高可用集群服务的
环境介绍:
硬件: 4台虚拟服务器
系统:CentOS 7.3
软件:Keepalived、Apache、Nginx
IP及主机名
Master
主机名:shiyan1
IP地址:172.18.17.31
Backup
主机名:shiyan2
IP地址:172.18.17.32
Httpd1
主机名:shiyan3
IP地址:172.18.17.33
Httpd2
主机名:shiyan4
IP地址:172.18.17.34
四台服务器初始化配置(四台服务器相同的配置)
关闭防火墙
[root@shiyan~ ]# systemctl disable firewalld [root@shiyan~ ]# systemctl stop firewalld [root@shiyan~ ]# iptables -F
关闭Selinux
[root@shiyan~ ]# vim /etc/selinux/config SELINUX=disabled #保存重启系统生效
安装软件
Master/Backup
[root@shiyan~ ]# yum install keepalived httpd nginx #(Nginx需要单独配置EPEL源)
Httpd1/Httpd2
[root@shiyan~ ]# yum install httpd
Httpd1配置
[root@shiyan3 ~ ]# mkdir -p /app/apache/html/ [root@shiyan3 ~ ]# chown -R apache.apache /app/apache/html/ [root@shiyan3 ~ ]# echo "Apache Server 1" > /app/apache/html/index.html [root@shiyan3 ~ ]# vim /etc/httpd/conf/httpd.conf #此处是更改httpd.conf中的内容,并非添加内容 DocumentRoot "/app/apache/html" #更改为自定义的路径 # # Relax access to content within /var/www. ##更改为自定义的路径 AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root:#更改为自定义的路径. [root@shiyan3 ~ ]# systemctl restart httpd #测试网站是否正常运行 [root@yum ~ ]# curl http://172.18.17.33 Apache Server 1 #测试成功
Httpd2配置
[root@shiyan4 ~ ]# mkdir -p /app/apache/html/ [root@shiyan4 ~ ]# chown -R apache.apache /app/apache/html/ [root@shiyan4 ~ ]# echo "Apache Server 2" > /app/apache/html/index.html [root@shiyan4 ~ ]# vim /etc/httpd/conf/httpd.conf #此处是更改httpd.conf中的内容,并非添加内容 DocumentRoot "/app/apache/html" #更改为自定义的路径 # # Relax access to content within /var/www. ##更改为自定义的路径 AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root:#更改为自定义的路径. [root@shiyan4 ~ ]# systemctl restart httpd #测试网站是否正常运行 [root@yum ~ ]# curl http://172.18.17.34 Apache Server 2 #测试成功
Master配置
配置Sorry-Server [root@shiyan1 ~ ]# mkdir -p /app/apache/html/ [root@shiyan1 ~ ]# chown -R apache.apache /app/apache/html/ [root@shiyan1 ~ ]# echo "Sorry Server 1
" > /app/apache/html/index.html [root@shiyan1 ~ ]# vim /etc/httpd/conf/httpd.conf #此处是更改httpd.conf中的内容,并非添加内容 Listen 8080 DocumentRoot "/app/apache/html" #更改为自定义的路径 # # Relax access to content within /var/www. ##更改为自定义的路径 AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root:#更改为自定义的路径. [root@shiyan1 ~ ]# systemctl restart http #测试网站是否正常运行 [root@yum ~ ]# curl http://172.18.17.31:8080 Sorry Server 1
#测试成功 配置Keepalived [root@shiyan1 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak} #备份文件 [root@shiyan1 ~ ]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { root #定义收邮件的用户 } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 172.18.17.31 #定义邮件地址 smtp_connect_timeout 30 router_id node1 #定义节点名称 } vrrp_instance VI_1 { state MASTER #定义节点为主节点模式 interface ens33 #定义使用ens33为VIP网卡 virtual_router_id 51 #定义节点编号 priority 150 #定义优先级 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.17.30 #定义VIP } }~ 配置Nginx服务 [root@shiyan1 ~ ]# vim /etc/nginx/nginx.conf #添加nginx集群 upstream websrvs { server 172.18.17.33:80; server 172.18.17.34:80; server 127.0.0.1:8080 backup; } #server 部分的内容需要全部注释掉 [root@shiyan1 ~ ]# vim /etc/nginx/conf.d/default.conf server { listen 80; location / { root html; proxy_pass http://websrvs; index index.html index.htm; } } [root@shiyan1 ~ ]# systemctl restart nginx [root@shiyan1 ~ ]# systemctl restart keepalived [root@shiyan1 ~ ]# systemctl restart httpd
Backup配置
配置Sorry-Server [root@shiyan2 ~ ]# mkdir -p /app/apache/html/ [root@shiyan2 ~ ]# chown -R apache.apache /app/apache/html/ [root@shiyan2 ~ ]# echo "Sorry Server 2
" > /app/apache/html/index.html [root@shiyan2 ~ ]# vim /etc/httpd/conf/httpd.conf #此处是更改httpd.conf中的内容,并非添加内容 Listen 8080 DocumentRoot "/app/apache/html" #更改为自定义的路径 # # Relax access to content within /var/www. ##更改为自定义的路径 AllowOverride None # Allow open access: Require all granted # Further relax access to the default document root:#更改为自定义的路径. [root@shiyan2 ~ ]# systemctl restart http #测试网站是否正常运行 [root@yum ~ ]# curl http://172.18.17.31:8080 Sorry Server 2
#测试成功 配置Keepalived [root@shiyan2 ~ ]# cp /etc/keepalived/keepalived.conf{,.bak} #备份文件 [root@shiyan2 ~ ]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { root #定义收邮件的用户 } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 172.18.17.31 #定义邮件地址 smtp_connect_timeout 30 router_id node1 #定义节点名称 } vrrp_instance VI_1 { state MASTER #定义节点为主节点模式 interface ens33 #定义使用ens33为VIP网卡 virtual_router_id 51 #定义节点编号 priority 150 #定义优先级 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.17.30 #定义VIP } }~ 配置Nginx服务 [root@shiyan2 ~ ]# vim /etc/nginx/nginx.conf #添加nginx集群 upstream websrvs { server 172.18.17.33:80; server 172.18.17.34:80; server 127.0.0.1:8080 backup; } #server 部分的内容需要全部注释掉 [root@shiyan2 ~ ]# vim /etc/nginx/conf.d/default.conf server { listen 80; location / { root html; proxy_pass http://websrvs; index index.html index.htm; } } [root@shiyan2 ~ ]# systemctl restart keepalived [root@shiyan2 ~ ]# systemctl restart nginx [root@shiyan2 ~ ]# systemctl restart httpd
测试环境
#默认使用rr算法依次轮询访问后端httpd服务器 [root@yum ~ ]# curl http://172.18.17.30 Apache Server 1 [root@yum ~ ]# curl http://172.18.17.30 Apache Server 2 #关闭后端http1服务,这样只能访问httpd2的服务 [root@yum ~ ]# curl http://172.18.17.30 Apache Server 2 [root@yum ~ ]# curl http://172.18.17.30 Apache Server 2 #关闭两台后端主机的httpd服务,这样因为没有后端服务器所以Master的sorry-server提供服务 [root@yum ~ ]# curl http://172.18.17.30Sorry Server 1
#关闭Master测试 [root@yum ~ ]# curl http://172.18.17.30Sorry Server 2
服务
配置
路径
内容
测试
节点
主机
地址
服务器
成功
网站
邮件
集群
运行
软件
优先级
名称
备份
文件
模式
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京市软件开发收费标准
通知 网络安全
泰拉瑞亚服务器怎么打开商店
学游戏软件开发读什么专业好
明日之后怎样跨服务器卖东西
删除数据库的日志文件
云服务器 鼠标
python写入数据库
会用服务器的公司有哪些
网络安全宣传精选手抄报图片
数据库系统的特点
哪些系统需要数据库
开源xml数据库
万得数据库如何使用说明
网络安全实施方案设计报告
浪潮服务器装系统先要做什么
边缘计算服务器品牌商
网络安全实训总结一千字
芜湖飞慧网络技术有限公司相亲
泰拉瑞亚如何买服务器
服务器维护工作
广电网络技术人员先进事迹
wow怀旧60能跨服务器组队吗
mysql数据库分类教程
校园网络安全季度总结
魔鬼服务器
在数据库的数据表中查询姓名
大连软件开发外企
内网服务器映射到公网 安全
杰迪森互联网科技有限公司