一、keepalived基于服務(wù)器,nginx掛了不會(huì)自動(dòng)切換如何解決
修改內(nèi)核參數(shù)
在Linux如果需要綁定本機(jī)不存在的IP,例如在HAproxy及Nginx可能會(huì)用到,需要開(kāi)啟Kernel的參數(shù)net.ipv4.ip_nonlocal_bind
echo "net.ipv4.ip_nonlocal_bind = 1" >>/etc/sysctl.conf

1.檢查狀態(tài)
ps -ef |grep keepalived
關(guān)閉不了nginx服務(wù)的方法
1.restart
2. pkill nginx
3. restart
2.寫(xiě)腳本
腳本名字不要寫(xiě)服務(wù)的名字,如nginx.sh
檢查nginx狀態(tài)
nginx關(guān)閉,keepalived也關(guān)閉
[root@lb01 nginx]# vim /server/scripts/jiancha.sh
#!/bin/bash
count=`netstat -lntup|grep 80|wc -l`
if [ $count -eq 0 ];then
systemctl stop keepalived.service
echo 'keepalived主已關(guān)閉'
else
echo 'nginx任然存活進(jìn)程'
fi
3.一定要給腳本添加執(zhí)行權(quán)限
[root@lb01 nginx]# sh /server/scripts/jiancha.sh
4.添加函數(shù)
vrrp_script jiancha { #腳本名稱(chēng)
script "/server/scripts/jiancha.sh" #定義檢查的腳本
interval 2 #每隔2秒執(zhí)行
weight 1 #權(quán)重分配數(shù)量
track_script { #執(zhí)行腳本
jiancha #腳本名稱(chēng)
}
5. 完整書(shū)寫(xiě)
[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.測(cè)試一下
[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看一下是否跳過(guò)去了
[root@lb02 ~]# ip a|grep 0.3
inet 10.0.0.3/24 scope global secondary eth0:1
8.回到lb01把nginx和keepalived開(kāi)啟
[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
#又轉(zhuǎn)到主了
二、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
三、每個(gè)域名綁定對(duì)應(yīng)ip
1.基于ip的虛擬主機(jī)
添加虛擬主機(jī)的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; ##添加虛擬主機(jī)的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; #添加虛擬主機(jī)的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.重啟檢查語(yǔ)法nginx報(bào)錯(cuò)問(wèn)題

修改內(nèi)核參數(shù):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.內(nèi)核參數(shù)修改了哪些內(nèi)容
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
四、高可用的裂腦(腦裂)問(wèn)題
1.while死循環(huán)語(yǔ)法
[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

到這里基礎(chǔ)的中小規(guī)模架構(gòu)就結(jié)束了,在未來(lái)的9天里的任務(wù),要將基礎(chǔ)的架構(gòu)利用ansible一鍵部署出來(lái)!





