您好,登錄后才能下訂單哦!
本篇內容介紹了“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來進行編排管理。
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 是一種標記語言,它可以很直觀的展示數據序列化格式,可讀性高。類似于 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 |
字段 | 描述 |
---|---|
build | 重新構建服務 |
ps | 列出容器 |
up | 創建和啟動容器 |
exec | 在容器里面執行命令 |
scale | 指定一個服務容器啟動數量 |
top | 顯示正在運行的容器進程 |
logs | 查看服務容器的輸出 |
down | 刪除容器、網絡、數據卷和鏡像 |
stop/start/restart | 停止/啟動/重啟服務 |
vim /etc/sysctl.conf net.ipv4.ip_forward = 1 #末行添加 sysctl -p #刷新配置 systemctl restart docker #重啟docker服務
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; # } #} } 折疊
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
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"]
編寫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容器集群怎么快速編排”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。