In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
I. Docker Compose container choreography
1. Docker Compose, formerly known as Fig, is a tool for defining and running multiple Docker containers
2. It is no longer necessary to use shell script to start the container when using Docker Compose
3. Docker Compose is very suitable for scenarios where multiple containers are combined for development.
4. File structure of Docker Compose:
-vim docker-compose.yml
YAML is an intuitive data serialization format in markup language.
The following is the file format and preparation considerations:
2. Docker Compose configuration:
(1) commonly used fields:
(2) Common commands:
(3) Compose command description:
Second, an example of arrangement:
(1) optimize the network first:
Vim / etc/sysctl.confnet.ipv4.ip_forward=1 # # add sysctl-p # # modify effective systemctl restart network # # restart the network service at the end of the file
(2) copy the downloaded docker-compose to the / usr/bin/ directory:
Cp-p docker-compose / usr/bin/
(3) create a working directory (copy the nginx package to the nginx directory):
[root@localhost ~] # mkdir compose_ nginx [root @ localhost] # cd compose_ nginx [root @ localhost compose_nginx] # mkdir nginx [root@localhost compose_nginx] # cd nginx [root@localhost nginx] # vim DockerfileFROM 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.0.tar.gz / usr/local/srcWORKDIR / usr/local/srcWORKDIR nginx-1.12.0RUN. / 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] # vim run.shrun.shqingray
(4) create and edit yml files:
Vim / root/compose_nginx/docker-compose.ymlversion: '3'services: nginx: hostname: nginx build: context:. / nginx dockerfile: Dockerfile ports:-1216 wwwroot:/usr/local/nginx/htmlnetworks 80-1217 networks:-abc volumes: -. / wwwroot:/usr/local/nginx/htmlnetworks: abc:
(5) at this point, you can use the tree command to check the file structure in the root directory:
[root@localhost compose_nginx] # tree. / # # View the tree view. / ├── docker-compose.yml # # create template script ├── nginx │ ├── Dockerfile # # create container script │ ├── nginx-1.12.0.tar.gz # # Source code package │ └── run.sh # # Service script └── wwwroot # # site
(6) perform the opening:
Docker-compose-f docker-compose.yml up-d
Check whether the image and container are normal and created successfully:
(7) We can create a web page under the site directory and use the browser to see if it can be displayed properly:
[root@localhost compose_nginx] # cd wwwroot/ [root@localhost wwwroot] # vim index.htmlthis is nginx new web
Browser access (IP address is local): http://192.168.220.131:1216/
3. Update and discovery of Docker concul CCS:
(1) Consul:
1. Consul is an open source tool developed by HashCorp, which is used to realize service discovery and configuration in distributed systems.
2. Characteristics of Consul:
Consul supports health check, allowing you to store key-value pairs
The consistency protocol uses the Raft algorithm to ensure the high availability of the service.
Member management and message broadcasting adopt GOSSIP protocol, and support ACL access control.
3. It is easy to deploy and can work seamlessly with lightweight containers such as Docker.
(2) establish Consul service:
1. Agent of consul needs to be deployed and run on each node that improves the service.
2. Two operation modes of Consul agent:
Server
Client
3. Server and client are only the distinction at the cluster level of consul, and have nothing to do with the application services built on cluster.
4. Set up a consul cluster:
Environment preparation: the first server downloads a nginx image for use in the experiment:
[root@localhost vhost] # docker pull nginx / / download a nginx image [root@localhost vhost] # docker create-it nginx:latest / bin/bash / / create a container be7904151f5d6cb110aba1aaa637dffeb8100c4d8761a1492e4b008dcd57d313 [root@localhost vhost] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESbe7904151f5d Nginx:latest "/ bin/bash" 3 seconds ago Created xenodochial_black
Architectural mind map:
Deployment process:
(1) configure consul server:
[root@localhost ~] # mkdir consul [root@localhost abc1] # cp consul_0.9.2_linux_amd64.zip / root/consul [root@localhost abc1] # cd / root/consul [root@localhost consul] # unzip consul_0.9.2_linux_amd64.zip # # decompress [root@localhost consul] # mv consul / usr/bin/ # # to facilitate system identification and establishment of Consul service: consul agent\-server\-bootstrap\ -ui\-data-dir=/var/lib/consul-data\-bind=192.168.220.131\-client=0.0.0.0\-node=consul-server01 & > / var/log/consul.log &
(2) View the cluster information:
[root@localhost consul] # consul members [root@localhost consul] # consul info | grep leader
(3) Cluster information can be obtained through httpd api:
[root@localhost consul] # curl 127.0.0.1:8500/v1/status/peers # # View all services registered by cluster server members [root@localhost consul] # curl 127.0.0.1:8500/v1/status/leaders # # Raf leader [root@localhost consul] # curl 127.0.0.1:8500/v1/catalog/services # # [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
(4) allow CCS to join the nginx cluster automatically:
Configure 192.168.220.140 node: docker run-d\-- name=registrator\-- net=host\-v / var/run/docker.sock:/tmp/docker.sock\-- restart=always\ gliderlabs/registrator:latest\-ip=192.168.220.140\ consul://192.168.220.131:8500
(5) whether the service and function are normal:
Create two containers, test-01 and test02, and specify port numbers 83 and 84: [root @ localhost ~] # docker run-itd-p test02 nginx 83docker run 80-- name test-01-h test01 nginx [root@localhost ~] # docker run-itd-p localhost 84localhost 80-- name test-02-h test02 nginx
(6) verify whether both http and nginx services are registered with consul:
Browser visit: 192.168.220.131purl 8500
Click "NODES"-> "consurl-server01", and the two services you just created will appear.
(7) automatically join the CCS in the Nginx cluster:
1 、 consul-template:
It is the application of automatic replacement profile based on Consul.
You can query the service catalogs in Consul: Key, Key-values, etc.
Especially suitable for dynamic creation of configuration files
Is a daemon for querying consul cluster information in real time
2. Prepare the template nginx template file:
/ / create a template file on the consul server: vim / root/consul/nginx.ctmplupstream http_backend {{range service "nginx"}} server {{.Address}: {{.Port}}; {{end} server {listen 1216; server_name localhost 192.168.220.131; 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;}}
3. Compile and install a nginx service:
Yum install gcc gcc-c++ pcre pcre-devel zlib-devel-ytar zxvf nginx-1.12.0.tar.gz-C / opt/./configure-- prefix=/usr/local/nginxmake & & make installln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/
4. Configure nginx:
[root@localhost nginx-1.12.0] # vim / usr/local/nginx/conf/nginx.conf add virtual host directory in http template: http {include mime.types; include vhost/*.conf; # # add virtual host directory default_type application/octet-stream;}
/ / create virtual host directory: [root@localhost nginx-1.12.0] # mkdir / usr/local/nginx/conf/vhost// create log file directory: [root@localhost nginx-1.12.0] # mkdir / var/log/nginx// launch nginx [root@localhost nginx-1.12.0] # / usr/local/nginx/sbin/nginx [root@localhost nginx-1.12.0] # netstat-natp | grep nginxtcp 0 0 0.0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0
(8) configure and start template:
1. Decompress and copy to the / bin directory for direct use: [root@localhost abc] # unzip consul-template_0.19.3_linux_amd64.zip [root@localhost abc] # mv consul-template / usr/bin/
2. Start:
Consul-template-consul-addr 192.168.220.131 template-template "/ root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx-s reload"-- log-level=info
At this point, we can open another terminal and view the configuration file generated from the template:
Cat / usr/local/nginx/conf/vhost/kgc.conf
(9) to test the effect of automatic updates, we can create a nginx container node on the registrator server to detect service discovery and configure update features:
[root@localhost] # docker run-itd-pRV 85 test05 nginxbdc51a5c59e68c032e7466494fcb0212bae48fe939325845e00abb4840d0b48e 80-- name test-05-h test05 nginxbdc51a5c59e68c032e7466494fcb0212bae48fe939325845e00abb4840d0b48e
At this point, you will be prompted to update automatically in consul server monitoring to view the configuration file:
(10) to show that polling processes requests, you can use the logs command to view the logs of the three nginx containers, and the visits from the same IP address will be displayed:
Refresh and visit the home page of nginx several times: 192.168.220.131purl 1216
[root@localhost] # docker logs-f test-01192.168.220.131-- [03/Jan/2020:15:01:57 + 0000] "GET / HTTP/1.0" 200612 "-" Mozilla/5.0 (X11; Linux x861664) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:17 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:20 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"[root@localhost ~] # docker logs-f test-02192.168.220.131-- [03/Jan/2020:15:02:18 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:21 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:24 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:28 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"[root@localhost ~] # docker logs-f test-05192.168.220.131-- [03/Jan/2020:15:01:57 + 0000]" GET / favicon.ico HTTP/1.0 "404153"-"" Mozilla/5.0 (X11; Linux x86 * 64 Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:19 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:22 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x86 * 64) Rv:52.0) Gecko/20100101 Firefox/52.0 ""-"192.168.220.131-- [03/Jan/2020:15:02:26 + 0000]" GET / HTTP/1.0 "304 0"-"" Mozilla/5.0 (X11; Linux x8634; rv:52.0) Gecko/20100101 Firefox/52.0 ""-"
Yes, it can be seen that all the visits from 192.168.220.131 are sent to the backend docker for processing by polling, and the load balance is achieved.
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.