我们先来谈谈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
配置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.htmlWelcome to nginx! 172.25.44.3
安装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 “备机次同步主机的操作步骤:keepalived的配置文件中,主机的state为MASTER,备机的state为BACKUP,备机的priority要小于主机
测试
验证nginx启动正常
访问vip
问题补充:在备机keepalived的配合配置文件中,如果补修改state的情况下实验也是可以成功的,我觉得应该时两个机子通过priority优先级竞争。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:keepalived+nginx-创新互联
文章出自:http://lswzjz.com/article/depsoe.html