我们先来谈谈keepalived的工作原理:
keepalived的工作原理基于三个层面,分别时IP层,TCP层和应用层
IP
层:keepalived会定期向服务器集群中的服务器发送一个类似于ping程序的ICMP数据包。通俗理解就是我keepalived来ping你服
务器,我要是ping通了,那我知道你还活着,咱们可以继续合作,如果不幸发现你挂了,那就得把这台服务器踢了。该层就是确保服务器ip地址有效,一次来
判断这台服务器可否继续在岗谋职。
TCP层:这一层和IP层类似,只不过IP层检查的是IP,该层检查TCP端口。keepalived在IP层
且报你服务器的IP有效,然后继续看你的TCP端口,比如说你是web
server,那你的80端口就应该开启,如果keepalived发现你的80端口没有启动。那么它就会认为你已经挂掉,然后剔除。
应用层:这一层就是具体的工作程序了,keepalived是个恪尽职守的检察官,应用层的程序与用户的设定息息相关,keepalived则通过检查服务器的运行是否和用户设定一致,如果不一致,keepalived就会宣判这台服务器牺牲。
网站建设哪家好,找
创新互联公司!专注于网页设计、网站建设、微信开发、
微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了
利通免费建站欢迎大家使用!
keepalived + nginx
主机:172.25.44.3 server3
备机:172.25.44.4 server4
配置虚拟ip
[root@server3 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0DEVICE=eth0:0TYPE=EthernetONBOOT=yesBOOTPROTO=staticDNS1=172.25.44.44IPADDR=172.25.44.100NETMASK=255.255.255.0GETWAY=172.25.44.44[root@server3 ~]# /etc/init.d/network restart[root@server3 ~]# ifconfigeth0 Link encap:Ethernet HWaddr 52:54:00:48:60:3E inet addr:172.25.44.4 Bcast:172.25.44.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fe48:603e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11926 errors:0 dropped:0 overruns:0 frame:0 TX packets:6533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:25662550 (24.4 MiB) TX bytes:876645 (856.0 KiB)
eth0:0 Link encap:Ethernet HWaddr 52:54:00:48:60:3E inet addr:172.25.44.100 Bcast:172.25.44.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:36283 errors:0 dropped:0 overruns:0 frame:0 TX packets:36283 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1982812 (1.8 MiB) TX bytes:1982812 (1.8 MiB)
主备机均需配置安装nginx
安装包:nginx-1.10.3.tar.gz
解压安装包:tar zxf nginx-1.10.3.tar.gz
[root@server3 software]# cd nginx-1.10.3 —prefix=/usr/local/lnmp/nignx
[root@server3 nginx-1.10.3]# ./configure
[root@server3 nginx-1.10.3]# make
[root@server3 nginx-1.10.3]# make install
[root@server3 nginx-1.10.3]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
#创建nginx的软链接
配置nginx
[root@server3 nginx-1.10.3]# cd /usr/local/lnmp/nginx/conf[root@server3 conf]# vim nginx.conf/#访问8088端口是的发布目录为/var/www/html,而nginx的默认发布目录还是/usr/local/lnmp/nginx/html
server { listen 8088;#监听8088端口 server_name localhost;
location / {
root /var/www/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/lnmp/nginx/html;
}
}
[root@server3 conf]# cd /var/www/html[root@server3 html]# vim index.html
Welcome to nginx!
Welcome to nginx! 172.25.44.3
[root@server3 html]# nginx
安装keepalived
安装包:keepalived-1.2.2.tar.gz
[root@server3 software]# wget
http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
[root@server3 software]# tar zxf keepalived-1.2.2.tar.gz
[root@server3 software]# cd keepalived-1.2.2
[root@server3 keepalived-1.2.2]# ./configure
[root@server3 keepalived-1.2.2]# make
[root@server3 keepalived-1.2.2]# make install
[root@server3 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@server3 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@server3 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@server3 keepalived-1.2.2]# cp -r /usr/local/etc/keepalived/ /etc/
配置leepalived
[root@server3 keepalived-1.2.2]# vim /var/www/html/index.html
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script “
#state MASTER:MASTER主机,BACKUP备机#priority 100:优先级指定,数字越大优先级越高#virtual_ipaddress:指定vip#track_scrip:检查端口[root@server3 keepalived-1.2.2]# /etc/init.d/keepalived start
备机次同步主机的操作步骤:keepalived的配置文件中,主机的state为MASTER,备机的state为BACKUP,备机的priority要小于主机
测试
验证nginx启动正常
访问主机nginx的默认页访问主机nginx的指定页访问备机nginx的默认页访问备机nginx的指定页访问vip
主机server3的优先级更高,在主机完好的情况下,有server3来负载nginx运行[root@server3 software]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
当主机的keeplived服务挂掉的时候,在则访问vip,则由备机自动负载nginx运行[root@server4 conf]# ip a
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:48:60:3e brd ff:ff:ff:ff:ff:ff
inet 172.25.44.4/24 brd 172.25.44.255 scope global eth0
inet 172.25.254.104/24 brd 172.25.254.255 scope global eth0
inet 172.25.44.100/32 scope global eth0
inet 172.25.44.100/24 brd 172.25.44.255 scope global secondary eth0:0
inet6 fe80::5054:ff:fe48:603e/64 scope link
valid_lft forever preferred_lft forever
同时可以看到vip浮动在备机server4上[root@server3 software]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
启动主机server3上的keepalived服务,再次访问vip,则nginx的负载运行又回到主机server3上[root@server3 software]# ip a
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:b4:14:0d brd ff:ff:ff:ff:ff:ff
inet 172.25.44.3/24 brd 172.25.44.255 scope global eth0
inet 172.25.254.103/24 brd 172.25.254.255 scope global eth0
inet 172.25.44.100/32 scope global eth0
inet6 fe80::5054:ff:feb4:140d/64 scope link
valid_lft forever preferred_lft forever
该情况下查看ip,发现vip浮动在主机server3上在应用层出问题的情况下,如停掉主机server3的nginx服务,则备机server4也会自动接管nginx支持前端的访问[root@server3 software]# killall nginx
问题补充:在备机keepalived的配合配置文件中,如果补修改state的情况下实验也是可以成功的,我觉得应该时两个机子通过priority优先级竞争。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前文章:keepalived+nginx-创新互联
本文来源:
http://shouzuofang.com/article/depsoe.html