本篇内容主要讲解“CentOS7源码安装和部署keepalived-2.1.5”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“CentOS7源码安装和部署keepalived-2.1.5”吧!
系统版本 | nginx版本 | keepalived版本 | ip | 作用 |
---|---|---|---|---|
CentOS Linux release 7.6 | nginx/1.18.0 | keepalived-2.1.5 | 192.168.86.135 | master |
CentOS Linux release 7.6 | nginx/1.18.0 | keepalived-2.1.5 | 192.168.86.136 | slave |
VIP:192.168.86.137
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
安裝nignx
#进入目录 cd /tools #上传安装文件,并解压 tar -zxvf nginx-1.18.0.tar.gz #进入安装目录 cd nginx-1.18.0 #检查配置 ./configure --prefix=/usr/local/nginx make&&make install #查看nginx版本 /usr/local/nginx/sbin/nginx -v #配置nginx(检查)/usr/local/nginx/sbin/nginx -t #nginx管理命令 /usr/local/nginx/sbin/nginx # 启动 /usr/local/nginx/sbin/nginx -s stop # 停止 /usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
修改nginx配置文件
修改 Nginx 欢迎首页内容(用于后面测试, 用于区分两个节点的 Nginx):
master
echo 'this is master 135' > /usr/share/nginx/html/index.html
slave
echo 'this is slave 136' > /usr/share/nginx/html/index.html
修改nginx.conf
# vi /usr/local/nginx/conf/nginx.con
user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 88; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
启动nginx
systemctl start nginx
测试nginx启动
curl localhost this is master
yum -y install libnl libnl-devel gcc openssl-devel libnl3-devel pcre-devel net-snmp-agent-libs libnfnetlink-devel curl mkdir /etc/keepalived wget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz
tar -zxvf keepalived-2.1.5.tar.gz cd keepalived-2.0.15 ./configure --prefix=/usr/local/keepalived-2.1.5 make && make install
ln -s /usr/local/keepalived-2.1.5 /usr/local/keepalived mkdir /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived-2.1.5/etc/sysconfig/keepalived /etc/sysconfig/ n -s /usr/local/keepalived-2.1.5/sbin/keepalived /usr/sbin/ 下面文件要从keepalived源码目录复制,安装目录中没有cp cp /tools/keepalived-2.1.5/keepalived/keepalived.service /etc/systemd/system/ cp /tools/keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d/
master
cat > /etc/keepalived/keepalived.conf << EOF # vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { ## keepalived 自带的邮件提醒需要开启 sendmail 服务。 建议用独立的监控或第三方 SMTP router_id server1 ## 标识本节点的字条串,通常为 hostname } ## keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级。如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加。如果脚本执行结果非 0,并且 weight配置的值小于 0,则优先级相应的减少。其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值。 vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径 interval 2 ## 检测时间间隔 weight -20 ## 如果条件成立,权重-20 } ## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称 vrrp_instance VI_1 { state MASTER ## 主节点为 MASTER, 对应的备份节点为 BACKUP interface ens33 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同, 我的是 eth0 virtual_router_id 33 ## 虚拟路由的 ID 号, 两个节点设置必须一样, 可选 IP 最后一段使用, 相同的 VRID 为一个组,他将决定多播的 MAC 地址 mcast_src_ip 192.168.86.135 ## 本机 IP 地址 priority 100 ## 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高 nopreempt ## 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题 advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样, 默认 1s ## 设置验证信息,两个节点必须一致 authentication { auth_type PASS auth_pass 123456 ## 真实生产,按需求对应该过来 } ## 将 track_script 块加入 instance 配置块 track_script { chk_nginx ## 执行 Nginx 监控的服务 } # # 虚拟 IP 池, 两个节点设置必须一样 virtual_ipaddress { 192.168.86.137 ## 虚拟 ip,可以定义多个 } } EOF
slave
cat > /etc/keepalived/keepalived.conf << EOF # vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id server2 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 33 mcast_src_ip 192.168.86.136 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 123456 } track_script { chk_nginx } virtual_ipaddress { 192.168.86.137 } } EOF
编写 Nginx 状态检测脚本 /etc/keepalived/nginx_check.sh (已在 keepalived.conf 中配置)脚本要求:如果 nginx 停止运行,
尝试启动,如果无法启动则杀死本机的 keepalived 进程, keepalied将虚拟 ip 绑定到 BACKUP 机器上。 内容如下:
cat > /etc/keepalived/nginx_check.sh << EOF # vi /etc/keepalived/nginx_check.sh #!/bin/bash A=`ps -C nginx –no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi EOF
保存后,给脚本赋执行权限:
# chmod +x /etc/keepalived/nginx_check.sh
systemctl start keepalived systemctl enable keepalived
master
$ curl localhost this is master root@centos7[14:46:07]:~ $ curl 192.168.86.135 this is master root@centos7[15:03:29]:~
slave
$ curl localhost this is slave root@centos7[15:03:59]:/etc/keepalived $ curl 192.168.86.136 this is master
master关闭keepalived
$ systemctl stop keepalived
在slave上面进行测试
$ curl localhost this is slave root@centos7[15:10:29]:/etc/keepalived $ curl 192.168.86.136 this is slave
到此keepalived完成
到此,相信大家对“CentOS7源码安装和部署keepalived-2.1.5”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。