Docker Compose容器編排
Compose是Docker官方的開源項目,可以實現對Docker容器集群的快速編排。
Compose 中有兩個重要的概念:
服務(service):一個應用的容器,實際上可以包括若幹運行相同鏡像的容器實例。
項目(project):由一組關聯的應用容器組成的一個完整業務單元,在 docker-compose.yml 文件中定義。
一、安裝Compose
Compose由python編寫,因此可以使用pip方式進行安裝。
# pip install -U docker-compose
安裝成功後可以查看docker-compose的用法:
# docker-compose -h 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 -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 (for example if your docker host is an IP address) Commands: build Build or rebuild services 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 kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pulls 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 unpause Unpause services up Create and start containers version Show the Docker-Compose version information
添加bash補全命令:
# curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
其它安裝方式:二進制包、容器中運行。
二、Compose命令說明
格式:
docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
選項:
-f, --file FILE 指定使用的 Compose 模板文件,默認為 docker-compose.yml,可以多次指定。
-p, --project-name NAME 指定項目名稱,默認將使用所在目錄名稱作為項目名。
--x-networking 使用 Docker 的可拔插網絡後端特性(需要 Docker 1.9 及以後版本)。
--x-network-driver DRIVER 指定網絡後端的驅動,默認為 bridge(需要 Docker 1.9 及以後版本)。
--verbose 輸出更多調試信息。
-v, --version 打印版本並退出
常用命令使用說明:
三、Compose模板文件
默認的模板文件名稱為 docker-compose.yml,格式為 YAML 格式。
版本1中,其中每個頂級元素為服務名稱,次級元素為服務容器的配置信息,例如
webapp: image: examples/web ports: - "80:80" volumes: - "/data"
版本2擴展了 Compose 的語法,同時盡量保持跟版本1的兼容,除了可以聲明網絡和存儲信息外,最大的不同一是添加了版本信息,另一個是需要將所有的服務放到 services 根下面。版本2寫法如下:
version: "2" services: webapp: image: examples/web ports: - "80:80" volumes: - "/data"
每個服務都必須通過 image 指令指定鏡像或 build 指令(需要 Dockerfile)等來自動構建生成鏡像。
常用指定如下:
四、使用docker-compose編排nginx、tomcat集群
創建一個名為compose-tomcat-nginx的目錄,作為項目工作目錄,並在其中創建兩個子目錄:nginx、tomcat。目錄結構如下:
# tree . ├── docker-compose.yml ├── nginx │ ├── Dockerfile │ ├── nginx-1.8.0.tar.gz │ ├── nginx.conf │ └── nginx-sticky.tar.gz └── tomcat ├── apache-tomcat-7.0.56.tar.gz ├── Dockerfile └── jdk-8u73-linux-x64.tar.gz 2 directories, 8 files
docker-compose.yml文件內容如下:
# cat docker-compose.yml tomcat1: build: ./tomcat expose: - 8080 tomcat2: build: ./tomcat expose: - 8080 nginx: build: ./nginx links: - tomcat1:t01 - tomcat2:t02 ports: - "80:80" expose: - "80"
tomcat的Dockerfile文件內容如下:
# cat Dockerfile FROM centos:6.9 MAINTAINER [email protected] ADD jdk-8u73-linux-x64.tar.gz /app ENV JAVA_HOME /app/jdk1.8.0_73 ADD apache-tomcat-7.0.56.tar.gz /app WORKDIR /app/apache-tomcat-7.0.56 ENTRYPOINT ["bin/catalina.sh","run"] EXPOSE 8080
nginx的Dockerfile文件內容如下:
FROM centos:6.9 MAINTAINER [email protected] ADD nginx-1.8.0.tar.gz /app ADD nginx-sticky.tar.gz /app RUN yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel RUN cd /app/nginx-1.8.0 && ./configure --prefix=/app/nginx --add-module=/app/nginx-sticky && make && make install COPY nginx.conf /app/nginx/conf cmd ["/app/nginx/sbin/nginx", "-g", "daemon off;"] EXPOSE 80
nginx.conf文件內容如下(未優化):
# cat nginx.conf user root root; worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; gzip_comp_level 6; gzip_proxied any; gzip_buffers 4 8k; gzip_min_length 1024; gzip_types text/plain text/xml text/css application/x-javascript text/javascript image/jpeg; proxy_buffer_size 64k; proxy_buffers 32 32k; proxy_ignore_client_abort on; client_header_buffer_size 4k; client_max_body_size 50m; #send_timeout 5m; upstream tomcat_client { sticky; server t01:8080 weight=1; server t02:8080 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://tomcat_client; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } #location ~ ^/console/ { # proxy_pass http://mh.lkpower.com; # ssi on; # proxy_redirect off; #} #location ~ ^/hospital/ { # proxy_pass http://mh.lkpower.com; # ssi on; # proxy_redirect off; #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache‘s document root # concurs with nginx‘s one # } }
運行docker-compose:
# docker-compose up #前臺 # docker-compose up -d #後臺
其它相關命令:
# docker-compose ps # docker-compose build --no-cache --force-rm # docker-compose rm --all
# docker-compose scale tomcat1=5
彈性伸縮:
本集群實現了nginx代理後端tomcat,並實現了session保持。
Docker Compose容器編排