小豆浆 lvs集群搭建 www.xdoujiang.com

  • A+
所属分类:linux实战
一、基础信息
1、IP、主机名、软件、系统版本
IP 主机名 软件 系统版本
192.168.4.73 lvs lvs centos7
192.168.4.77 lvs lvs centos7
192.168.4.89 nginx1 nginx1 centos7
192.168.4.94 nginx2 nginx2 centos7
192.168.4.244 VIP虚拟IP地址
2、iptables(四台)
root@192.168.4.73:~# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
3、selinux(四台)
root@192.168.4.73:~# getenforce
Disabled
二、开始lvs搭建73 77(二台机器)
1、yum安装包
root@192.168.4.73:~# yum -y install ipvsadm
2、先看下初始状态 一会需要配置 73 77(二台机器)
root@192.168.4.73:~# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
3、在看下模块是否加载成功 73 77(二台机器)
root@192.168.4.73:~# lsmod|grep ip
ip_vs 141092 0
nf_conntrack 133387 1 ip_vs
libcrc32c 12644 2 ip_vs,nf_conntrack
ip_tables 27115 0
4、我这里网卡名称是ens33 绑定vip的IP地址 73 77(二台机器)
root@192.168.4.73:~# ip addr add 192.168.4.244/24 dev ens33
root@192.168.4.73:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fb:e0:c9 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.73/24 brd 192.168.4.255 scope global dynamic ens33
valid_lft 85408sec preferred_lft 85408sec
inet 192.168.4.244/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::eeba:cb52:add:522/64 scope link
valid_lft forever preferred_lft forever
5、清除当前所有LVS规则 73 77(二台机器)
root@192.168.4.73:~# ipvsadm -C
6、设置tcp、tcpfin、udp链接超时时间(--set) 73 77(二台机器)
root@192.168.4.73:~# ipvsadm --set 30 5 60
7、添加虚拟服务(-A),-t指定虚拟服务的IP端口, 73 77(二台机器)
-s 指定调度算法 调度算法见man ipvsadm, rr wrr 权重轮询 -p 指定超时时间
root@192.168.4.73:~# ipvsadm -A -t 192.168.4.244:80 -s wrr -p 20
8、将虚拟服务关联到真实服务上(-a) -r指定真实服务的IP端口 -g LVS的模式 DR模式 -w 指定权重 73 77(二台机器)
root@192.168.4.73:~# ipvsadm -a -t 192.168.4.244:80 -r 192.168.4.89:80 -g -w 1
root@192.168.4.73:~# ipvsadm -a -t 192.168.4.244:80 -r 192.168.4.94:80 -g -w 1
9、使用命令查看下之前设置的 73 77(二台机器)
root@192.168.4.73:~# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.4.244:80 wrr persistent 20
-> 192.168.4.89:80 Route 1 0 0
-> 192.168.4.94:80 Route 1 0 0
PS:
-L, -l, --list
List the virtual server table if no argument is specified. If a service-address is selected,
list this service only. If the -c option is selected, then display the connec‐
tion table. The exact output is affected by the other arguments given.
-n, --numeric
Numeric output. IP addresses and port numbers will be printed in numeric format
rather than as as host names and services respectively, which is the default.
10、接下来是在2台rs上配置89和94(二台)
root@192.168.4.89:~# ip addr add 192.168.4.244/32 dev lo
root@192.168.4.89:~# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
11、在89和94上安装nginx 一会测试需要(二台)
root@192.168.4.89:~# yum -y install nginx
root@192.168.4.89:~# systemctl start nginx
12、创建主页内容(二台)
root@192.168.4.89:~# echo "nginx1" > /usr/share/nginx/html/index.html
13、我是在1.92上测试
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.89
nginx1
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.94
nginx2
14、我是在1.92上测试 测试虚拟vip192.168.4.244
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.244
nginx2
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.244
nginx2
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.244
nginx1
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.244
nginx2
15、我这里测试当73机器挂掉了 整个页面还是可以访问的
root@192.168.4.73:~# poweroff
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.244
nginx1
[root@FULIHUIWGJ-92 ~]#
[root@FULIHUIWGJ-92 ~]# curl 192.168.4.244
nginx2
整个lvs还是可以使用的

参考:http://mp.weixin.qq.com/s/HAmAyIbwkLhLIfH3VifRmQ

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: