Docker Compose容器編排

Compose是Docker官方的開源項目,能夠實現對Docker容器集羣的快速編排。
Compose 中有兩個重要的概念:
服務(service):一個應用的容器,實際上能夠包括若干運行相同鏡像的容器實例。
項目(project):由一組關聯的應用容器組成的一個完整業務單元,在 docker-compose.yml 文件中定義。javascript

1、安裝Composephp

Compose由python開發,所以可使用pip方式進行安裝。css

# pip install -U docker-compose

安裝成功後能夠查看docker-compose的用法:html

# 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補全命令:java

# curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

其它安裝方式:二進制包、容器中運行。python

2、Compose命令說明linux

格式:
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 打印版本並退出
經常使用命令使用說明:nginx

3、Compose模板文件
默認的模板文件名稱爲 docker-compose.yml,格式爲 YAML 格式。
版本1中,其中每一個頂級元素爲服務名稱,次級元素爲服務容器的配置信息,例如c++

webapp:
  image: examples/web
  ports:
    - "80:80"
  volumes:
    - "/data"

版本2擴展了 Compose 的語法,同時儘可能保持跟版本1的兼容,除了能夠聲明網絡和存儲信息外,最大的不一樣一是添加了版本信息,另外一個是須要將全部的服務放到 services 根下面。版本2寫法以下:git

version: "2"
services:
  webapp:
    image: examples/web
    ports:
      - "80:80"
    volumes:
      - "/data"

每一個服務都必須經過 image 指令指定鏡像或 build 指令(須要 Dockerfile)等來自動構建生成鏡像。
經常使用指定以下:

4、使用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 eivll0m@163.com

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 eivll0m@163.com

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保持。

相關文章
相關標籤/搜索