In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
The main purpose of this article is to share how to configure Compose and consul for Docker containers. This paper also introduces the common fields and commands of Docker-Compose, as well as the overview and implementation of consul-template. I hope you can get something through this article.
Docker compose configuration common fields
Docker compose common commands
The compose command describes the basic use of the format docker-compose [options] [COMMAND] [ARGS...] docker-compose option-- verbose outputs more debugging information-- version prints the version and exits-- Fmaitsu file FILE uses a specific compose template file. The default is docker-compose.yml-p,--project-name NAME to specify the project name, and the default is directory name 1. Install docker container and compose orchestration tool [root@localhost ~] # yum install-y\ > yum-utils\ # # set Source tool > device-mapper-persistent-data\ # # Mapping tool > lvm2 # # Mapping tool [root@localhost ~] # yum-config-manager-- add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo## loads Ali Cloud Image Source [root@localhost ~] # Yum install docker-ce-y # # install docker container [root@localhost ~] # systemctl stop firewalld.service # # close firewall [root@localhost ~] # setenforce 0 [root@localhost ~] # systemctl start docker # # Open docker container [root@localhost ~] # systemctl enable docker # # Boot self-start [root@localhost ~] # ps aux | grep docker # # check whether the docker process is enabled [root@localhost ~] # tee / etc/docker/daemon.json "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]>} > EOF {" registry-mirrors ": [" https://3a8s9zx5.mirror.aliyuncs.com"]}[root@localhost ~] # systemctl daemon-reload # # reload daemon [root@localhost ~] # systemctl restart docker # # restart the docker service [root@localhost ~] # mount.cifs / / 192.168.100.3/LNMP-C7 / mnt/ Password for root@//192.168.100.3/LNMP-C7: [root@localhost ~] # cd / mnt/docker/ [root@localhost docker] # cp-p docker-compose / usr/bin/ # # copy docker-compose to / usr/bin/ directory [root@localhost docker] # docker-compose-v # # View version information docker-compose version 1.21.1 Build 5a3f1a32 Create Nginx orchestration instance [root@localhost docker] # cd ~ [root@localhost ~] # mkdir compose-nginx # # create working directory [root@localhost ~] # cd compose-nginx/ [root@localhost compose-nginx] # mkdir nginx # # create Nginx directory [root@localhost compose-nginx] # cd nginx/ [root@localhost nginx] # vim run.sh # # Edit startup script #! / bin/bash/usr/local/nginx/sbin/nginx # # launch Move the Nginx service [root@localhost nginx] # mkdir / abc [root@localhost nginx] # mount.cifs / / 192.168.100.3/LNMP-C7 / abc/Password for root@//192.168.100.3/LNMP-C7: [root@localhost nginx] # cp / abc/nginx-1.12.2.tar.gz. / # # copy the Nginx package to the current directory [root@localhost nginx] # vim Dockerfile # # write the container file FROM Centos:7RUN yum-y updateRUN yum-y install pcre-devel zlib-devel gcc gcc-c++ makeRUN useradd-M-s / sbin/nologin nginxADD nginx-1.12.2.tar.gz / usr/local/srcWORKDIR / usr/local/srcWORKDIR nginx-1.12.2RUN. / configure\-prefix=/usr/local/nginx\-user=nginx\-group=nginx\-with-http_stub_status_module & & make & & make installENV PATH / usr/local/nginx/sbin:$PATHEXPOSE 80EXPOSE 443RUN echo "daemon off "> > / usr/local/nginx/conf/nginx.confADD run.sh / run.shRUN chmod 755 / run.shCMD [" / run.sh "] [root@localhost nginx] # cd.. / [root@localhost compose-nginx] # vim docker-compose.yml # # create compose template script version:'3' # # version services: # # Service nginx: hostname: nginx # # container hostname build: Context:. / nginx # # create the file path of the container dockerfile: Dockerfile ports:-1216 dockerfile 80 # # Mapping port-1217 dockerfile 443 networks:-abc # # Network name volumes: -. / wwwroot:/usr/local/nginx/html # # data volume association Host site directory networks: # # declare network abc: [root@localhost compose-nginx] # docker-compose-f docker-compose.yml up-DVS # execute compose script on -d daemon [root@localhost compose-nginx] # cd wwwroot/ [root@localhost wwwroot] # vim index.htmlthis is testplate editing! # # Editing the homepage content [root@localhost wwwroot] # yum install tree-y [root@localhost wwwroot] # tree ~ # # View the entire required content structure / root ├── anaconda-ks.cfg ├── compose-nginx │ ├── docker-compose.yml # # orchestration template │ ├── nginx │ │ ├── Dockerfile # # Container script │ │ ├── nginx-1.12.2.tar.gz # # installation package │ │ └── run.sh # # start service script │ └── wwwroot │ └── index.html # # website homepage 3 View a web page with a browser
Docker consul CCS Update and Discovery Topology Diagram
Consul Overview consul is an open source tool for implementing service discovery and configuration consul features in distributed systems: (1) consul supports health check and allows storage key-value pairs (2) consistency protocol uses Raft algorithm to ensure high availability of services. (3) member management and message broadcasting use GOSSIP protocol, support ACL access control for easy deployment Work seamlessly with lightweight containers such as docker to build consul services consul and agentconsul agent are deployed and run on every node that provides services. There are two operation modes: server,clientserver and client knowledge consul cluster level distinction, independent architecture topology diagram of application services built on top of cluster
Experimental environment consul server 192.168.13.128 Docker-ce, Compose 3, Consul, Consul-templateclient client node 192.168.13.129 Docker-ce, registrator1 Configure consul server [root@consul ~] # mkdir consul [root@consul ~] # mount.cifs / / 192.168.100.3/LNMP-C7 / mnt/Password for root@//192.168.100.3/LNMP-C7: [root@consul ~] # cd / mnt/docker/ [root@consul docker] # cp consul_0.9.2_linux_amd64.zip / root/consul/ [root@consul docker] # cd / root/consul/ [root@consul consul] # unzip consul_0.9.2_linux_amd64.zip # # decompress consulArchive: consul_0.9.2_linux_amd64.zip inflating: consul [root@consul consul] # mv consul/ usr/bin/ # # easy for the system to identify [root@consul consul] # consul agent\ # Agent >-server\ # # provide server >-bootstrap\ >-ui\ # # web access interface >-data-dir=/var/lib/consul-data\ # # data storage directory >-bind=192.168.13.128\ # # Local address >-client=0.0.0.0\ # # all nodes >-node=consul-server01 & > / var/log/consul.log & # # specify the local node name [root@consul consul] # consul members # # View cluster information Node Address Status Type Build Protocol DCconsul-server01 192.168.13.128 Address Status Type Build Protocol DCconsul-server01 8301 alive server 0.9.2 dc1 [root@consul consul] # consul info | grep leader # # View leader leader = true leader_addr = 192.168.13.128 Address Status Type Build Protocol DCconsul-server01 8300 [root@consul consul] # systemctl stop firewalld.service # # close the firewall [root@consul consul] # setenforce 0 obtained through httpd api Take the cluster information [root@localhost consul] # curl 127.0.0.1:8500/v1/status/peers # # to view all the services registered by Raf leader [root@localhost consul] # curl 127.0.0.1:8500/v1/catalog/services # # in the cluster server member [root@localhost consul] # curl 127.0.0.1:8500/v1/status/leaders # # [root@localhost consul] # curl 127.0.0.1:8500/v1/catalog/nodes # # Cluster node details [root@localhost consul] # curl 127.0.0.1:8500/v1/catalog/nginx # # View nginx service information 2 Configure client node server [root@client ~] # docker run-d\ >-- name=registrator\ # # install registrator auto-registration function >-- net=host\ # # specify network >-v / var/run/docker.sock:/tmp/docker.sock\ # # specify data volume >-- restart=always\ > gliderlabs/registrator:latest\ # # Image >-ip=192.168.13.129\ # # Local address > consul:/ / 192.168.13.128 consul Server address 3 Test whether the function is normal on the node server [root@client ~] # docker run-itd-p 83:80-- name test-01-h test01 nginx [root@client ~] # docker run-itd-p 84:80-- name test-02-h test02 nginx [root@client ~] # docker run-itd-p 88:80-- name test-03-h test03 httpd [root@client ~] # docker run-itd-p 89:80-name test-04-h test04 Httpd [root@client ~] # systemctl stop firewalld.service # # turn off the firewall [root@client ~] # setenforce 04 Verify that http and Nginx services are registered with consul
Consul-template Overview is an application based on Consul to automatically replace configuration files; you can query service directories in Consul: Key, Key-values, etc.; it is especially suitable for dynamically creating configuration files; it is a daemon that is used to query consul cluster information in real time. 1. Configure the template file [root@consul consul] # vim / root/consul/nginx.ctmpl upstream http_backend {# # on the consul server to declare the backend server pool {{range service "nginx"}} server {{.address}}: {{.Port}}; {{end} server {listen 1216; # # proxy port server_name localhost 192.168.13.128 # # Local address access_log / var/log/nginx/kgc.cn-access.log; index index.html index.php; location / {proxy_set_header HOST $host; # # header information proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr Proxy_set_header X-Fprwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend; # # Redirect backend server pool}} 2 Install and configure Nginx [root@consul consul] # cd / mnt/ [root@consul mnt] # tar zxvf nginx-1.12.2.tar.gz-C / opt/ # # extract Nginx [root@consul mnt] # cd / opt/nginx-1.12.2 [root@consul nginx-1.12.2] # yum install gcc gcc-c++ pcre-devel zlib-devel-y # # installation component [root@consul nginx-1.12.2] # on the consul server. / configure-- prefix=/usr/local/nginx # # configure [root@consul nginx-1.12.2] # make & & make install [root@consul nginx-1.12.2] # vim / usr/local/nginx/conf/nginx.conf # # modify the Nginx configuration file 17 http {18 include mime.types 19 include vhost/*.conf # # specify virtual host directory [root@consul nginx-1.12.2] # mkdir / usr/local/nginx/conf/vhost # # create virtual host directory [root@consul nginx-1.12.2] # mkdir / var/log/nginx # # create log file directory [root@consul nginx-1.12.2] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ # # create Soft connection [root@consul nginx-1.12.2] # nginx # # enable the Nginx service [root@consul nginx-1.12.2] # netstat-natp | grep nginx3 Configure and start templeta [root @ consul nginx-1.12.2] # cd / mnt/docker/ [root@consul docker] # cp consul-template_0.19.3_linux_amd64.zip / root/ [root@consul docker] # cd / root/ [root@consul ~] # unzip consul-template_0.19.3_linux_amd64.zip # # install templetaArchive: consul-template_0.19.3_linux_amd64.zip inflating: consul-template on the consul server [root@consul ~] # mv consul-template / usr/bin [root@consul ~] # consul-template-consul-addr 192.168.13.128 root@consul 8500\ # # specify consul local address >-template "/ root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx-s reload"\ # # specify the path to template >-log-level=info # # log level [root @ consul ~] # cd / usr/local/nginx/conf/vhost/ [root@consul vhost] # lskgc.conf # # automatically generate configuration file template upstream http_backend {server 192.168.13.129VLAR 83 # # automatically generate backend CCS address and port number server 192.168.13.129 server 84;} server {listen 1216; server_name localhost 192.168.13.128; access_log / var/log/nginx/kgc.cn-access.log; index index.html index.php; location / {proxy_set_header HOST $host Proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-Fprwarded-For $proxy_add_x_forwarded_for; proxy_pass http://http_backend;}} # # View with a browser
# # you can check the visiting address 1284 on the backend node server through docker logs-f test-01, create a nginx container node on the client side, and detect service discovery and configuration updates (do not turn off the firewall! ) [root@localhost ~] # docker run-itd-name test-05-h test05 nginx## continues browser access to [root@client ~] # docker logs-f test-05192.168.13.128-- [09/Jan/2020:11:05:39 + 0000] # # polling access, or the node accessed by the 128server, realizing load balancing
This is what the Docker container configures Compose and consul. Have you learned anything after reading it? If you want to know more about it, you are welcome to follow the industry information!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.