基于keepalived实现高可用nginx服务
环境及配置前提说明
主机1,ip:192.168.25.140 运行web服务
主机2,ip:192.168.25.141 运行web服务
主机3,ip:192.168.25.138 运行nginx服务和keepalived服务
主机4,ip:192.168.25.139 运行nginx服务和keepalived服务
1两台nginx主机编译安装nginx服务
# 下载源码包 ]# yum install -y make ]# yum install -y gcc ]# yum -y groupinstall "Development Tools" "Server Platfrom Development" ]# yum install pcre-devel openssl-devel zlib-devel -y 手动解决最重要的依赖关系包 ]# useradd -r nginx 增加一个系统用户 ]# tar -xf nginx-1.10.0.tar.gz ]# cd nginx-1.10.0/ nginx-1.10.0]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/va/log/nginx.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio ]# make && make install ]# vim /etc/profile.d/nginx.sh export PATH=/usr/local/nginx/sbin:$PATH ]# . /etc/profile.d/nginx.sh ]# nginx -t 检测错误 ]# nginx -h 可查看命令使用帮助 启动服务,检测端口是否已经启用 ]# /usr/local/nginx/sbin/nginx 启动服务 ]# ss -tnlp 查看80端口是否已经启用,注意,如果httpd服务已经启用了话 这里会启动失败
2两台nginx主机安装keepalived服务
]# yum install keepalived -y
2.1为两台keepalived主机编辑邮件通知脚本
]# vim notify.sh #!/bin/bash # contact='root@localhost' notify() { mailsubject="$(hostname) to be $1, vip floating" mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1" echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master ;; backup) notify backup ;; fault) notify fault ;; *) echo "Usage: $(basename $0) {master|backup|fault}" exit 1 ;; esac
2.2将脚本移动至/etc/keepalived/目录下,并且给脚本执行权限
]# mv notify.sh /etc/keepalived/ ]# chmod +x notify.sh
3配置nginx的反代配置文件
]# vim /etc/nginx/nginx.conf 编辑主配置文件,在http上下文编辑upstream server,如下;
4配置两台keepalived节点主机配置文件
]# vim keepalived.conf,详细配置如下;
4.1节点一配置
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 172.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.100.19 } vrrp_script chk_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight -5 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -5 } vrrp_script chk_nginx vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass cb77b8da } virtual_ipaddress { 192.168.25.142/32 dev eth0 #nginx外网地址 } track_script { chk_down chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
4.2节点二配置
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 172.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.100.19 } vrrp_script chk_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" interval 1 weight -5 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -5 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 1 priority 98 advert_int 1 authentication { auth_type PASS auth_pass cb77b8da } virtual_ipaddress { 192.168.25.142/32 dev eth0 #nginx外网地址 } track_script { chk_down chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
5为两台RS web服务器编辑主页以便进行测试
]# vim /var/www/html/index.html <h1>RS 1</h1> #RS1的主页 <h1>RS 2</h1> #RS2的主页
6进行访问测试
原创文章,作者:M20-1马星,如若转载,请注明出处:http://www.178linux.com/58159