Lvs+Keepalived实现高可用

LVS+KEEPAIVED
	
	
	IP规划
		KEEPAIVED 主为MASTER
			172.16.30.70
			
		KEEPAIVED 备用BACKUP	
			172.16.30.71
			
		VIP地址
			172.16.30.49
			
		WEB-1
			172.16.30.78
			
		WEB-2
			172.16.30.79
	
	
	
	
	
	安装
		yum install ipvsadm keepalived -y
	备份一下源配置文件:
		cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak -a
	
	keepalived
		主
			vim /etc/keepalived/keepalived.conf
			改成一下内容
			
			! Configuration File for keepalived

			global_defs {
					router_id Director1
			}
			
			vrrp_instance VI_1 {
				state MASTER
				interface eth0
				virtual_router_id 51
				priority 100
				advert_int 1
				authentication {
					auth_type PASS
					auth_pass 1111
				}
				virtual_ipaddress {
					172.16.30.49/24 dev eth0
				}
			}
			
			virtual_server 172.16.30.49 80 {
				delay_loop 3
				lb_algo rr
				lb_kind DR
				protocol TCP
			
				real_server 172.16.30.78 80 {
					weight 1
					TCP_CHECK {
							connect_timeput 3
					}
				}
			real_server 172.16.30.79 80 {
					weight 1
					TCP_CHECK {
							connect_timeput 3
					}
				}
			}
		
		
		
		
		备用
		
		vim /etc/keepalived/keepalived.conf
			改成一下内容
		
			
			! Configuration File for keepalived
            
            global_defs {
            		router_id Director2
            }
            
            vrrp_instance VI_1 {
            	state BACKUP
            	interface eth0
            	virtual_router_id 51
            	priority 80
            	advert_int 1
            	authentication {
            		auth_type PASS
            		auth_pass 1111
            	}
            	virtual_ipaddress {
            		172.16.30.49/24 dev eth0
            	}
            }
            
            virtual_server 172.16.30.49 80 {
            	delay_loop 3
            	lb_algo rr
            	lb_kind DR
            	protocol TCP
            
            	real_server 172.16.30.78 80 {
            		weight 1
            		TCP_CHECK {
            				connect_timeput 3
            		}
            	}
            real_server 172.16.30.79 80 {
            		weight 1
            		TCP_CHECK {
            				connect_timeput 3
            		}
            	}
            }
			
			
		解释:
			! Configuration File for keepalived
            
            global_defs {
            		router_id Director1			#两边的ID不一样
            }
            
            vrrp_instance VI_1 {
            	state MASTER					#主为MASTER 备用的为BACKUP
            	interface eth0                  #心跳网卡设备名称
            	virtual_router_id 51            #虚拟路由编号,主备要一致
            	priority 100                    #优先级1-255,数字越大优先级越高
            	advert_int 1                    #检测间隔,单位秒
            	authentication {                #
            		auth_type PASS              #
            		auth_pass 1111              #
            	}                               #
            	virtual_ipaddress {             #虚拟VIP地址
            		172.16.30.49/24 dev eth0    #VIP地址和工作端口和设备名称
            	}								#
            }                                   #
												#
            virtual_server 172.16.30.49 80 {    #LVS的虚拟IP地址和端口
            	delay_loop 3                    #服务轮询的时间间隔
            	lb_algo rr                      #LVS调度算法
            	lb_kind DR                      #LVS集群模式
            	protocol TCP                    #
												#
            	real_server 172.16.30.78 80 {   #后端web服务器IP和端口
            		weight 1                    #主机权重为1
            		TCP_CHECK {                 #TCP链接校验
            				connect_timeput 3   #链接超时时间为3,如果超过3秒连不上,服务端就会认为这台服务器挡掉了,就会把这台机器踢出去
            		}                           #
            	}                               #
            real_server 172.16.30.79 80 {
            		weight 1
            		TCP_CHECK {
            				connect_timeput 3
            		}
            	}
            }
			
	不需要启动LVS,只启动keepalived即可!!
	不需要启动LVS,只启动keepalived即可!!
	
	主节点启动
		启动keepalived
			systemctl start keepalived
		
		开机自启动
			systemctl enable keepalived
	
	备用节点启动
		启动keepalived
			systemctl start keepalived
			
		开机自启动	
        	systemctl enable keepalived
			
		web-1安装nginx
			172.16.30.78
			
		脚本安装
			vim  nginx.sh
			输入以下内容
			
			#!/bin/bash
			#install nginx
			yum install wget gcc pcre pcre-devel openssl-devel openssl gzip make cmake zlib-devel tar lrzsz -y
			cd
			wget http://nginx.org/download/nginx-1.18.0.tar.gz
			useradd -s /sbin/nologin  -M nginx
			tar zxvf nginx-1.18.0.tar.gz
			cd nginx-1.18.0/
			./configure  --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_mp4_module
			make 
			make install
			echo "
				[Unit]
			Description=nginx service
			After=network.target 
			
			[Service] 
			Type=forking 
			ExecStart=/usr/local/nginx/sbin/nginx
			ExecReload=/usr/local/nginx/sbin/nginx -s reload
			ExecStop=/usr/local/nginx/sbin/nginx -s quit
			PrivateTmp=true 
			
			[Install] 
			WantedBy=multi-user.target
			">/lib/systemd/system/nginx.service
			systemctl daemon-reload
			systemctl start nginx.service
			systemctl enable nginx.service
			
		再来执行脚本
			sh nginx.sh
			
		添加测试页面
			echo Nginx-1 > /usr/local/nginx/html/index.html
			
		测试访问
			[root@nginx-1 ~]# curl 127.0.0.1
			Nginx-1
			
		添加虚拟网卡,并编写虚拟网卡配置文件
			cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0 -a
			
		修改虚拟网卡配置文件
			
			vim /etc/sysconfig/network-scripts/ifcfg-lo:0
			改成以下内容
			DEVICE=lo:0
			IPADDR=172.16.30.49
			NETMASK=255.255.255.255
			ONBOOT=yes
			
			解释:
			DEVICE=lo:0					#设备名称lo:0
			IPADDR=172.16.30.49         #VIP地址也是就是keepalived里面写的VIP地址
			NETMASK=255.255.255.255     #子网掩码32位,不要写成24位
			ONBOOT=yes                  #开机启动此设备
			
		添加路由规则,这样开机就可以自动添加了
			echo /sbin/route add host 172.16.30.49 dev lo:0 >> /etc/rc.local
		
		添加权限
			chmod +x /etc/rc.local
		
		
		配置ARP,防止这个设备在局域网内广播,在最后一行添加
			vim /etc/sysctl.conf
			在最后一行添加以下内容
			
			net.ipv4.conf.all.arp_ignore = 1 
			net.ipv4.conf.all.arp_announce = 2
			net.ipv4.conf.default.arp_ignore = 1
			net.ipv4.conf.default.arp_announce = 2
			net.ipv4.conf.lo.arp_ignore = 1
			net.ipv4.conf.lo.arp_announce = 2
		
		重启让配置生效
			reboot	
			
			
		MASTER上查询LVS
			ipvsadm -L
			返回结果:
				IP Virtual Server version 1.2.1 (size=4096)
				Prot LocalAddress:Port Scheduler Flags
				-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
				TCP  172.16.30.49:80 rr
				-> 172.16.30.78:80              Route   1      0          0         
				-> 172.16.30.79:80              Route   1      0          0         
				
				
				
		web-2
			编译安装nginx,执行以上nginx的脚本即可
			
			安装好以后再来添加测试页面
			echo Nginx-2 > /usr/local/nginx/html/index.html
			
		测试访问
			[root@nginx-2 ~]# curl 127.0.0.1
			Nginx-2
			
			
		添加虚拟网卡,并编写虚拟网卡配置文件	
			cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0 -a	
				
		修改虚拟网卡配置文件	
				
			vim /etc/sysconfig/network-scripts/ifcfg-lo:0	
			改成以下内容	
			DEVICE=lo:0
			IPADDR=172.16.30.49	
			NETMASK=255.255.255.255	
			ONBOOT=yes	
				
			解释:	
        	DEVICE=lo:0					#设备名称lo:0
        	IPADDR=172.16.30.49         #VIP地址也是就是keepalived里面写的VIP地址
        	NETMASK=255.255.255.255     #子网掩码32位,不要写成24位
        	ONBOOT=yes                  #开机启动此设备
        	
        添加路由规则,这样开机就可以自动添加了
        	echo /sbin/route add host 172.16.30.49 dev lo:0 >> /etc/rc.local
        
        添加权限
        	chmod +x /etc/rc.local
        
        
        配置ARP,防止这个设备在局域网内广播,在最后一行添加
        	vim /etc/sysctl.conf
        	在最后一行添加以下内容
        	
        	net.ipv4.conf.all.arp_ignore = 1 
        	net.ipv4.conf.all.arp_announce = 2
        	net.ipv4.conf.default.arp_ignore = 1
        	net.ipv4.conf.default.arp_announce = 2
        	net.ipv4.conf.lo.arp_ignore = 1
        	net.ipv4.conf.lo.arp_announce = 2
        
        重启让配置生效
        	reboot	
			
			
	这样LVS调度就完成,可以用浏览器访问vip地址 172.16.30.49
		172.16.30.49
			
			
			
			
			
			
			
			
			
			
			


Powered By Z-BlogPHP 1.7.3

 Theme By 优美模版

本博客为开源博客,本博客联系QQ:372097243