一、安装lvs
sudo apt-get install ipvsadm
二、安装keepalived
sudo apt-get install keepalived
三、创建keepalived.conf文件
sudo gedit /etc/keepalived/keepalived.conf
四、配置keepalived.conf
# Global Configuration global_defs { lvs_id director1 } # VRRP Configuration vrrp_instance LVS { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.2.68 } # Virtual Server Configuration - for WWW service virtual_server 192.168.2.68 80 { delay_loop 1 lb_algo rr lb_kind DR persistence_timeout 60 protocol TCP # Real Server 1 configuration real_server 192.168.2.67 8070 { weight 3 TCP_CHECK { connection_timeout 10 nb_get_retry 3 delay_before_retry 3 } } # Real Server 2 configuration real_server 192.168.2.66 8070 { weight 1 TCP_CHECK { connection_timeout 10 nb_get_retry 3 delay_before_retry 3 } } } }
1.配置vrrp ip为68(启动keepalived之后可使用ip addr可查看)
2.配置real_server 此处是67和66的一个集群
五、启动并查看keepalived
#service keepalived start 查看 keepalived状态 # ps -ef | grep keepalived root 4783 1 0 09:49 ? 00:00:00 keepalived -D root 4784 4783 0 09:49 ? 00:00:01 keepalived -D root 5191 4896 0 13:43 pts/2 00:00:00 grep keepalived 如果未能启动,请使用sudo启动或者检查配置文件 # tail -f /var/log/messages Feb 21 14:18:48 localhost Keepalived_vrrp: Registering gratutious ARP shared channel Feb 21 14:18:48 localhost Keepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘. Feb 21 14:18:48 localhost Keepalived_vrrp: Configuration is using : 35706 Bytes Feb 21 14:18:48 localhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector... Feb 21 14:18:49 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(9,10)] Feb 21 14:18:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE Feb 21 14:18:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE Feb 21 14:18:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. Feb 21 14:18:51 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.2.150 Feb 21 14:18:51 localhost avahi-daemon[2549]: Registering new address record for 192.168.2.150 on eth0. 可以看到VIP已经在主服务器上开启
到此lvs+keepalived就已经完成了,接下来就是nginx.
时间: 2024-10-24 01:36:02