专栏首页测试开发架构之路centos7安装keepalived问题

centos7安装keepalived问题

● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled) Active: failed (Result: resources) since Sat 2018-08-04 13:06:35 EDT; 16s ago Process: 3435 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)

Aug 04 13:06:35 localhost.localdomain systemd[1]: Starting LVS and VRRP High Availability Monitor... Aug 04 13:06:35 localhost.localdomain systemd[1]: PID file /var/run/keepalived.pid not readable (yet?) after start. Aug 04 13:06:35 localhost.localdomain systemd[1]: Failed to start LVS and VRRP High Availability Monitor. Aug 04 13:06:35 localhost.localdomain systemd[1]: Unit keepalived.service entered failed state. Aug 04 13:06:35 localhost.localdomain systemd[1]: keepalived.service failed.

搭建keepalived注意事项:

环境

主nginx负载均衡器:192.168.5.129 (通过keepalived配置了VIP:192.168.5.101/0供外使用) 副nginx负载均衡器:192.168.5.130(通过keepalived配置了VIP:192.168.5.101/0供外使用)

1.安装 keepalived-1.4.1

 tar xf keepalived-1.3.4.tar.gz
 cd keepalived-1.3.4
 ./configure --prefix=/usr/local/keepalived
 make && make install

2.配置keepalived文件

复制/sbin/keepalived到/usr/sbin下
> cp /keepalived/sbin/keepalived /usr/sbin/

keepalived默认会读取/etc/keepalived/keepalived.conf配置文件
> mkdir /etc/keepalived
> cp /keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

复制sysconfig文件到/etc/sysconfig下
> cp /keepalived/etc/sysconfig/keepalived /etc/sysconfig/

复制启动脚本到/etc/init.d下
> cd /keepalived-1.3.4
> cp ./keepalived/etc/init.d/keepalived /etc/init.d/
> chmod 755 /etc/init.d/keepalived
  

3.配置keepalived.conf启动文件

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER(BACKUP)
    interface ens33    -- 注意网卡名字
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    virtual_ipaddress {
        192.168.5.100
        192.168.5.101
    }
}

4.启动与验证keepalievd  <ip addr list>

service keepalived start
systemctl status keepalived.service

 5. 遗留问题:

当服务启动后一段时间就自动关闭了服务;(大约几分钟后)

6. 设置centos7开机自动启动服务

vim /lib/systemd/system/keepalived.service
[Unit]
Description=Keepalived
After=syslog.target network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
ExecStart=/usr/local/keepalived/sbin/keepalived -D
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
1;systemctl daemon-reload  重新加载

2:systemctl enable keepalived.service  设置开机自动启动

3:systemctl disable keepalived.service 取消开机自动启动

4:systemctl start keepalived.service 启动

5:systemctl stop keepalived.service停止

设置nginx开机自启动

vim /lib/systemd/system/keepalived.service

[Unit]
Description=nginx 
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target
systemctl enable nginx.service

systemctl start nginx.service    启动nginx

systemctl stop nginx.service    结束nginx

systemctl restart nginx.service    重启nginx

7.配置监听文件

vi /root/check_nginx.sh

#! /bin/bash
if [`ps -C nginx --no-header |wc -l` -eq 0 ]
 then
    systemctl start nginx.service
    sleep 2
    if [`ps -C nginx --no-header |wc -l` -eq 0]
         then
                systemctl stop keepalived.service
    fi
fi
chmod +x /root/check_nginx.sh
加入到作业计划
crontab -e
*/1 * * * * /root/check_nginx.sh >>/root/check_nginx.log

追加keepalived.conf文件

vrrp_script check_nginx {                   ######定义监控nginx的脚本
    script "/root/check_nginx.sh"
    interval 2                            ######监控时间间隔
    weight 2                              ######负载参数
}

vrrp_instance vrrptest {
track_script {                       
    check_nginx
}

8.知识补充:学习shell编程

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • CentOS7 实现 Keepalived + Nginx 实现高可用 Web 负载均衡

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

    PHP开发工程师
  • 听说Memcache你很豪横?-------------深入剖析Memcache+keepalive高可用群集

    memcached群集作用主要解决服务器单点故障。不做群集部署,服务器之间不会复制 同步数据,一旦宕机数据就会丢失,无法有效的为后台web服务器等减轻并发连接...

    不吃小白菜
  • Keepalived+Nginx+Apache主备及双活搭建测试

          keepalived+nginx高可用有主备和双活两种方式。主备方式下对外提供一个vip,同时只有一台服务器工作,另一台作备机;双活方式下对外提供两...

    loong576
  • SaltStack——配置管理(grains实战-keepalived)

    keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived是自动完成...

    江小白
  • 2万字,实战 Docker 部署:完整的前后端,主从热备高可用服务!!

    1、 解决物理机不够用的问题 2、 解决物理机资源使用不充分问题 3、 解决系统高可用问题 4、 解决不停机更新问题

    搜云库技术团队
  • CentOS7 下 keepalived 的安装和配置

    安装前准备: yum -y install gcc gcc-c++ autoconf automake make  yum -y install zlib zl...

    庞小明
  • Kubernetes v1.18.2 二进制高可用部署(修正版)

    二进制包下载地址:https://github.com/etcd-io/etcd/releases/download/v3.4.7/etcd-v3.4.7-li...

    YP小站
  • Kubernetes v1.18.2 二进制高可用部署

    二进制包下载地址:https://github.com/etcd-io/etcd/releases/download/v3.4.7/etcd-v3.4.7-li...

    YP小站
  • 高可用高性能分布式文件系统FastDFS进阶keepalived+nginx对多tracker进行高可用热备

      在上一篇 分布式文件系统FastDFS如何做到高可用 中已经介绍了FastDFS的原理和怎么搭建一个简单的高可用的分布式文件系统及怎么访问。   高可用是实...

    欢醉

扫码关注云+社区

领取腾讯云代金券