一、keepalived基于服务器,nginx挂了不会自动切换如何解决
修改内核参数
echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf
1.检查状态
ps -ef |grep keepalived
关闭不了nginx服务的方法
1.restart
2\. pkill nginx
3\. restart
2.写脚本
脚本名字不要写服务的名字,如nginx.sh
检查nginx状态
nginx关闭,keepalived也关闭
[root@lb01 nginx]# vim /server/scripts/jiancha.sh
#!/bin/bash
. /etc/profile
count=` ps -ef|grep nginx |grep -v grep |wc -l `
if [ $count -eq 0 ];then
systemctl stop keepalived
fi
3.一定要给脚本添加执行权限
[root@lb01 nginx]# sh /server/scripts/jiancha.sh
4.添加函数
vrrp_script jiancha { #脚本名称
script "/server/scripts/jiancha.sh" #定义检查的脚本
interval 2 #每隔2秒执行
weight 1 #权重分配数量
track_script { #执行脚本
jiancha #脚本名称
}
5. 完整书写
[root@lb01 nginx]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script jiancha {
script "/server/scripts/jiancha.sh"
interval 2
weight 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
jiancha
}
}
6.测试一下
[root@lb01 nginx]#systemctl restart keepalived.servic
[root@lb01 nginx]# systemctl is-active nginx
active
[root@lb01 nginx]# ip a|grep 0.3
inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 nginx]# systemctl stop nginx
[root@lb01 nginx]# ip a|grep 0.3
[root@lb01 nginx]# \\虚拟ip跳走了
[root@lb01 nginx]#
7.去lb02看一下是否跳过去了
[root@lb02 ~]# ip a|grep 0.3
inet 10.0.0.3/24 scope global secondary eth0:1
8.回到lb01把nginx和keepalived开启
[root@lb01 nginx]# systemctl start nginx
[root@lb01 nginx]# ip a|grep 0.3
[root@lb01 nginx]# systemctl start keepalived.service
[root@lb01 nginx]# ip a|grep 0.3 #间隔2秒
[root@lb01 nginx]#
[root@lb01 nginx]# ip a|grep 0.3
inet 10.0.0.3/24 scope global secondary eth0:1
#又转到主了
二、keepalived双主模式
修改配置文件后重启keepalived
systemctl restart keepalived
lb01的keepalived双主配置文件
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script jiancha {
script "/server/scripts/jiancha.sh"
interval 2
weight 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
jiancha
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
}
lb02的keepalived双主配置文件
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
}
让lb01和lb02的nginx配置文件相同
vim /etc/nginx/nginx.conf :
upstream web_pools {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
curl一下
保证俩边的/etc/nginx/nginx.conf配置文件一样
[root@lb01 nginx]# curl 10.0.0.3
web01 www.oldboy.com
[root@lb01 nginx]# curl 10.0.0.3
web02 www.oldboy.com
[root@lb01 nginx]# curl 10.0.0.3
web01 www.oldboy.com
[root@lb01 nginx]#
[root@lb01 nginx]# curl 10.0.0.4
web01 www.oldboy.com
[root@lb01 nginx]# curl 10.0.0.4
web02 www.oldboy.com
[root@lb01 nginx]# curl 10.0.0.4
web01 www.oldboy.com
三、每个域名绑定对应ip
1.基于ip的虚拟主机
添加虚拟主机的ip就可以了
listen 10.0.0.3:80;
listen 10.0.0.4:80;lb01和lb02的修改相同
upstream web_pools {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 10.0.0.3:80; ##添加虚拟主机的ip
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.4:80; #添加虚拟主机的ip
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
------------------------------------------------------------
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl restart nginx
2.重启检查语法nginx报错问题
修改内核参数:net.ipv4.ip_nonlocal_bind = 1
sysctl -p #生效
[root@lb01 nginx]# tail -1 /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@lb01 nginx]# sysctl -p #生效
net.ipv4.ip_nonlocal_bind = 1
[root@lb02 ~]# tail -1 /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@lb02 ~]# sysctl -p #生效
net.ipv4.ip_nonlocal_bind = 1
再重启就可以了
[root@lb01 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 nginx]# systemctl restart nginx
3.内核参数修改了哪些内容
net.ipv4.ip_nonlocal_bind = 1
[root@lb01 nginx]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@lb01 nginx]# #cat /proc/sys/net/ipv4/ip_nonlocal_bind
[root@lb01 nginx]# #net.ipv4.ip_nonlocal_bind = 1
[root@lb01 nginx]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1
四、高可用的裂脑(脑裂)问题
1.while死循环语法
[root@lb02 ~]# cat /server/scripts/chk_vip.sh
#!/bin/bash
while true
do
date
sleep 2;
done
[root@lb02 ~]# sh /server/scripts/chk_vip.sh
Mon Jun 17 12:01:19 CST 2019
Mon Jun 17 12:01:21 CST 2019
Mon Jun 17 12:01:23 CST 2019
Mon Jun 17 12:01:25 CST 2019
Mon Jun 17 12:01:27 CST 2019
Mon Jun 17 12:01:29 CST 2019
Mon Jun 17 12:01:31 CST 2019
到这里基础的中小规模架构就结束了,在未来的9天里的任务,要将基础的架构利用ansible一键部署出来!