容器技术|Docker三剑客之docker-compose
三剑客简介
docker-machine
docker技术是基于Linux内核的cgroup技术实现的,那么问题来了,在非Linux平台上是否就不能使用docker技术了呢?答案是可以的,不过显然需要借助虚拟机去模拟出Linux环境来。
docker-machine就是docker公司官方提出的,用于在各种平台上快速创建具有docker服务的虚拟机的技术,甚至可以通过指定driver来定制虚拟机的实现原理(一般是virtualbox)。
docker-compose
docker镜像在创建之后,往往需要自己手动pull来获取镜像,然后执行run命令来运行。当服务需要用到多种容器,容器之间又产生了各种依赖和连接的时候,部署一个服务的手动操作是令人感到十分厌烦的。
dcoker-compose技术,就是通过一个.yml配置文件,将所有的容器的部署方法、文件映射、容器连接等等一系列的配置写在一个配置文件里,最后只需要执行docker-compose up命令就会像执行脚本一样的去一个个安装容器并自动部署他们,极大的便利了复杂服务的部署。
docker-swarm
swarm是基于docker平台实现的集群技术,他可以通过几条简单的指令快速的创建一个docker集群,接着在集群的共享网络上部署应用,最终实现分布式的服务。
docker-compose 的工作流程
使用Compose仅需要三步:
1、用一个定义你的应用程序的环境,Dockerfile这样它就可以在任何地方再现。
2、定义组成您的应用程序的服务,docker-compose.yml 以便它们可以在隔离的环境中一起运行。
3、运行docker-compose up和撰写启动并运行您的整个应用程序。
参考文章:https://docs.docker.com/compose/overview/
docker-compose安装布署
[root@docker ~]# curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 396 0 --:--:-- 0:00:01 --:--:-- 397
100 10.3M 100 10.3M 0 0 678k 0 0:00:15 0:00:15 --:--:-- 1876k
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
[root@docker ~]# docker-compose --version
docker-compose version 1.21.2, build a133471
docker-compose管理命令介绍
Compose具有管理应用程序整个生命周期的命令:
- 启动,停止和重建服务
- 查看正在运行的服务的状态
- 流式传输运行服务的日志输出
- 在服务上运行一次性命令
[root@docker ~]# docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
docker-compose 运行时是需要指定service名称,可以同时指定多个,也可以不指定。不指定时默认就是对配置文件中所有的service执行命令。
-f #用于指定配置文件
-p #用于指定项目名称
**docker-compose build **
用来创建或重新创建服务使用的镜像
docker-compose build service_a
创建一个镜像名叫service_a
docker-compose kill
用于通过容器发送SIGKILL信号强行停止服务
docker-compose logs
显示service的日志信息
docker-compose pause/unpause
docker-compose pause #暂停服务
docker-compose unpause #恢复被暂停的服务
docker-compose port
用于查看服务中的端口与物理机的映射关系
docker-compose port nginx_web 80
查看服务中80端口映射到物理机上的那个端口
dokcer-compose ps
用于显示当前项目下的容器
注意,此命令与docker ps不同作用,此命令会显示停止后的容器(状态为Exited),只征对某个项目。
docker-compose pull
用于拉取服务依赖的镜像
docker-compose restart
用于重启某个服务中的所有容器
docker-compose restart service_name
只有正在运行的服务可以使用重启命令,停止的服务是不可以重启
docker-compose rm
删除停止的服务(服务里的容器)
-f #强制删除
-v #删除与容器相关的卷(volumes)
docker-compose run
用于在服务中运行一个一次性的命令。这个命令会新建一个容器,它的配置和srvice的配置相同。
但两者之间还是有两点不同之处
1、run指定的命令会直接覆盖掉service配置中指定的命令
2、run命令启动的容器不会创建在service配置中指定的端口,如果需要指定使用--service-ports指定
docker-compose start/stop
docker-compose start 启动运行某个服务的所有容器
docker-compose stop 停止运行某个服务的所有容器
docker-compose scale
指定某个服务启动的容器个数
[root@docker ~]# docker-compose scale --help
Numbers are specified in the form `service=num` as arguments.
For example:
$ docker-compose scale web=2 worker=3
This command is deprecated. Use the up command with the `--scale` flag
instead.
Usage: scale [options] [SERVICE=NUM...]
Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
docker-compose配置文件实例
一个docker-compose.yml的实例文件如下
version: "3"
services:
nginx:
container_name: web-nginx
image: nginx:latest
restart: always
ports:
- 80:80
volumes:
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
下面将配置文件做个简单的解释说明
docker-compose的配置文件是一个.yml格式的文件
第一部分
version: "3" #指定语法的版本
第二部分
services: #定义服务
nginx: #服务的名称,-p参数后接服务名称
container_name: web-nginx #容器的名称
image: nginx:latest #镜像
restart: always
ports: #端口映射
- 80:80
第三部分
volumes: #物理机与容器的磁盘映射关系
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
整体的目录结构如下
[root@docker docker]# tree ./
./
├── docker-compose.yml
├── nginx
│?? └── nginx.conf
└── webserver
└── index.html
2 directories, 3 files
配置文件如下
[root@docker docker]# cat webserver/index.html
welcome to nginx server!!!!!!!!!
[root@docker docker]# cat nginx/nginx.conf
#user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
client_max_body_size 10m;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /webserver;
index index.html index.htm;
}
}
include /etc/nginx/conf.d/*.conf;
}
使用命令启动容器
[root@docker docker]# docker-compose up -d
Pulling nginx (nginx:1.14)...
Trying to pull repository docker.io/library/nginx ...
1.14: Pulling from docker.io/library/nginx
f2aa67a397c4: Already exists
6160d1ac49e9: Pull complete
046b67408776: Pull complete
Digest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6
Status: Downloaded newer image for docker.io/nginx:1.14
Creating nginx-server ... done
[root@docker docker]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 891 root 4u IPv6 1187080 0t0 TCP *:http (LISTEN)
[root@docker docker]# docker ps |grep nginx
07ca899cc44b nginx:1.14 "nginx -g 'daemon ..." 29 seconds ago Up 28 seconds 0.0.0.0:80->80/tcp nginx-server
#如果启动时不指定里面的服务名称,就是直接启动配置文件里所有的服务
浏览器测试是否正常访问
然后我们修改相应的首页文件如下
[root@docker docker]# cat webserver/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>welcome to nginx web stie</title>
</head>
<body>
<h2>欢迎来nginx站点</h2>
</body>
</html>
再次打开浏览器查看效果
#是不是非常的方便
使用Docker-Compose部署nginx代理Tomcat集群,实现负载均衡
大体步骤分为以下四步
1、下载所需的文件tomcat,jdk
2、编写dockerfile来布署tomcat与java环境,生成镜像文件
3、编写docker-compose.yml配置文件,启动所有容器服务
4、测试负载均衡
具体配置文件如下
#整个目录结构
[root@master java]# tree ./
./
├── docker-compose.yml
├── etc
│ └── localtime
├── nginx
│ └── nginx.conf
├── tomcat
│ ├── apache-tomcat-8.5.31.tar.gz
│ ├── Dockerfile
│ └── jdk-8u144-linux-x64.tar.gz
└── webserver
├── tomcatA
│ └── index.jsp
└── tomcatB
└── index.jsp
6 directories, 8 files
两个测试首页文件
[root@master java]# cat webserver/tomcatA/index.jsp
welcome to tomcat-A server
[root@master java]# cat webserver/tomcatB/index.jsp
welcome to tomcat-B server
配置文件
[root@master java]# cat docker-compose.yml
version: "3"
services:
nginx:
image: nginx:1.14
restart: always
ports:
- 80:80
links:
- tomcat1:tomcat1
- tomcat2:tomcat2
volumes:
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./etc/localtime:/etc/localtime
depends_on:
- tomcat1
- tomcat2
tomcat1:
hostname: tomcat1
build: ./tomcat
volumes:
- ./webserver/tomcatA:/usr/local/apache-tomcat-8.5.31/webapps/ROOT
- ./etc/localtime:/etc/localtime
tomcat2:
hostname: tomcat2
build: ./tomcat
volumes:
- ./webserver/tomcatB:/usr/local/apache-tomcat-8.5.31/webapps/ROOT
- ./etc/localtime:/etc/localtime
安装JAVA环境
[root@master java]# cat tomcat/Dockerfile
FROM centos
ADD jdk-8u144-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_144
ADD apache-tomcat-8.5.31.tar.gz /usr/local
EXPOSE 8080
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.31/bin/catalina.sh", "run"]
启动所有容器服务
[root@master java]# docker-compose up
Building tomcat1
Step 1/6 : FROM centos
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
7dc0dca2b151: Pull complete
Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322
Status: Downloaded newer image for docker.io/centos:latest
---> 49f7960eb7e4
Step 2/6 : ADD jdk-8u144-linux-x64.tar.gz /usr/local
---> 8c9e14062a24
Removing intermediate container a499940235ac
Step 3/6 : ENV JAVA_HOME /usr/local/jdk1.8.0_144
---> Running in cefedfd97f61
---> 12528cd5a517
Removing intermediate container cefedfd97f61
Step 4/6 : ADD apache-tomcat-8.5.31.tar.gz /usr/local
---> 246fa08bea1c
Removing intermediate container a1aaaa2bf0b8
Step 5/6 : EXPOSE 8080
---> Running in 87c4b41f3c1e
---> fd207f27b830
Removing intermediate container 87c4b41f3c1e
Step 6/6 : ENTRYPOINT /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run
---> Running in 9adaed8e3ab9
---> b6fc6d3925f7
Removing intermediate container 9adaed8e3ab9
Successfully built b6fc6d3925f7
WARNING: Image for service tomcat1 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building tomcat2
Step 1/6 : FROM centos
---> 49f7960eb7e4
Step 2/6 : ADD jdk-8u144-linux-x64.tar.gz /usr/local
---> Using cache
---> 8c9e14062a24
Step 3/6 : ENV JAVA_HOME /usr/local/jdk1.8.0_144
---> Using cache
---> 12528cd5a517
Step 4/6 : ADD apache-tomcat-8.5.31.tar.gz /usr/local
---> Using cache
---> 246fa08bea1c
Step 5/6 : EXPOSE 8080
---> Using cache
---> fd207f27b830
Step 6/6 : ENTRYPOINT /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run
---> Using cache
---> b6fc6d3925f7
Successfully built b6fc6d3925f7
WARNING: Image for service tomcat2 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling nginx (nginx:1.14)...
Trying to pull repository docker.io/library/nginx ...
1.14: Pulling from docker.io/library/nginx
f2aa67a397c4: Already exists
6160d1ac49e9: Pull complete
046b67408776: Pull complete
Digest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6
Status: Downloaded newer image for docker.io/nginx:1.14
Creating java_tomcat2_1 ... done
Creating java_tomcat1_1 ... done
Creating java_nginx_1 ... done
查看启动情况
[root@master java]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------
java_nginx_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
java_tomcat1_1 /usr/local/apache-tomcat-8 ... Up 8080/tcp
java_tomcat2_1 /usr/local/apache-tomcat-8 ... Up 8080/tcp
检测负载均衡
[root@master java]# curl http://localhost
welcome to tomcat-A server
[root@master java]# curl http://localhost
welcome to tomcat-B server
[root@master java]# curl http://localhost
welcome to tomcat-A server
[root@master java]# curl http://localhost
welcome to tomcat-B server
浏览器访问测试负载均衡
查看日志输出信息
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:33 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:34 +0000] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:34 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:39 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:30 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:31 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:32 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:33 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:19:32 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
©著作权归作者所有:来自作者民工哥的原创作品,如需转载,请注明出处,否则将追究法律责任
作者:民工哥
如果本文如对您有帮助,还请多帮 【推荐】 下此文,感谢支持。
如果喜欢我的文章,不妨关注下我的公众号哟
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。