###
记录一下配置时遇到的问题
安装nginx
这个之前操作过了,没有截图就只写步骤吧。
$ cd /usr/local/ $ wget http://nginx.org/download/nginx-1.8.0.tar.gz
$ tar -zxvf nginx-1.8.0.tar.gz $ cd nginx-1.8.0
$ ./configure --prefix=/usr/local/nginx
$ make
$ make install
nginx常用命令
/usr/local/nginx/sbin/nginx 启动命令
重启:
$ /usr/local/nginx/sbin/nginx -s reload
停止:
$ /usr/local/nginx/sbin/nginx -s stop
测试配置文件是否正常:
$ /usr/local/nginx/sbin/nginx -t
强制关闭:
$ pkill nginx
安装Keepalived(版本2.0.18)
解压
tar -zxvf keepalived-2.0.18.tar.gz -C /usr/local/
安装openssl-devel
yum install -y openssl openssl-devel
编译安装
./configure --prefix=/usr/local/keepalived
make
make
make install
make install
keepalived配置文件
! 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_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务
interval 2 #检测时间间隔
weight 10 #如果条件成立的话,则权重 -20
}
# 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state BACKUP #来决定主从
interface ens33 # 绑定虚拟 IP 的网络接口,根据自己的机器填写
virtual_router_id 222 # 虚拟路由的 ID 号, 两个节点设置必须一样
mcast_src_ip 192.168.206.23 #填写clear本机ip
priority 90 # 节点优先级,主要比从节点优先级高
nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s
authentication {
auth_type PASS
auth_pass 1111
}
# 将 track_script 块加入 instance 配置块
track_script {
chk_nginx #执行 Nginx 监控的服务
}
virtual_ipaddress {
192.168.206.110 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
注意下图的注释部分。我的版本必须注释掉 不然报错
##
配置keppalived服务(开机自启动)
创建文件夹,将配置文件复制到文件夹中
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
没有找到/rc.d/init.d文件夹,要在源码文件中找
然后复制keepalived脚本文件:(注意init文件位置)
cp /usr/local/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
然后使用下面命令设置开机自启动
systemctl常用配置
注意 关闭的时候使用kill的那个命令
1;systemctl daemon-reload 重新加载
2:systemctl enable keepalived.service 设置开机自动启动
3:systemctl disable keepalived.service 取消开机自动启动
4:systemctl start keepalived.service 启动
5:systemctl stop keepalived.service停止
systemctl status keepalived.service查看服务状态
systemctl kill keepalived 关闭
发现stop命令不好使的话,像下面一样注释掉killmode选项
/etc/keepalive下编写监控nignx脚本
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
# 判断nginx是否宕机,如果宕机了,尝试重启
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
# 等待一小会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
如果发现并不能启动服务——->查找错误日志
在/var/log/messages下查看错误日志,这里发现是网卡问题
1.编写本地配置文件
2.修改本地配置文件的网卡,改为自己网卡,这里我的网卡是eth33
3.etc下的配置文件的网卡也要改动。如下图所示
最终效果
启动成功
日志配置
我们只需要修改 /etc/sysconfig/keepalived 就可以了
把KEEPALIVED_OPTIONS="-D" 修改为KEEPALIVED_OPTIONS="-D -d -S 0"
最后设置syslog,修改/etc/rsyslog.conf
最后添加:
# keepalived -S 0
local0.* /var/log/keepalived.log
重新启动日志
/etc/init.d/rsyslog restart
使用以下命令进行验证
/etc/init.d/keepalived restart;tailf /var/log/keepalived.log