keepalived暂时找不到不用root安装或者运行方法,可能须要做网络层的虚拟ip映射,须要权限。html
\[nginx@pay-test ~\]$ sftp myc@1.13.22.112 sftp> get keepalived-1.3.9.tar.gz \[nginx@pay-test ~\]$ gzip -d keepalived-1.3.9.tar.gz \[root@pay-test nginx\]# cp keepalived-1.3.9.tar /usr/local/src/ \[root@pay-test nginx\]# cd /usr/local/src/ \[root@pay-test src\]# tar xvf keepalived-1.3.9.tar
\[root@pay-test keepalived-1.3.9\]# pwd /usr/local/src/keepalived-1.3.9 \[root@pay-test keepalived-1.3.9\]# ./configure --prefix=/usr/local/keepalived
[root@pay-test keepalived-1.3.9]# make && make install 要注意,最好分开执行,刚刚连一块儿执行失败没有生成/usr/local/keepalived目录,也没有任何提示。nginx
mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/src/keepalived-1.3.9/keepalived/etc/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ ln -s /usr/local/keepalived/sbin/keepalived /sbin/ (这个其实不须要,公司机器已自动拷贝) ln前要先删除,若是有的话。
[nginx@pay-test keepalived]$ pwd /etc/keepalived [nginx@pay-test keepalived]$ vi keepalived.confbash
! Configuration File for keepalived global_defs { router_id pay-test } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx_pid.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 110 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 1.13.22.118 } } # 很重要
119主机:网络
! Configuration File for keepalived global_defs { router_id pay-test2 } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx_pid.sh" interval 5 weight 2 } vrrp_instance VI_1 { state BACKUP # 很重要 interface eth0 virtual_router_id 51 priority 110 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 1.13.22.118 } }
[nginx@pay-test keepalived]$ vi check_nginx_pid.sh测试
#!/bin/bash LOG_DIR="/etc/keepalived" echo $(date "+%Y-%m-%d %H:%M:%S") "check nginx status" >> $LOG_DIR/log_nginx.log A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then /usr/sbin/nginx #重启nginx echo $(date "+%Y-%m-%d %H:%M:%S") "restarting nginx..." >> $LOG_DIR/restart.log sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移 killall keepalived echo $(date "+%Y-%m-%d %H:%M:%S") "killing keepalived..." >> $LOG_DIR/restart.log fi fi
[nginx@pay-test keepalived]$ chmod +x check_nginx_pid.shui
测试: nginx -s stop 自动生成日志:restart.log,ps一下nginx,发现重启成功。.net
check脚本增长: sleep 2rest
失败: [root@pay-test ~]# service keepalived start Reloading systemd: [ OK ] Starting keepalived (via systemctl): Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details. [FAILED]日志
查看日志: systemctl status keepalived.servicecode
Starting keepalived: /bin/bash: keepalived: command not found 解决: /usr/sbin/里必须有keepalived(make install没执行,没生成/usr/local/keepalived致使)
成功: [root@pay-test ~]# service keepalived start Starting keepalived (via systemctl): [ OK ]
\[nginx@pay-test keepalived\]$ ps -ef|grep keepalived root 27566 1 0 17:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D root 27567 27566 0 17:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D root 27568 27566 0 17:21 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D nginx 37088 21123 0 17:47 pts/1 00:00:00 grep --color=auto keepalived
\[root@zhifu-db1 keepalived\]# service keepalived stop Stopping keepalived (via systemctl): \[ OK \]
或者: killall keepalived
Keepalived默认的日志文件保存在/var/log/messages 修改/etc/sysconfig/keepalived为:KEEPALIVED_OPTIONS="-D -d -S 0"
[nginx@pay-test sysconfig]$ vi keepalived #KEEPALIVED_OPTIONS="-D" KEEPALIVED_OPTIONS="-D -S 0"
[root@pay-test ~]# vi /etc/rsyslog.conf
# Save boot messages also to boot.log local7.* /var/log/boot.log local0.* /var/log/keepalived.log
重启rsyslog:
\[root@pay-test ~\]# /etc/init.d/rsyslog restart -bash: /etc/init.d/rsyslog: No such file or directory \[root@pay-test /\]# /etc/sysconfig/rsyslog restart -bash: /etc/sysconfig/rsyslog: Permission denied \[root@pay-test rsyslog\]# /etc/rsyslog.conf restart -bash: /etc/rsyslog.conf: Permission denied \[root@pay-test rsyslog\]# systemctl restart rsyslog \[root@pay-test rsyslog\]#
重启keepalived:
\[root@pay-test rsyslog\]# /etc/init.d/keepalived restart Restarting keepalived (via systemctl): \[ OK \] \[root@pay-test rsyslog\]# tail -f /var/log/keepalived.log tail: cannot open ‘/var/log/keepalived.log’ for reading: No such file or directory tail: no files remaining
重启日志:
Jun 27 09:53:32 pay-test systemd: Stopping LVS and VRRP High Availability Monitor… Jun 27 09:53:32 pay-test Keepalived\[123109\]: Stopping Jun 27 09:53:32 pay-test Keepalived_healthcheckers\[123110\]: Stopped Jun 27 09:53:33 pay-test Keepalived_vrrp\[123111\]: Stopped Jun 27 09:53:33 pay-test Keepalived\[123109\]: Stopped Keepalived v1.3.9 (10/21,2017) Jun 27 09:53:33 pay-test systemd: Starting LVS and VRRP High Availability Monitor… Jun 27 09:53:33 pay-test Keepalived\[123755\]: Starting Keepalived v1.3.9 (10/21,2017) Jun 27 09:53:33 pay-test Keepalived\[123755\]: Opening file '/etc/keepalived/keepalived.conf'. Jun 27 09:53:33 pay-test systemd: PID file /var/run/keepalived.pid not readable (yet?) after start. Jun 27 09:53:33 pay-test Keepalived\[123756\]: Starting Healthcheck child process, pid=123757 Jun 27 09:53:33 pay-test Keepalived_healthcheckers\[123757\]: Opening file '/etc/keepalived/keepalived.conf'. Jun 27 09:53:33 pay-test Keepalived\[123756\]: Starting VRRP child process, pid=123758 Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Registering Kernel netlink reflector Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Registering Kernel netlink command channel Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Registering gratuitous ARP shared channel Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Opening file '/etc/keepalived/keepalived.conf'. Jun 27 09:53:33 pay-test Keepalived_vrrp\[123758\]: Using LinkWatch kernel netlink reflector… Jun 27 09:53:33 pay-test systemd: Started LVS and VRRP High Availability Monitor.
没有定时执行脚本: keepalived.conf里少了一个}
参考: http://wosyingjun.iteye.com/blog/2313147
keepalived: command not found 错误参考: https://blog.csdn.net/lxn19860201/article/details/51422489
日志参考: https://www.cnblogs.com/zzzhfo/p/6070575.html