三劍客簡介
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" "-"
複製程式碼