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

Bertha ·
更新时间:2024-11-13
· 1821 次阅读

目录

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

Docker-compose 简介

compose 部署

Docker Compose 环境安装

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

Docker compose 常用命令字段

编写Docker Compose搭建lnmp论坛

配置mysql

配置php

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.conf net.ipv4.ip_forward = 1 #末行添加 sysctl -p #刷新配置 systemctl restart docker #重启docker服务

配置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

到此这篇关于Docker-Compose 容器集群的快速编排的文章就介绍到这了,更多相关Docker Compose 容器集群内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



集群 方法 docker-compose 容器 Docker

需要 登录 后方可回复, 如果你还没有账号请 注册新账号