docker-compose实现nginx负载均衡
环境:docker1:rhel7系统,已经安装了docker软件:docker-compose ===> 点击下载 提取码: suef1.部署使用docker-compose首先使用了scp命令将docker-compose传到了我的虚拟机[root@docker1 ~]# lsdockerdocker-compose-Li...
·
环境:
docker1:rhel7系统,已经安装了docker
软件:docker-compose ===> 点击下载 提取码: suef
1.部署使用docker-compose
首先使用了scp命令将docker-compose传到了我的虚拟机
[root@docker1 ~]# ls
docker docker-compose-Linux-x86_64-1.22.0 images
[root@docker1 ~]# mv docker-compose-Linux-x86_64-1.22.0 /usr/local/bin/docker-compose
[root@docker1 ~]# docker-compose ##实验一下,若可以补齐,则为可以使用
2.编写yml文件
[root@docker1 tmp]# cd /tmp/
[root@docker1 tmp]# mkdir docker
[root@docker1 tmp]# cd docker/
[root@docker1 docker]# mkdir compose
[root@docker1 docker]# cd compose/
[root@docker1 compose]# vim docker-compose.yml ##配置文件
web1:
image: nginx
expose:
- 80
volumes:
- ./web1:/usr/share/nginx/html
web2:
image: nginx
expose:
- 80
volumes:
- ./web2:/usr/share/nginx/html
haproxy:
image: haproxy
volumes:
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
links:
- web1
- web2
ports:
- "80:80"
expose:
- "80"
3.编写网页信息
[root@docker1 compose]# mkdir web1
[root@docker1 compose]# mkdir web2
[root@docker1 compose]# echo we1 > web1/index.html ##两个网页不同内容,以示区分
[root@docker1 compose]# echo we2 > web2/index.html
4.配置haproxy
[root@docker1 compose]# mkdir haproxy
[root@docker1 compose]# cd haproxy/
[root@docker1 haproxy]# vim haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
stats uri /status
frontend balancer
bind 0.0.0.0:80
default_backend web_backends
backend web_backends
balance roundrobin
server server1 web1:80 check
server server2 web2:80 check
5.导入镜像
如果本来就有镜像,就不用导入了
[root@docker1 images]# docker load -i haproxy.tar
917c0fc99b35: Loading layer 130.9MB/130.9MB
5f70bf18a086: Loading layer 1.024kB/1.024kB
c205bb11f213: Loading layer 4.684MB/4.684MB
ffef890bdf7b: Loading layer 9.549MB/9.549MB
3ec368642ee3: Loading layer 2.048kB/2.048kB
Loaded image: haproxy:latest
[root@docker1 images]# docker load -i nginx.tar
014cf8bfcb2d: Loading layer 58.46MB/58.46MB
832a3ae4ac84: Loading layer 53.91MB/53.91MB
e89b70d28795: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
6.运行测试
[root@docker1 compose]# docker-compose up -d
Creating compose_web2_1 ... done
Creating compose_web1_1 ... done
Creating compose_haproxy_1 ... done
如上图成功
7.其他指令
docker-compose start ##开始docker-compose
docker-compose stop ##停止docker-compose
docker-compose rm ##删除某个项目
更多推荐
已为社区贡献14条内容
所有评论(0)