千家信息网

CentOS7源码安装和部署keepalived-2.1.5

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,本篇内容主要讲解"CentOS7源码安装和部署keepalived-2.1.5",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"CentOS7源码安装和部署k
千家信息网最后更新 2025年01月31日CentOS7源码安装和部署keepalived-2.1.5

本篇内容主要讲解"CentOS7源码安装和部署keepalived-2.1.5",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"CentOS7源码安装和部署keepalived-2.1.5"吧!

一、基础环境

系统版本nginx版本keepalived版本ip作用
CentOS Linux release 7.6nginx/1.18.0keepalived-2.1.5192.168.86.135master
CentOS Linux release 7.6nginx/1.18.0keepalived-2.1.5192.168.86.136slave

VIP:192.168.86.137

二、编译环境安装

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel

三、安裝nginx

  1. 安裝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            # 重新载入配置文件
  2. 修改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;                }        }}
  3. 启动nginx

    systemctl start nginx
  4. 测试nginx启动

    curl localhostthis is master

四、安装keepalived

1、 创建依赖环境

yum -y install libnl libnl-devel gcc openssl-devel libnl3-devel pcre-devel net-snmp-agent-libs libnfnetlink-devel curlmkdir /etc/keepalivedwget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz

2、安装keepalived

tar -zxvf keepalived-2.1.5.tar.gzcd keepalived-2.0.15./configure --prefix=/usr/local/keepalived-2.1.5 make && make install

3、创建启动文件

ln -s /usr/local/keepalived-2.1.5 /usr/local/keepalivedmkdir /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/

4、创建配置文件

  1. master

    cat > /etc/keepalived/keepalived.conf << EOF# vi /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_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
  2. slave

    cat > /etc/keepalived/keepalived.conf << EOF# vi /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_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

5、启编写Nginx 状态检测脚本

编写 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/bashA=`ps -C nginx -no-header |wc -l`if [ $A -eq 0 ];then/usr/local/nginx/sbin/nginxsleep 2if [ `ps -C nginx --no-header |wc -l` -eq 0 ];thenkillall keepalivedfifiEOF

保存后,给脚本赋执行权限:

# chmod +x /etc/keepalived/nginx_check.sh

6、启动keepalived

systemctl start keepalivedsystemctl enable keepalived

五、测试

1、在两台服务器上测试

  1. master

    $ curl localhostthis is masterroot@centos7[14:46:07]:~$ curl 192.168.86.135this is masterroot@centos7[15:03:29]:~
  2. slave

    $ curl localhostthis is slaveroot@centos7[15:03:59]:/etc/keepalived$ curl 192.168.86.136this is master

2、关闭master的keepalived模仿down机

  1. master关闭keepalived

    $ systemctl stop keepalived
  2. 在slave上面进行测试

$  curl localhostthis is slaveroot@centos7[15:10:29]:/etc/keepalived$ curl 192.168.86.136this is slave

到此keepalived完成

到此,相信大家对"CentOS7源码安装和部署keepalived-2.1.5"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0