Docker Compose容器集群怎么快速编排
本篇内容介绍了“Docker Compose容器集群怎么快速编排”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Docker-Compose 容器集群的快速编排
Docker-compose 简介
Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。
Docker-Compose将所管理的容器分为三层,分别是 工程(project),服务(service)以及容器(container)。Docker-Compose运行目录下的所有文件(docker-compose.yml,extends文件或环境变量文件等)组成一个工程,若无特殊指定工程名即为当前目录名。一个工程当中可包含多个服务,每个服务中定义了容器运行的镜像、参数、依赖。一个服务当中可包括多个容器实例,Docker-Compose并没有解决负载均衡的问题,因此需要借助其它工具实现服务发现及负载均衡,比如 Consul。
Docker-Compose的工程配置文件默认为docker-compose.yml,可通过环境变量COMPOSE_FILE或-f参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。
使用一个Dockerfile模板文件,可以让用户很方便的定义一个单独的应用容器。在工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个Web项目,除了Web服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。
Compose允许用户通过一个单独的docker-compose.yml模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。
Docker-Compose项目由Python编写,调用Docker服务提供的API来对容器进行管理。因此,只要所操作的平台支持Docker API, 就可以在其上利用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
YAML 文件格式及编写注意事项
YAML 是一种标记语言,它可以很直观的展示数据序列化格式,可读性高。类似于 XML数据描述语言,语法比 XML 简单的很多。YAML 数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号 [ ] 括起来, hash 用花括号 { } 括起来。
YAML 文件格式及编写注意事项
大小与敏感
通过缩进表示层级关系
不支持制表符tab键缩进,只能使用空格缩进
缩进的空格数目不重要,只要相同层级左对齐,通常开头缩进2个空格
用#号注释
符号字符后缩进1个空格,如冒号:、逗号,、横杠-
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.confnet.ipv4.ip_forward = 1 #末行添加sysctl -p #刷新配置systemctl restart docker #重启docker服务
配置nginx
FROM centos:7MAINTAINER this is nginx images<gb>RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ makeRUN useradd -M -s /sbin/nologin nginxADD 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 installENV PATH $PATH:/usr/local/nginx/sbinADD nginx.conf /usr/local/nginx/conf/RUN chmod 777 -R /usr/local/nginx/html/EXPOSE 80EXPOSE 443CMD ["/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 = 3306socket=/usr/local/mysql/mysql.sock[mysqld]user = mysqlbasedir=/usr/local/mysqldatadir=/usr/local/mysql/dataport = 3306character-set-server=utf8pid-file = /usr/local/mysql/mysqld.pidsocket=/usr/local/mysql/mysql.sockbind-address = 0.0.0.0skip-name-resolvemax_connections=2048default-storage-engine=INNODBmax_allowed_packet=16Mserver-id = 1sql_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:7MAINTAINER this is mysql image <lnmp>RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make;useradd -M -s /sbin/nologin mysqlADD mysql-boost-5.7.20.tar.gz /usr/local/class="lazy" data-src/WORKDIR /usr/local/class="lazy" data-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 installADD my.cnf /etc/my.cnfEXPOSE 3306RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnfWORKDIR /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 mysqldENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATHVOLUME [ "/usr/local/mysql" ]CMD ["/usr/sbin/init"]
配置php
编写Dockerfile文件
FROM centos:7MAINTAINER 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 nginxADD php-7.1.10.tar.bz2 /usr/local/class="lazy" data-src/WORKDIR /usr/local/class="lazy" data-src/php-7.1.10RUN ./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 installENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATHADD 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.defaultADD www.conf /usr/local/php/etc/php-fpm.d/EXPOSE 9000CMD /usr/local/php/sbin/php-fpm -F
将所需的三个配置文件修改后写入
vim php-fpm.conf[global]pid = run/php-fpm.pidinclude=/usr/local/php/etc/php-fpm.d/*.confvim php.ini[PHP]engine = Onshort_open_tag = Offprecision = 14output_buffering = 4096zlib.output_compression = Offimplicit_flush = Offunserialize_callback_func =serialize_precision = -1disable_functions =disable_classes =zend.enable_gc = Onexpose_php = Onmax_execution_time = 30max_input_time = 60memory_limit = 128Merror_reporting = E_ALLdisplay_errors = Ondisplay_startup_errors = Onlog_errors = Onlog_errors_max_len = 1024ignore_repeated_errors = Offignore_repeated_source = Offreport_memleaks = Ontrack_errors = Onhtml_errors = Onvariables_order = "GPCS"request_order = "GP"register_argc_argv = Offauto_globals_jit = Onpost_max_size = 8Mauto_prepend_file =auto_append_file =default_mimetype = "text/html"default_charset = "UTF-8"doc_root =user_dir =enable_dl = Offfile_uploads = Onupload_max_filesize = 2Mmax_file_uploads = 20allow_url_fopen = Onallow_url_include = Offdefault_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 = 2000pdo_mysql.default_socket=[Phar][mail function]SMTP = localhostsmtp_port = 25mail.add_x_header = On[SQL]sql.safe_mode = Off[ODBC]odbc.allow_persistent = Onodbc.check_persistent = Onodbc.max_persistent = -1odbc.max_links = -1odbc.defaultlrl = 4096odbc.defaultbinmode = 1[Interbase]ibase.allow_persistent = 1ibase.max_persistent = -1ibase.max_links = -1ibase.timestampformat = "%Y-%m-%d %H:%M:%S"ibase.dateformat = "%Y-%m-%d"ibase.timeformat = "%H:%M:%S"[MySQLi]mysqli.max_persistent = -1mysqli.allow_persistent = Onmysqli.max_links = -1mysqli.cache_size = 2000mysqli.default_port = 3306mysqli.default_socket = /usr/local/mysql/mysql.sockmysqli.default_host =mysqli.default_user =mysqli.default_pw =mysqli.reconnect = Off[mysqlnd]mysqlnd.collect_statistics = Onmysqlnd.collect_memory_statistics = On[OCI8][PostgreSQL]pgsql.allow_persistent = Onpgsql.auto_reset_persistent = Offpgsql.max_persistent = -1pgsql.max_links = -1pgsql.ignore_notice = 0pgsql.log_notice = 0[bcmath]bcmath.scale = 0[browscap][Session]session.save_handler = filessession.use_strict_mode = 0session.use_cookies = 1session.use_only_cookies = 1session.name = PHPSESSIDsession.auto_start = 0session.cookie_lifetime = 0session.cookie_path = /session.cookie_domain =session.cookie_httponly =session.serialize_handler = phpsession.gc_probability = 1session.gc_divisor = 1000session.gc_maxlifetime = 1440session.referer_check =session.cache_limiter = nocachesession.cache_expire = 180session.use_trans_sid = 0session.sid_length = 26session.trans_sid_tags = "a=href,area=href,frame=class="lazy" data-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=1soap.wsdl_cache_dir="/tmp"soap.wsdl_cache_ttl=86400soap.wsdl_cache_limit = 5[sysvshm][ldap]ldap.max_links = -1[mcrypt][dba][opcache][curl][openssl]vim php-fpm.conf[global]pid = run/php-fpm.pidinclude=/usr/local/php/etc/php-fpm.d/*.conf折叠 vim www.conf[www]user = nginxgroup = nginxlisten = 172.18.0.30:9000pm = dynamicpm.max_children = 5pm.start_servers = 2pm.min_spare_servers = 1pm.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 - mysqlnetworks: lnmp: driver: bridge ipam: config: - subnet: 172.18.0.0/16折叠 #启动容器docker-compose -f docker-compose.yml up -ddocker ps -a
“Docker Compose容器集群怎么快速编排”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341