第十三周-day53-Keepalived高可用

一、keepalived基于服务器,nginx挂了不会自动切换如何解决

修改内核参数
在Linux如果需要绑定本机不存在的IP,例如在HAproxy及Nginx可能会用到,需要开启Kernel的参数net.ipv4.ip_nonlocal_bind

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
count=`netstat -lntup|grep 80|wc -l`

if [ $count -eq 0 ];then
        systemctl stop keepalived.service
        echo 'keepalived主已关闭'
        else
        echo 'nginx任然存活进程'
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一键部署出来!


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269