首页 文章 精选 留言 我的

精选列表

搜索[Java应用],共10000篇文章
优秀的个人博客,低调大师

LVS+keepalived高性能集群应用实战

[root@example~]#yuminstallipvsadm-y安装ip_vs管理工具 理论部分我们在上篇文章中详细介绍了,这次进入实操阶段 [root@example~]#modprobeip_vs加载ip_vs模块 [root@example~]#modprobeip_vs_rr [root@example~]#lsmod|grepip_vs ip_vs_rr14200 ip_vs1265342ip_vs_rr libcrc32c12461ip_vs ipv6335589271ip_vs [root@example~]#ipaddradd192.168.189.200/24deveth0labeleth0:1手动加vip地址 [root@example~]#ipaddr 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 inet127.0.0.1/8scopehostlo inet6::1/128scopehost valid_lftforeverpreferred_lftforever 2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000 link/ether00:0c:29:5a:e1:a3brdff:ff:ff:ff:ff:ff inet192.168.189.130/24brd192.168.189.255scopeglobaleth0 inet192.168.189.200/24scopeglobalsecondaryeth0:1 inet6fe80::20c:29ff:fe5a:e1a3/64scopelink valid_lftforeverpreferred_lftforever 本次环境规划如下 操作系统 centos6.7 [root@example~]#curl192.168.189.132 web01_192.168.189.132 [root@example~]#curl192.168.189.133 web02_192.168.189.133 手工执行配置lvs服务器并添加两台rs服务 查看相关的帮助命令 [root@example~]#ipvsadm--help ipvsadmv1.262008/5/15(compiledwithpoptandIPVSv1.2.1) Usage: ipvsadm-A|E-t|u|fservice-address[-sscheduler][-p[timeout]][-Mnetmask][--pepersistence_engine] ipvsadm-D-t|u|fservice-address ipvsadm-C ipvsadm-R ipvsadm-S[-n] ipvsadm-a|e-t|u|fservice-address-rserver-address[options] ipvsadm-d-t|u|fservice-address-rserver-address ipvsadm-L|l[options] ipvsadm-Z[-t|u|fservice-address] ipvsadm--settcptcpfinudp ipvsadm--start-daemonstate[--mcast-interfaceinterface][--syncidsid] ipvsadm--stop-daemonstate ipvsadm-h Commands: Eitherlongorshortoptionsareallowed. --add-service-Aaddvirtualservicewithoptions --edit-service-Eeditvirtualservicewithoptions --delete-service-Ddeletevirtualservice --clear-Cclearthewholetable --restore-Rrestorerulesfromstdin --save-Ssaverulestostdout --add-server-aaddrealserverwithoptions --edit-server-eeditrealserverwithoptions --delete-server-ddeleterealserver --list-L|-llistthetable --zero-Zzerocountersinaserviceorallservices --settcptcpfinudpsetconnectiontimeoutvalues --start-daemonstartconnectionsyncdaemon --stop-daemonstopconnectionsyncdaemon --help-hdisplaythishelpmessage [root@example~]#ipvsadm-C清空全部配置 [root@example~]#ipvsadm-A-t192.168.189.200:80-swrr指定vip并设定调度算法 [root@example~]#ipvsadm-a-t192.168.189.200:80-r192.168.189.132:80-g-w1添加rs [root@example~]#ipvsadm-a-t192.168.189.200:80-r192.168.189.133:80-g-w3 [root@example~]#ipvsadm-Ln IPVirtualServerversion1.2.1(size=4096) ProtLocalAddress:PortSchedulerFlags ->RemoteAddress:PortForwardWeightActiveConnInActConn TCP192.168.189.200:80wrr ->192.168.189.132:80Route100 ->192.168.189.133:80Route300 手工在rs服务器上面绑定操作 第一台 [root@example~]#ifconfiglo:132192.168.189.200netmask255.255.255.255up [root@example~]#routeadd-host192.168.189.200devlo [root@example~]#ipaddr 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 inet127.0.0.1/8scopehostlo inet192.168.189.200/32brd192.168.189.200scopegloballo:132 inet6::1/128scopehost valid_lftforeverpreferred_lftforever 2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000 link/ether00:0c:29:66:66:33brdff:ff:ff:ff:ff:ff inet192.168.189.132/24brd192.168.189.255scopeglobaleth0 inet6fe80::20c:29ff:fe66:6633/64scopelink valid_lftforeverpreferred_lftforever 第二台 [root@example~]#ipaddradd192.168.189.200/32devlolabello:133 [root@example~]#routeadd-host192.168.189.200devlo [root@example~]#ipaddr 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 inet127.0.0.1/8scopehostlo inet192.168.189.200/32scopegloballo:133 inet6::1/128scopehost valid_lftforeverpreferred_lftforever 2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000 link/ether00:0c:29:6c:a9:25brdff:ff:ff:ff:ff:ff inet192.168.189.133/24brd192.168.189.255scopeglobaleth0 inet6fe80::20c:29ff:fe6c:a925/64scopelink valid_lftforeverpreferred_lftforever 手工在rs端抑制arp响应 第一台 [root@example~]#echo"1">/proc/sys/net/ipv4/conf/lo/arp_ignore [root@example~]#echo"2">/proc/sys/net/ipv4/conf/lo/arp_announce [root@example~]#echo"1">/proc/sys/net/ipv4/conf/all/arp_ignore [root@example~]#echo"2">/proc/sys/net/ipv4/conf/all/arp_announce 第二台 [root@example~]#echo"1">/proc/sys/net/ipv4/conf/lo/arp_ignore [root@example~]#echo"2">/proc/sys/net/ipv4/conf/lo/arp_announce [root@example~]#echo"1">/proc/sys/net/ipv4/conf/all/arp_ignore [root@example~]#echo"2">/proc/sys/net/ipv4/conf/all/arp_announce 测试访问,不停的刷新,同时打开web服务器的log观察 web01_132日志 [root@example~]#tail-f/var/log/httpd/access_log 192.168.189.1--[13/Aug/2016:14:34:21+0800]"GET/HTTP/1.1"20022"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:24+0800]"GET/HTTP/1.1"20022"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:25+0800]"GET/HTTP/1.1"20022"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" web01_133日志 [root@example~]#tail-f/var/log/httpd/access_log 192.168.189.1--[13/Aug/2016:14:33:54+0800]"GET/HTTP/1.1"20022"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:33:54+0800]"GET/favicon.icoHTTP/1.1"404290"http://192.168.189.200/""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:05+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:17+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:20+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:22+0800]"GET/HTTP/1.1"20022"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:23+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:23+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:24+0800]"GET/HTTP/1.1"20022"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:25+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:25+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:26+0800]"GET/HTTP/1.1"20022"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" 192.168.189.1--[13/Aug/2016:14:34:26+0800]"GET/HTTP/1.1"304-"-""Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/51.0.2704.106Safari/537.36" [root@example~]#ipvsadm-Ln--stats可以看到基本符合我们设定的权重1:3 IPVirtualServerversion1.2.1(size=4096) ProtLocalAddress:PortConnsInPktsOutPktsInBytesOutBytes ->RemoteAddress:Port TCP192.168.189.200:80663350444740 ->192.168.189.132:8016820109780 ->192.168.189.133:80502530334960 上面日志可能不够直观,最好再开一个客户端去执行for循环测试,这里就不在演示 以上手工创建只是为了让我更好的理解lvs的过程,好了下面开始我们的keepalived部分 安装keepalived [root@example~]#yuminstallkeepalived-y 配置keepalived 主节点上面 [root@example~]#cat/etc/keepalived/keepalived.conf global_defs{ notification_email{ 1335120568@qq.com } notification_email_fromAlexandre.Cassen@firewall.loc smtp_server192.168.200.1 smtp_connect_timeout30 router_idLVS_01 } vrrp_instanceweb{ stateMASTER interfaceeth0 virtual_router_id51 priority150 advert_int1 authentication{ auth_typePASS auth_pass1111 } virtual_ipaddress{ 192.168.189.200/24 } } virtual_server192.168.189.20080{ delay_loop6 lb_algowrr lb_kindDR nat_mask255.255.255.0 persistence_timeout50 protocolTCP real_server192.168.189.13280{ weight1 TCP_CHECK{ connect_timeout8 nb_get_retry3 delay_before_retry3 connect_port80 } } real_server192.168.189.13380{ weight1 TCP_CHECK{ connect_timeout8 nb_get_retry3 delay_before_retry3 connect_port80 } } } 备用节点上面keepalived配置 [root@example~]#cat/etc/keepalived/keepalived.conf global_defs{ notification_email{ 1335120568@qq.com } notification_email_fromAlexandre.Cassen@firewall.loc smtp_server192.168.200.1 smtp_connect_timeout30 router_idLVS_02 } vrrp_instanceweb{ stateBACKUP interfaceeth0 virtual_router_id51 priority100 advert_int1 authentication{ auth_typePASS auth_pass1111 } virtual_ipaddress{ 192.168.189.200/24 } } virtual_server192.168.189.20080{ delay_loop6 lb_algowrr lb_kindDR nat_mask255.255.255.0 persistence_timeout50 protocolTCP real_server192.168.189.13280{ weight1 TCP_CHECK{ connect_timeout8 nb_get_retry3 delay_before_retry3 connect_port80 } } real_server192.168.189.13380{ weight1 TCP_CHECK{ connect_timeout8 nb_get_retry3 delay_before_retry3 connect_port80 } } } 启动主备keepalived的观察log [root@example~]#/etc/init.d/keepalivedstart Startingkeepalived:[OK] 主节点上面 [root@examplekeepalived]#tail-f/var/log/messages Aug1315:11:10exampleKeepalived[45366]:StartingKeepalivedv1.2.13(03/19,2015) Aug1315:11:10exampleKeepalived[45367]:StartingHealthcheckchildprocess,pid=45369 Aug1315:11:10exampleKeepalived[45367]:StartingVRRPchildprocess,pid=45370 Aug1315:11:10exampleKeepalived_healthcheckers[45369]:NetlinkreflectorreportsIP192.168.189.130added Aug1315:11:10exampleKeepalived_vrrp[45370]:NetlinkreflectorreportsIP192.168.189.130added Aug1315:11:10exampleKeepalived_vrrp[45370]:NetlinkreflectorreportsIP192.168.189.200added Aug1315:11:10exampleKeepalived_vrrp[45370]:NetlinkreflectorreportsIPfe80::20c:29ff:fe5a:e1a3added Aug1315:11:10exampleKeepalived_vrrp[45370]:RegisteringKernelnetlinkreflector Aug1315:11:10exampleKeepalived_vrrp[45370]:RegisteringKernelnetlinkcommandchannel Aug1315:11:10exampleKeepalived_vrrp[45370]:RegisteringgratuitousARPsharedchannel Aug1315:11:10exampleKeepalived_healthcheckers[45369]:NetlinkreflectorreportsIP192.168.189.200added Aug1315:11:10exampleKeepalived_healthcheckers[45369]:NetlinkreflectorreportsIPfe80::20c:29ff:fe5a:e1a3added Aug1315:11:10exampleKeepalived_healthcheckers[45369]:RegisteringKernelnetlinkreflector Aug1315:11:10exampleKeepalived_healthcheckers[45369]:RegisteringKernelnetlinkcommandchannel Aug1315:11:10exampleKeepalived_vrrp[45370]:Openingfile'/etc/keepalived/keepalived.conf'. Aug1315:11:10exampleKeepalived_vrrp[45370]:Configurationisusing:63278Bytes Aug1315:11:10exampleKeepalived_vrrp[45370]:UsingLinkWatchkernelnetlinkreflector... Aug1315:11:10exampleKeepalived_healthcheckers[45369]:Openingfile'/etc/keepalived/keepalived.conf'. Aug1315:11:10exampleKeepalived_healthcheckers[45369]:Configurationisusing:14742Bytes Aug1315:11:10exampleKeepalived_vrrp[45370]:VRRPsockpool:[ifindex(2),proto(112),unicast(0),fd(10,11)] Aug1315:11:10exampleKeepalived_healthcheckers[45369]:UsingLinkWatchkernelnetlinkreflector... Aug1315:11:10exampleKeepalived_healthcheckers[45369]:Activatinghealthcheckerforservice[192.168.189.132]:80 Aug1315:11:10exampleKeepalived_healthcheckers[45369]:Activatinghealthcheckerforservice[192.168.189.133]:80 Aug1315:11:11exampleKeepalived_vrrp[45370]:VRRP_Instance(web)TransitiontoMASTERSTATE Aug1315:11:11exampleKeepalived_vrrp[45370]:VRRP_Instance(web)Receivedlowerprioadvert,forcingnewelection Aug1315:11:12exampleKeepalived_vrrp[45370]:VRRP_Instance(web)EnteringMASTERSTATE Aug1315:11:12exampleKeepalived_vrrp[45370]:VRRP_Instance(web)settingprotocolVIPs. Aug1315:11:12exampleKeepalived_vrrp[45370]:VRRP_Instance(web)SendinggratuitousARPsoneth0for192.168.189.200 Aug1315:11:17exampleKeepalived_vrrp[45370]:VRRP_Instance(web)SendinggratuitousARPsoneth0for192.168.189.200 Aug1315:13:36exampledhclient[1239]:DHCPREQUESToneth0to192.168.189.254port67(xid=0x3b1957c6) Aug1315:13:36exampledhclient[1239]:DHCPACKfrom192.168.189.254(xid=0x3b1957c6) Aug1315:13:38exampledhclient[1239]:boundto192.168.189.130--renewalin678seconds. 备用节点上面 [root@examplekeepalived]#tail-f/var/log/messages Aug1315:11:03exampleKeepalived[1937]:StartingKeepalivedv1.2.13(03/19,2015) Aug1315:11:03exampleKeepalived[1938]:StartingHealthcheckchildprocess,pid=1940 Aug1315:11:03exampleKeepalived[1938]:StartingVRRPchildprocess,pid=1941 Aug1315:11:03exampleKeepalived_vrrp[1941]:NetlinkreflectorreportsIP192.168.189.131added Aug1315:11:03exampleKeepalived_healthcheckers[1940]:NetlinkreflectorreportsIP192.168.189.131added Aug1315:11:03exampleKeepalived_healthcheckers[1940]:NetlinkreflectorreportsIPfe80::20c:29ff:feea:4c15added Aug1315:11:03exampleKeepalived_healthcheckers[1940]:RegisteringKernelnetlinkreflector Aug1315:11:03exampleKeepalived_healthcheckers[1940]:RegisteringKernelnetlinkcommandchannel Aug1315:11:03exampleKeepalived_vrrp[1941]:NetlinkreflectorreportsIPfe80::20c:29ff:feea:4c15added Aug1315:11:03exampleKeepalived_vrrp[1941]:RegisteringKernelnetlinkreflector Aug1315:11:03exampleKeepalived_vrrp[1941]:RegisteringKernelnetlinkcommandchannel Aug1315:11:03exampleKeepalived_vrrp[1941]:RegisteringgratuitousARPsharedchannel Aug1315:11:03exampleKeepalived_vrrp[1941]:Openingfile'/etc/keepalived/keepalived.conf'. Aug1315:11:03exampleKeepalived_vrrp[1941]:Configurationisusing:63278Bytes Aug1315:11:03exampleKeepalived_vrrp[1941]:UsingLinkWatchkernelnetlinkreflector... Aug1315:11:03exampleKeepalived_vrrp[1941]:VRRP_Instance(web)EnteringBACKUPSTATE Aug1315:11:03exampleKeepalived_vrrp[1941]:VRRPsockpool:[ifindex(2),proto(112),unicast(0),fd(10,11)] Aug1315:11:03exampleKeepalived_healthcheckers[1940]:Openingfile'/etc/keepalived/keepalived.conf'. Aug1315:11:03exampleKeepalived_healthcheckers[1940]:Configurationisusing:14742Bytes Aug1315:11:04exampleKeepalived_healthcheckers[1940]:UsingLinkWatchkernelnetlinkreflector... Aug1315:11:04exampleKeepalived_healthcheckers[1940]:Activatinghealthcheckerforservice[192.168.189.132]:80 Aug1315:11:04exampleKeepalived_healthcheckers[1940]:Activatinghealthcheckerforservice[192.168.189.133]:80 Aug1315:11:04examplekernel:IPVS:[wrr]schedulerregistered. Aug1315:11:07exampleKeepalived_vrrp[1941]:VRRP_Instance(web)TransitiontoMASTERSTATE Aug1315:11:08exampleKeepalived_vrrp[1941]:VRRP_Instance(web)EnteringMASTERSTATE Aug1315:11:08exampleKeepalived_vrrp[1941]:VRRP_Instance(web)settingprotocolVIPs. Aug1315:11:08exampleKeepalived_vrrp[1941]:VRRP_Instance(web)SendinggratuitousARPsoneth0for192.168.189.200 Aug1315:11:08exampleKeepalived_healthcheckers[1940]:NetlinkreflectorreportsIP192.168.189.200added Aug1315:11:10examplentpd[1525]:Listennormallyon6eth0192.168.189.200UDP123 Aug1315:11:11exampleKeepalived_vrrp[1941]:VRRP_Instance(web)Receivedhigherprioadvert Aug1315:11:11exampleKeepalived_vrrp[1941]:VRRP_Instance(web)EnteringBACKUPSTATE Aug1315:11:11exampleKeepalived_vrrp[1941]:VRRP_Instance(web)removingprotocolVIPs. Aug1315:11:11exampleKeepalived_healthcheckers[1940]:NetlinkreflectorreportsIP192.168.189.200removed Aug1315:11:13examplentpd[1525]:Deletinginterface#6eth0,192.168.189.200#123,interfacestats:received=0,sent=0,dropped=0,active_time=3secs 主节点vip地址情况 [root@example~]#ipaddr 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 inet127.0.0.1/8scopehostlo inet6::1/128scopehost valid_lftforeverpreferred_lftforever 2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000 link/ether00:0c:29:5a:e1:a3brdff:ff:ff:ff:ff:ff inet192.168.189.130/24brd192.168.189.255scopeglobaleth0 inet192.168.189.200/24scopeglobalsecondaryeth0:1 inet6fe80::20c:29ff:fe5a:e1a3/64scopelink valid_lftforeverpreferred_lftforever 备用节点vip地址情况 [root@example~]#ipaddr 1:lo:<LOOPBACK,UP,LOWER_UP>mtu65536qdiscnoqueuestateUNKNOWN link/loopback00:00:00:00:00:00brd00:00:00:00:00:00 inet127.0.0.1/8scopehostlo inet6::1/128scopehost valid_lftforeverpreferred_lftforever 2:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPqlen1000 link/ether00:0c:29:ea:4c:15brdff:ff:ff:ff:ff:ff inet192.168.189.131/24brd192.168.189.255scopeglobaleth0 inet6fe80::20c:29ff:feea:4c15/64scopelink valid_lftforeverpreferred_lftforever rs服务器配置(两台都需要做) [root@example ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore [root@example ~]# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce [root@example ~]# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore [root@example ~]# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce [root@example ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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 inet 192.168.189.200/32 scope global lo:1 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:66:66:33 brd ff:ff:ff:ff:ff:ff inet 192.168.189.132/24 brd 192.168.189.255 scope global eth0 inet6 fe80::20c:29ff:fe66:6633/64 scope link valid_lft forever preferred_lft forever 高可用×××测试 高可用性是通过LVS的两个DirectorServer完成的,为了模拟故障,先将主DirectorServer 上面的Keepalived服务停止,然后观察备用DirectorServer上Keepalived的运行日志,信息如下: [root@example~]#/etc/init.d/keepalivedstop主节点 Stoppingkeepalived:[OK] 主节点日志 Aug1315:18:53exampleKeepalived[45367]:StoppingKeepalivedv1.2.13(03/19,2015) Aug1315:18:53exampleKeepalived_vrrp[45370]:VRRP_Instance(web)sending0priority Aug1315:18:53exampleKeepalived_vrrp[45370]:VRRP_Instance(web)removingprotocolVIPs. Aug1315:18:53exampleKeepalived_healthcheckers[45369]:NetlinkreflectorreportsIP192.168.189.200removed Aug1315:18:53exampleKeepalived_healthcheckers[45369]:Removingservice[192.168.189.132]:80fromVS[192.168.189.200]:80 Aug1315:18:53exampleKeepalived_healthcheckers[45369]:Removingservice[192.168.189.133]:80fromVS[192.168.189.200]:80 Aug1315:18:53examplekernel:IPVS:__ip_vs_del_service:enter Aug1315:18:54examplentpd[1525]:Deletinginterface#6eth0:1,192.168.189.200#123,interfacestats:received=0,sent=0,dropped=0,active_time=5131secs 备用节点日志 Aug1315:18:53exampleKeepalived_vrrp[1941]:VRRP_Instance(web)TransitiontoMASTERSTATE Aug1315:18:54exampleKeepalived_vrrp[1941]:VRRP_Instance(web)EnteringMASTERSTATE Aug1315:18:54exampleKeepalived_vrrp[1941]:VRRP_Instance(web)settingprotocolVIPs. Aug1315:18:54exampleKeepalived_vrrp[1941]:VRRP_Instance(web)SendinggratuitousARPsoneth0for192.168.189.200 Aug1315:18:54exampleKeepalived_healthcheckers[1940]:NetlinkreflectorreportsIP192.168.189.200added Aug1315:18:56examplentpd[1525]:Listennormallyon7eth0192.168.189.200UDP123 Aug1315:18:59exampleKeepalived_vrrp[1941]:VRRP_Instance(web)SendinggratuitousARPsoneth0for192.168.189.200 主节点keepalived恢复观察日志情况 [root@example~]#/etc/init.d/keepalivedstop Stoppingkeepalived:[OK] 主节点日志 Aug1315:22:04exampleKeepalived[45420]:StartingKeepalivedv1.2.13(03/19,2015) Aug1315:22:04exampleKeepalived[45421]:StartingHealthcheckchildprocess,pid=45423 Aug1315:22:04exampleKeepalived[45421]:StartingVRRPchildprocess,pid=45424 Aug1315:22:04exampleKeepalived_vrrp[45424]:NetlinkreflectorreportsIP192.168.189.130added Aug1315:22:04exampleKeepalived_vrrp[45424]:NetlinkreflectorreportsIPfe80::20c:29ff:fe5a:e1a3added Aug1315:22:04exampleKeepalived_vrrp[45424]:RegisteringKernelnetlinkreflector Aug1315:22:04exampleKeepalived_vrrp[45424]:RegisteringKernelnetlinkcommandchannel Aug1315:22:04exampleKeepalived_vrrp[45424]:RegisteringgratuitousARPsharedchannel Aug1315:22:04exampleKeepalived_healthcheckers[45423]:NetlinkreflectorreportsIP192.168.189.130added Aug1315:22:04exampleKeepalived_healthcheckers[45423]:NetlinkreflectorreportsIPfe80::20c:29ff:fe5a:e1a3added Aug1315:22:04exampleKeepalived_healthcheckers[45423]:RegisteringKernelnetlinkreflector Aug1315:22:04exampleKeepalived_healthcheckers[45423]:RegisteringKernelnetlinkcommandchannel Aug1315:22:05exampleKeepalived_vrrp[45424]:Openingfile'/etc/keepalived/keepalived.conf'. Aug1315:22:05exampleKeepalived_vrrp[45424]:Configurationisusing:63278Bytes Aug1315:22:05exampleKeepalived_vrrp[45424]:UsingLinkWatchkernelnetlinkreflector... Aug1315:22:05exampleKeepalived_vrrp[45424]:VRRPsockpool:[ifindex(2),proto(112),unicast(0),fd(10,11)] Aug1315:22:05exampleKeepalived_healthcheckers[45423]:Openingfile'/etc/keepalived/keepalived.conf'. Aug1315:22:05exampleKeepalived_healthcheckers[45423]:Configurationisusing:14742Bytes Aug1315:22:05exampleKeepalived_healthcheckers[45423]:UsingLinkWatchkernelnetlinkreflector... Aug1315:22:05exampleKeepalived_healthcheckers[45423]:Activatinghealthcheckerforservice[192.168.189.132]:80 Aug1315:22:05exampleKeepalived_healthcheckers[45423]:Activatinghealthcheckerforservice[192.168.189.133]:80 Aug1315:22:05exampleKeepalived_vrrp[45424]:VRRP_Instance(web)TransitiontoMASTERSTATE Aug1315:22:05exampleKeepalived_vrrp[45424]:VRRP_Instance(web)Receivedlowerprioadvert,forcingnewelection Aug1315:22:06exampleKeepalived_vrrp[45424]:VRRP_Instance(web)EnteringMASTERSTATE Aug1315:22:06exampleKeepalived_vrrp[45424]:VRRP_Instance(web)settingprotocolVIPs. Aug1315:22:06exampleKeepalived_vrrp[45424]:VRRP_Instance(web)SendinggratuitousARPsoneth0for192.168.189.200 Aug1315:22:06exampleKeepalived_healthcheckers[45423]:NetlinkreflectorreportsIP192.168.189.200added Aug1315:22:07examplentpd[1525]:Listennormallyon7eth0192.168.189.200UDP123 Aug1315:22:11exampleKeepalived_vrrp[45424]:VRRP_Instance(web)SendinggratuitousARPsoneth0for192.168.189.200 备用节点 Aug1315:22:05exampleKeepalived_vrrp[1941]:VRRP_Instance(web)Receivedhigherprioadvert Aug1315:22:05exampleKeepalived_vrrp[1941]:VRRP_Instance(web)EnteringBACKUPSTATE Aug1315:22:05exampleKeepalived_vrrp[1941]:VRRP_Instance(web)removingprotocolVIPs. Aug1315:22:05exampleKeepalived_healthcheckers[1940]:NetlinkreflectorreportsIP192.168.189.200removed Aug1315:22:07examplentpd[1525]:Deletinginterface#7eth0,192.168.189.200#123,interfacestats:received=0,sent=0,dropped=0,active_time=191secs Aug1315:22:54exampledhclient[1239]:DHCPREQUESToneth0to192.168.189.254port67(xid=0x6d287b9) Aug1315:22:54exampledhclient[1239]:DHCPACKfrom192.168.189.254(xid=0x6d287b9) Aug1315:22:56exampledhclient[1239]:boundto192.168.189.131--renewalin680seconds. 可以看到备用节点的vip地址被移除了,一切都符合预期 负载均衡测试,打开浏览器测试访问lvs的vip地址 故障切换测试 故障切换是测试在某个节点出现故障后,Keepalived监控模块是否能及时发现,然后屏蔽故障节点,同时将服务转移到正常节点上执行。 这里将real server 1节点服务停掉,假定这个节点出现故障,然后查看主、备机日志信息,相关日志如下: [root@example ~]# /etc/init.d/httpd stop Stopping httpd: [ OK ] 观察主节点keepalived日志情况 Aug1315:30:05exampleKeepalived_healthcheckers[45423]:TCPconnectionto[192.168.189.132]:80failed!!! Aug1315:30:05exampleKeepalived_healthcheckers[45423]:Removingservice[192.168.189.132]:80fromVS[192.168.189.200]:80 Aug1315:30:05exampleKeepalived_healthcheckers[45423]:RemoteSMTPserver[192.168.200.1]:25connected. Aug1315:30:35exampleKeepalived_healthcheckers[45423]:TimeoutreadingdatatoremoteSMTPserver[192.168.200.1]:25. 观察备用节点keepalived日志情况 Aug1315:30:04exampleKeepalived_healthcheckers[1940]:TCPconnectionto[192.168.189.132]:80failed!!! Aug1315:30:04exampleKeepalived_healthcheckers[1940]:Removingservice[192.168.189.132]:80fromVS[192.168.189.200]:80 Aug1315:30:04exampleKeepalived_healthcheckers[1940]:RemoteSMTPserver[192.168.200.1]:25connected. Aug1315:30:34exampleKeepalived_healthcheckers[1940]:TimeoutreadingdatatoremoteSMTPserver[192.168.200.1]:25. 通过日志可以看出,Keepalived监控模块检测到192.168.189.132这台主机出现故障后,将此节点从集群系统中剔除掉了。 此时访问http://192.168.189.200这个地址,应该只能看到“web02_192.168.189.133”了, 这是因为节点1出现故障,而Keepalived监控模块将节点1从集群系统中剔除了 再次重启web01服务观察主备keepalived日志情况 主节点keepalived日志情况 Aug1315:33:47exampleKeepalived_healthcheckers[45423]:TCPconnectionto[192.168.189.132]:80success. Aug1315:33:47exampleKeepalived_healthcheckers[45423]:Addingservice[192.168.189.132]:80toVS[192.168.189.200]:80 Aug1315:33:47exampleKeepalived_healthcheckers[45423]:RemoteSMTPserver[192.168.200.1]:25connected. Aug1315:34:17exampleKeepalived_healthcheckers[45423]:TimeoutreadingdatatoremoteSMTPserver[192.168.200.1]:25. 备用节点keepalived日志情况 Aug1315:33:46exampleKeepalived_healthcheckers[1940]:TCPconnectionto[192.168.189.132]:80success. Aug1315:33:46exampleKeepalived_healthcheckers[1940]:Addingservice[192.168.189.132]:80toVS[192.168.189.200]:80 Aug1315:33:46exampleKeepalived_healthcheckers[1940]:RemoteSMTPserver[192.168.200.1]:25connected. Aug1315:34:16exampledhclient[1239]:DHCPREQUESToneth0to192.168.189.254port67(xid=0x6d287b9) Aug1315:34:16exampledhclient[1239]:DHCPACKfrom192.168.189.254(xid=0x6d287b9) Aug1315:34:16exampleKeepalived_healthcheckers[1940]:TimeoutreadingdatatoremoteSMTPserver[192.168.200.1]:25. Aug1315:34:18exampledhclient[1239]:boundto192.168.189.131--renewalin778seconds. 从日志可知,Keepalived监控模块检测到192.168.189.132这台主机恢复正常后,又将此节点加入到集群系统中。 此时再次访问http://192.168.189.200这个地址,然后不断刷新此页面,应该又能分别看到“web01_192.168.189.132”和 “web01_192.168.189.133”页面了,这说明在web1节点恢复正常后,Keepalived监控模块将此节点加入到集群系统中。

优秀的个人博客,低调大师

Jquery+Json+Handler文件结合应用实例

1、页面script代码-【model数据、字符串】 <script type="text/javascript" charset="utf-8" src="Js/jquery-1.6.2.min.js"></script> <script type="text/javascript"> //提交验证 function ChekFrom() { var msg = ""; //取值 var UserName = $("#txtUserName").val(); //验证 if (UserName == "") { msg += "用户名为空!/r/n"; } else { $.ajax({ type: "POST", url: "/Controller/UserHandler.ashx", data: { Action: "IsUserName", UserName: UserName }, cache: false, //设置时候从浏览器中读取 缓存 true 表示 是 datatype: "json", async: false, success: function (result) { if (result.code == 1) { msg += "该用户名已存在!/r/n"; } } }); } if (msg == "") { return true; } else { alert(msg); return false; } } //总计金额 可以写成js方法 function BuyTotal() { var Total = $.ajax({ type: "POST", dataType: text,//xml html script json url: "/Controller/UserHandler.ashx", data: { Action: "BuyTotal" }, async: false }).responseText; return Total; } </script> 2、返回Model的handler代码 <%@ WebHandler Language="C#" Class="UserHandler" %> using System; using System.Web; using ECS.Utility; using System.Collections.Generic; //使用session的时候必须继承IRequiresSessionState接口:, System.Web.SessionState.IRequiresSessionState public class UserHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //全局默认返回信息 string Json = JsonUtil.Serialize(new { code = 0, msg = "" }); //取值 string Action = context.Request.Form["Action"] == null ? "" : context.Request.Form["Action"].ToString().Trim(); string UserName = context.Request.Form["UserName"] == null ? "" : context.Request.Form["UserName"].ToString().Trim(); //判断 if (!string.IsNullOrEmpty(Action)) { //验证用户 if (Action.Equals("IsUserName")) { //验证 if (IsExistUser(UserName)) { Json = JsonUtil.Serialize(new { code = 1, msg = "用户名已存在!" }); } else { Json = JsonUtil.Serialize(new { code = 0, msg = "用户名可以注册!" }); } } //计算金额 if (Action.Equals("BuyTotal")) { //Json格式 //Json = JsonUtil.Serialize(new { code = 1, msg = "成功", Total = 100 }); // Json = "100"; } else { //Json格式 //Json = JsonUtil.Serialize(new { code = 0, msg = "失败", Total = 0 }); Json = "0"; } } //最终返回信息 context.Response.Write(Json); } /// <summary> /// 判断UserName是否存在 /// </summary> /// <param name="userName">用户名</param> /// <returns>返回true or false</returns> public bool IsExistUser(string userName) { List<LSY.Model.A_User> UserList = new LSY.BLL.A_User().GetList(null, "UserName='" + userName.Trim() + "'and Type=0", null); if (UserList.Count > 0) { return true; } else { return false; } } public bool IsReusable { get { return false; } } } 3、页面script代码-【DataTable、List】 //查询城市 function getCity(CityVal) { var DDL_Province = $("#DDL_Province"); var DDL_City = $("#DDL_City"); DDL_City.empty(); DDL_City.append("<option value=\"0\">市/区</option>"); $.ajax( { type: "post", url: "/UserCart/Controller/CityAreas.ashx", data: { "type": "city", "provinceID": DDL_Province.val() }, dataType: "json", async: false, success: function (msg) { for (var i = 0; i < msg.length; i++) { if (CityVal == msg[i].CityName) { if (msg[i].IsCOD == 1) { DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "*</option>"); } else { DDL_City.append("<option value=" + msg[i].CityID + " selected=\"selected\">" + msg[i].CityName + "</option>"); } } else { if (msg[i].IsCOD == 1) { DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "*</option>"); } else { DDL_City.append("<option value=" + msg[i].CityID + " >" + msg[i].CityName + "</option>"); } } } getArea(''); GetAddreesSpan(); } }) }; 4、返回数据集Handler代码 <%@ WebHandler Language="C#" Class="CityAreas" %> using System; using System.Web; using System.Collections.Generic; public class CityAreas : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["type"] == "city") { context.Response.Write(select2(context.Request["provinceID"])); } else if (context.Request["type"] == "district") { context.Response.Write(select3(context.Request["cityID"])); } } public string select2(string id) { string str = string.Empty; if (!string.IsNullOrEmpty(id)) { List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=2 and ParentID=" + id, null); if (list != null && list.Count > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("["); foreach (ECS.Model.A_CityAreas item in list) { sb.Append("{"); sb.Append("\"CityID\":\"" + item.id + "\",\"CityName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\""); sb.Append("},"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); str = sb.ToString(); } } return str; } public string select3(string id) { string str = string.Empty; if (!string.IsNullOrEmpty(id)) { List<ECS.Model.A_CityAreas> list = new ECS.BLL.A_CityAreas().GetList(null, "deep=3 and ParentID=" + id, null); if (list != null && list.Count > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("["); foreach (ECS.Model.A_CityAreas item in list) { sb.Append("{"); sb.Append("\"DistrictID\":\"" + item.id + "\",\"DistrictName\":\"" + item.AreaName + "\",\"IsCOD\":\"" + item.IsCOD + "\""); sb.Append("},"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); str = sb.ToString(); } } return str; } public bool IsReusable { get { return false; } } }

优秀的个人博客,低调大师

左神算法、牛客网算法、剑指java、Java后台面试宝典----个人总结(实习、春招、秋招)

第一节课 第一题:题意与leetcode354的问题相同算法原型 最长递增子序列问题 * 题意:求出给定序列的最长递增子序列的长度,给定序列不是有序的,子序列不是子数组,元素在原数组中不必是连续的 * */ /* * solutions1: 时间复杂度O(n^2),空间复杂度O(n) * 新建一个辅助数组h,h[i]表示以nums[i]结尾的最长递增子序列的长度 * h[i]值的确定,需要nums[i]与nums[j](0<=j<i)做比较,h[i] = h[j] + 1 (when nums[j] < nums[i]),找出这些值中的最大值即最终的nums[i] * 如果nums[i]最小的话,则h[i] = 1; * 因为h[i]值的确定需要nums[i]进行最多i-1次比较,所以时间复杂度为O(n^2) */ /* * solution2: 时间复杂度O(n*log(n)),空间复杂度O(n) * 新建一个辅助数组h,h[i]表示遍历到当前nums[j]位置时长度为i+1的递增子序列的最小末尾 * h填入值的部分称作有效区,还未填入值的部分称作无效区 * h[i]值的确定,h[0] = nums[0],在遍历nums 的同时,在h[i]中找到第一个大于等于nums[j]的数,并将其替换为为nums[j],如果没找到则将h有效区后一个元素置为nums[j] * h[i]会一直保持有序状态,所以找第一个大于等于nums[j]的数可以用二分法,最后返回h有效区的长度即可 * 由于在遍历的同时采用了时间复杂度为log(n)的二分查找,故时间复杂度为O(n*log(n)) */ /* * 实现solution2 */ public class LongestSubSequence { public int getLonggestLen(int[] nums) { int maxLen = 0; if (nums.length == 0) return maxLen; int[] h = new int[nums.length]; h[0] = nums[0]; maxLen = 1; for (int i = 1; i < nums.length; i++) { int pos = getPos(h, 0, maxLen - 1,nums[i]); if (pos == -1) { h[maxLen++] = nums[i]; } else h[pos] = nums[i]; } return maxLen; } public int getPos(int[] h, int left, int right, int num) { if (left <= right) { int mid = left + (right - left) / 2; if (h[mid] >= num) { int pos = getPos(h, left, mid - 1, num); if (pos == -1) return mid; return pos; } else { return getPos(h, mid + 1, right, num); } } return -1; } public static void main(String[] args){ LongestSubSequence l = new LongestSubSequence(); int[] nums = {1,3,6,7,9,4,10,5,6}; l.getLonggestLen(nums); } } 第二题 * 题意:详见2016 《牛课堂第一节课课件.pdf》第2题,leetcode42原题 */ /* * solution1:时间复杂度 O(n) 空间复杂度O(n) * 首先简化一下题意:如果能求得当前位置格子上的水量,那么总水量就是每个位置水量之和 * 当前格子上所能存储的水量 = 当前格子左边最大值与右边最大值之间的较小值 - 当前格子高度 * 所以要先求出当前格子左边的最大值与右边最大值,对于右边最大值用数组r来辅助存储,从右往左遍历一下原数组即可得到r * 对于左边的最大值在遍历原数组的同时用一个全局变量记录下来就行,此时时间复杂度为O(n) 空间复杂度O(n),还没达到最优解 * * solution2:时间复杂度 O(n) 空间复杂度O(1) * 为了不使用辅助数组,我们采用双指针的方法 * 双指针left和right分别指向数组的第二个元素和倒数第二个元素,由题意可知第一个元素和最后一个元素的储水量都是零 * 当前元素的遍历则从第二个和倒数第二个元素开始,用leftMax和rightMax分别记录左边最大值和右边最大值,用一个全局变量totalWater 更新总的储水量 * 若leftMax < rightMax则结算左指针height[left],leftMax > height[left]时,totalWater += leftMax - height[left++],反之更新左边最大值leftMax = height[left++],左指针向右移一位; * 反之,结算右边的当前元素,过程与左边类似 */ public class Question1 { public int getWater(int[] height){ if (height == null || height.length < 3) return 0; int totalWater = 0; int left = 1, right = height.length - 2; int leftMax = height[0], rightMax = height[height.length - 1]; while (left <= right) { if (leftMax < rightMax) { if (leftMax > height[left]) { totalWater += leftMax - height[left++] ; } else { leftMax = height[left++]; } } else { if (rightMax > height[right]) { totalWater += rightMax - height[right--] ; } else { rightMax = height[right--]; } } } return totalWater; } public static void main(String[] args){ Question1 q = new Question1(); int[] height = {0,1,0,2,1,0,1,3,2,1,2,1}; q.getWater(height); } }

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码