操作系统centos7.3 ip:192.168.0.130
docker下部署一个haproxy容器,两个tomcat容器
测试方便,关闭selinux、关闭iptables
安装和启动docker,请参照Centos7安装Docker最新版
1、部署tomcat
docker run -d -p 8081:8080 --name tomcat1 tomcat
docker run -d -p 8082:8080 --name tomcat2 tomcat
2、创建新首页
vi index.jsp
this is tomcat1 #分别修改为1、2方便识别
3、将新首页分别复制到tomcat容器内
docker cp index.jsp tomcat1:/usr/local/tomcat/webapps/ROOT/index.jsp
docker cp index.jsp tomcat2:/usr/local/tomcat/webapps/ROOT/index.jsp
4、测试
这样两个个web服务创建成功!
1、准备haproxy.cfg配置文件
global
log /dev/log local0
log /dev/log local1 notice
# chroot /var/lib/haproxy
stats socket /var/run/haproxy-admin.sock mode 660 level admin
stats timeout 30s
# user haproxy
# group haproxy
daemon
nbproc 1
defaults
log global
timeout connect 5000
timeout client 10m
timeout server 10m
listen admin_stats
bind 0.0.0.0:1080
mode http
log 127.0.0.1 local0 err
stats refresh 30s
stats uri /status
stats realm welcome login\ Haproxy
stats auth admin:123456 #登录http://192.168.0.130:1080/status 管理页的账号密码
stats hide-version
stats admin if TRUE
listen kube-master
bind 0.0.0.0:1080
mode tcp
option tcplog
balance roundrobin
server tomcat 192.168.0.130:8081 check inter 2000 fall 2 rise 2 weight 1
server tomcat1 192.168.0.130:8082 check inter 2000 fall 2 rise 2 weight 1
2、部署haproxy
docker run -d -p 1080:1080 /src/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg haproxy
3、浏览haproxy状态管理页
http://192.168.0.130:1080/status
账号:admin 密码:123456
4、测试负载均衡请求
[root@master ~]# while true; do wget -O - -q http://192.168.0.130:1080;sleep 3; done
出现
this is tomcat1
this is tomcat2
this is tomcat1
this is tomcat2
this is tomcat1
this is tomcat2
this is tomcat1
this is tomcat2
this is tomcat1
说明配置成功
从上述结果可知,前端对1080端口的请求,被Haproxy的负载均衡器,均衡请求到后端8081、8082端口。
这样当两个中的一个出现故障,流量则能正常分发到剩余那个正常的web上,从来提高了系统可靠性。
在单个服务器上安装也是同理,各自默认安装并根据上述的配置文件即可实现。