Linux LVS/NAT搭建集群环境

2021-03-18  本文已影响0人  Yohann丶blog
WechatIMG593.jpeg

Linux LVS/NAT搭建集群环境

环境

如需按照 docker,前往 Docker 快速安装

安装ipvsadm

$ apt-get update
$ apt-get install ipvsadm
$ ipvsadm -v

配置集群服务器

# server1和server2
$ docker run --privileged --name=server1 -tdi ubuntu
$ docker run --privileged --name=server2 -tdi ubuntu
root@e73bba5a5b33:/# docker attach server1
root@e73bba5a5b33:/# apt-get update
root@e73bba5a5b33:/# apt-get install vim nginx net-tools -y 
root@e73bba5a5b33:/# service nginx start
root@e73bba5a5b33:/# vim /var/www/html/index.nginx-debian.html
<h1>Welcome to nginx</h1>
修改为
<h1>Welcome to nginx!server1</h1>

修改nginx默认展示的html页面,以区分访问的server1还是server2。

root@e73bba5a5b33:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 19713  bytes 48143630 (48.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11346  bytes 827130 (827.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

查看到server1的私有IP是172.17.0.2,使用快捷键ctrl+p+q退出当前容器,登录 server2 做以上同样的配置。

配置负载均衡器

# 开启当前主机的内核路由转发
$ echo '1' | sudo tee /proc/sys/net/ipv4/ip_forward
# 查看是否开启,返回1说明已开启
$ cat /proc/sys/net/ipv4/ip_forward
1 
# 创建集群服务
$ ipvsadm -A -t 172.18.119.29:80 -s rr
# 添加 server1
$ ipvsadm -a -t 172.18.119.29:80 -r 172.17.0.2 -m
# 添加 server2
$ ipvsadm -a -t 172.18.119.29:80 -r 172.17.0.3 -m
# 查看 ipvs 定义的规则
$ ipvsadm -l

172.18.119.29是当前主机的私有IP172.17.0.2172.17.0.3分别是server1和server2的私有IP。

测试负载均衡

2021-03-18_6052f60c5ad29.png
2021-03-18_6052f63705bbb.png

浏览器访问当前主机的公网IP,通过多次刷新看到页面变化,说明搭建成功。

上一篇下一篇

猜你喜欢

热点阅读