其他分享
首页 > 其他分享> > Docker-Compose 容器集群的快速编排

Docker-Compose 容器集群的快速编排

作者:互联网

Docker-Compose 容器集群的快速编排

目录

Docker-compose 简介

compose 部署

Docker Compose 环境安装

Docker Compose 是 Docker 的独立产品,因此需要安装 Docker 之后在单独安装 Docker Compose

#下载
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose 

#或者已下载过直接从宿主机传入到/opt目录下 
mv docker-compose /usr/local/bin/ 
#安装 
chmod +x /usr/local/bin/docker-compose 
#查看版本
docker-compose --version 

img

YAML 文件格式及编写注意事项

Docker Compose配置常用字段

字段 描述
build 指定Dockerfile文件名(要指定的Dockerfile文件需要在build标签的子级标签中用dockerfile标签指定)
dockerfile 构建镜像上下文路径
context 可以是dockerfile路径,或者是执行git 仓库的url地址
image 指定镜像(已存在)
command 执行命令,会覆盖容器启动后默认执行的命令(会覆盖Dockerfile的CMD指令)
container_name 指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法scale
deploy 指定部署和运行服务相关配置,只能在Swarm模式使用
enviroment 添加环境变量
networks 加入网络,引用顶级networks下条目
networks-mode 设置容器的网络模式,host、bridge.....
ports 暴露容器端口,与-p 相同,但是端口不能低于60
volumes 挂载一个宿主机目录或命令卷到容器,命名卷要在顶级volumes 定义卷名称
volumes_from 从另一个服务或容器挂载卷,可选参数 :ro 和 :rw,仅版本'2'支持
hostname 容器主机名
sysctls 在容器内设置内核参数
links 连接到另一个容器,- 服务名称[ :服务别名 ]
privilcgcd 用来给容器root权限,注意是不安全的,truc|false(开启|关闭)
restart 重启策略,定义是否重启容器; no(默认,不重启), always(总是重启), on-failure,(退出状态非0时重启), on-failure:3 ,在容器非正常退出时重启容器,最多重启3次, unless-stoped 在容器退出时总是重启容器,但是不考虑在Docker 守护进程启动时就已经停止了的容器
depends_on 此标签用于解决容器的依赖,启动先后问题。如启动应用容器,需要先启动数据库容器php: depends_on: - apache - mysql

Docker compose 常用命令

字段 描述
build 重新构建服务
ps 列出容器
up 创建和启动容器
exec 在容器里面执行命令
scale 指定一个服务容器启动数量
top 显示正在运行的容器进程
logs 查看服务容器的输出
down 删除容器、网络、数据卷和镜像
stop/start/restart 停止/启动/重启服务

编写Docker Compose搭建lnmp论坛

部署前准备

vim /etc/sysctl.conf
net.ipv4.ip_forward = 1           #末行添加
sysctl -p                         #刷新配置
systemctl restart docker          #重启docker服务

img

配置nginx

编写dockerfile文件
FROM centos:7
MAINTAINER this is nginx images<gb>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /opt/
WORKDIR /opt/nginx-1.12.0/
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j4 &&  make install
ENV PATH $PATH:/usr/local/nginx/sbin
ADD nginx.conf /usr/local/nginx/conf/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]

准备配置好nginx配置文件(添加支持php解析配置)

#user  nobody;
worker_processes  1;

#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;

server {
    listen       80;
    server_name  localhost;

    charset utf-8;

    #access_log  logs/host.access.log  main;
    
        location / {
            root   html;
            index  index.html index.php;
        }

        #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   172.18.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

    
折叠 

配置mysql

准备配置文件
vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
编写dockerfile文件
FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make;useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1;make -j 4;make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]

配置php

编写Dockerfile文件

FROM centos:7
MAINTAINER this is php image <lnmp>
RUN yum install -y gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip ; make -j 4 ; make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
RUN rm -rf /usr/local/php/etc/php-fpm.d/www.conf.default
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
CMD /usr/local/php/sbin/php-fpm -F

将所需的三个配置文件修改后写入

vim php-fpm.conf
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
vim php.ini
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]

vim php-fpm.conf
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
折叠 
vim www.conf
[www]
user = nginx
group = nginx
listen = 172.18.0.30:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

编写docker-compose.yml文件

version: '2'
services:
  nginx:
    container_name: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
    - 80:80
    networks:
      lnmp:
        ipv4_address: 172.18.0.10

  mysql:
    container_name: mysql
    privileged: true
    build:
      context: ./mysql
      dockerfile: Dockerfile
    ports:

   - 3306:3306
     networks:
          lnmp:
     ipv4_address: 172.18.0.20
       php:
         depends_on:
     - nginx
     - mysql
       build:
       context: ./php
       dockerfile: Dockerfile
       container_name: php
       ports:
     - 9000:9000
       networks:
       lnmp:
       ipv4_address: 172.18.0.30
         volumes_from:
     - nginx
     - mysql
networks:
  lnmp:
    driver: bridge
    ipam:
      config:
      - subnet: 172.18.0.0/16
折叠 
#启动容器

docker-compose -f docker-compose.yml up -d
docker ps -a

标签:容器,Compose,编排,usr,mysql,Docker,php,local
来源: https://www.cnblogs.com/DavinWw/p/16472433.html