compose简介、安装、负载均衡
- 一、compose简介
-
- Docker Compose 中有两个重要的概念:
- 二、Docker Compose实践
-
- 1.docker compose安装
- 2.导入镜像
- 3.docker-compose.yml 属性
- 4.新建haproxy配置文件:
- 5.流程
- 6.运行docker-compose
- 7.真机测试,实现负载均衡
- 8.Docker Compose 常用命令
一、compose简介
- 微服务架构的应用系统一般包含若干个微服务,每个微服务一般都会部署多个实例,如果每个微服务都要手动启停,那么效率之低,维护量之大可想而知。
- Docker Compose是一种编排服务,基于pyhton语言实现,是一个用于在 Docker 上定义并运行复杂应用的工具,可以让用户在集群中部署分布式应用。
- 用户可以很容易地用一个配置文件定义一个多容器的应用,然后使用一条指令安装这个应用的所有依赖,完成构建。
- 解决了容器与容器之间如何管理编排的问题。
Docker Compose 中有两个重要的概念:
- 服务 (service) :一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。
- 项目 (project) :由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。
17.Docker(八)-----Docker三剑客之compose一、compose简介二、Docker Compose实践
二、Docker Compose实践
1.docker compose安装
方式1:(官方推荐,外网但是慢)
# curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
方式2:(阿里云 国内推荐,速度快)
https://mirrors.aliyun.com/docker-toolbox/linux/compose/1.21.2/
下载后放在: /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
2.导入镜像
[[email protected] ~]# lftp 172.25.15.250
lftp 172.25.15.250:~> cd pub/images/
lftp 172.25.15.250:/pub/images> get haproxy.tar #下载镜像
145218560 bytes transferred
lftp 172.25.15.250:/pub/images> exit
[[email protected] ~]# 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
The image haproxy:latest already exists, renaming the old one with ID sha256:271c3cfa9d6bd4ac64f3204fee4b75a3a275378fcfc9ad3f314e4e4cfecaa3c3 to empty string
Loaded image: haproxy:latest
3.docker-compose.yml 属性
Image:指定为镜像名称或镜像 ID,如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。
Build:指定 Dockerfile 所在文件夹的路径。 Compose 将会利用它自动构建这个镜像,然后使用这个镜像。
Command:覆盖容器启动后默认执行的命令。
Links:链接到其它服务中的容器。
Ports:端口映射。
Expose::暴露端口信息
Volumes:卷挂载路径设置
##创建docker-compose.yml文件:
vim docker-compose.yml
version: "3.8"
services:
web1:
image: nginx
networks:
- webnet
volumes:
- ./web1:/usr/share/nginx/html
web2:
image: nginx
networks:
- webnet
volumes:
- ./web2:/usr/share/nginx/html
haproxy:
image: haproxy
networks:
- webnet
volumes:
- ./haproxy:/usr/local/etc/haproxy
ports:
- "80:80"
networks:
webnet:
4.新建haproxy配置文件:
[[email protected] compose]# cd haproxy/
[[email protected] haproxy]# vim haroxy.cfg
#
# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
#
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.
#
global
maxconn 65535
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0
uid 200
gid 200
daemon
defaults
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
retries 2
option redispatch
timeout connect 5s
timeout server 5s
stats uri /admin/stats
# The public 'www' address in the DMZ
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
default_backend dynamic
# The static backend backend for 'Host: img', /img and /css.
backend dynamic
balance roundrobin
server a web1:80 check inter 1000
server b web2:80 check inter 1000
5.流程
[[email protected] ~]# mkdir compose
[[email protected] ~]# cd compose/
[[email protected] compose]# ls
[[email protected] compose]# mkdir haproxy
[[email protected] compose]# cd haproxy/
[[email protected] haproxy]# vim haroxy.cfg
[[email protected] haproxy]# cd ..
[[email protected] compose]# vim docker-compose.yml
[[email protected] compose]# mkdir web1
[[email protected] compose]# mkdir web2
[[email protected] compose]# echo web1 > web1/index.html
[[email protected] compose]# echo web2 > web2/index.html
[ro[email protected] compose]# ls
docker-compose.yml haproxy web1 web2
6.运行docker-compose
docker-compose命令必须在项目下运行。
以下是项目目录结构:
[[email protected] compose]# docker-compose up -d #创建并启动容器
Starting compose_web2_1 ... done
Starting compose_web1_1 ... done
Starting compose_haproxy_1 ... done
[[email protected] compose]# docker-compose ps
7.真机测试,实现负载均衡
[[email protected] pub]# curl 172.25.15.1
web2
[[email protected] pub]# curl 172.25.15.1
web1
[[email protected] pub]# curl 172.25.15.1
web2
[[email protected] pub]# curl 172.25.15.1
web1
[[email protected] pub
** 关闭一个,另一个自动接管**
8.Docker Compose 常用命令
Build: 构建或重新构建服务。
kill:强制停止服务容器。
logs:查看服务的输出。
port:打印绑定的公共端口。
ps:列出所有容器。
pull:拉取服务所需镜像。
rm:删除停止的服务容器。
up:构建并启动容器。