我是靠谱客的博主 复杂帽子,最近开发中收集的这篇文章主要介绍docker基于haproxy实现容器(nginx、apache)的负载均衡,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

####第一步:导入haproxy镜像

[root@foundation23 docker]# docker load -i haproxy.tar
917c0fc99b35: Loading layer 130.9 MB/130.9 MB
5f70bf18a086: Loading layer 1.024 kB/1.024 kB
c205bb11f213: Loading layer 4.684 MB/4.684 MB
ffef890bdf7b: Loading layer 9.549 MB/9.549 MB
3ec368642ee3: Loading layer 2.048 kB/2.048 kB
Loaded image: haproxy:latest

这里写图片描述
####第二步:安装docker-compose命令

[root@foundation23 docker]# cd /usr/local/bin/
[root@foundation23 bin]# ls
rht-vmctl
rht-vmicons
rht-vmsetkeyboard
[root@foundation23 bin]# ls
docker-compose-Linux-x86_64-1.22.0
rht-vmicons
rht-vmctl
rht-vmsetkeyboard
[root@foundation23 bin]# chmod +x docker-compose-Linux-x86_64-1.22.0
[root@foundation23 bin]# ln -s docker-compose-Linux-x86_64-1.22.0 docker-compose

这里写图片描述

####配置haproxy的文件,实现负载均衡
这里的rhel7:v1是之前创建的镜像,具体的查看创建apache镜像博客

[root@foundation23 bin]# cd /tmp/docker/
[root@foundation23 docker]# mkdir compose
[root@foundation23 docker]# cd compose/
[root@foundation23 compose]# docker inspect haproxy
[
"Cmd": [
"haproxy",
"-f",
"/usr/local/etc/haproxy/haproxy.cfg"
[root@foundation23 compose]# vim docker-compose.yml
apache:
image: rhel7:v1
expose:
- 80
volumes:
- ./web:/var/www/html
nginx:
image: nginx
expose:
- 80
haproxy:
image: haproxy
volumes:
- ./haproxy:/usr/local/etc/haproxy
links:
- apache
- nginx
ports:
- "8080:80"
expose:
- 80
[root@foundation23 compose]# cd ..
[root@foundation23 docker]# cp -r web/ compose/
[root@foundation23 docker]# cd compose/
[root@foundation23 compose]# ls
docker-compose.yml
web

这里写图片描述
####编辑haproxy的配置文件

[root@foundation23 compose]# mkdir haproxy
[root@foundation23 compose]# cd haproxy/
[root@foundation23 haproxy]# vim haproxy.cfg
[root@foundation23 haproxy]# cd ..
[root@foundation23 compose]# docker-compose -v
docker-compose version 1.22.0, build f46880fe
[root@foundation23 compose]# docker-compose up
Creating compose_nginx_1
... done
Creating compose_apache_1 ... done
Creating compose_haproxy_1 ... done
Attaching to compose_nginx_1, compose_apache_1, compose_haproxy_1
apache_1
| AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message
haproxy_1
| <7>haproxy-systemd-wrapper: executing /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
nginx_1
| 2018/08/22 03:49:51 [error] 7#7: *1 open() "/usr/

这里写图片描述

[root@foundation23 compose]# docker ps -a
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
90b268360c70
haproxy
"/docker-entrypoin..."
2 minutes ago
Exited (0) 15 seconds ago
compose_haproxy_1
ecbbfe401523
rhel7:v1
"/usr/sbin/httpd -..."
2 minutes ago
Exited (0) 15 seconds ago
compose_apache_1
e56353584938
nginx
"nginx -g 'daemon ..."
2 minutes ago
Exited (0) 15 seconds ago
compose_nginx_1
[root@foundation23 compose]# docker-compose start
Starting apache
... done
Starting nginx
... done
Starting haproxy ... done
[root@foundation23 compose]# docker ps -a
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
90b268360c70
haproxy
"/docker-entrypoin..."
3 minutes ago
Up 4 seconds
0.0.0.0:8080->80/tcp
compose_haproxy_1
ecbbfe401523
rhel7:v1
"/usr/sbin/httpd -..."
3 minutes ago
Up 4 seconds
80/tcp
compose_apache_1
e56353584938
nginx
"nginx -g 'daemon ..."
3 minutes ago
Up 4 s

这里写图片描述
这里写图片描述

###测试:
浏览器输入172.25.254.23:8080
这里写图片描述
这里是将web映射到apache的默认发布目录,呈现出来的是web目录里面的内容
这里写图片描述
监控界面
这里写图片描述

最后

以上就是复杂帽子为你收集整理的docker基于haproxy实现容器(nginx、apache)的负载均衡的全部内容,希望文章能够帮你解决docker基于haproxy实现容器(nginx、apache)的负载均衡所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部