Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker Advanced Management (example demonstration!!)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Docker network communication Docker stand-alone network topology diagram:

Port mapping: the port mapping mechanism provides the services in the container to external network access, which can randomly or specify the mapping port range docker run-d-P httpd:centosdocker run-d-p 49888httpd:centos 80 httpd:centos containers interconnect to establish a network communication tunnel between the source container and the receiving container, use the docker run command-link option to achieve interconnection communication between containers to achieve container interconnection docker run-d-P-- name web1 httpd: Centosdocker run-d-P-- name web2-- link web1:web1httpd:centosdocker exec-it web2 / bin/bashping web1Docker Compose Container choreography Docker Compose is the predecessor of Fig It is a tool to define and run multiple Docker containers using Docker Compose no longer need to use the shell script to start the container Docker Compose is very suitable for scenarios where multiple containers are combined for development Docker Compose environment installation [root@localhost ~] # curl-L https://github.com/docker/compose/releases/download/1.21.1 / docker-compose- `uname-s`-`uname-m`-o / usr/local/bin/docker-compose [root@localhost ~] # chmod + x / usr/local/bin/docker-compose [root@localhost ~] # docker-compose-vDocker Compose file structure [root@localhost compose_ Inmp] # vim docker-compose.ymlYAML is a markup language very intuitive data serialization format file format and preparation considerations 1. Tab tab key indentation is not supported, you need to use space indentation 2. 5. It usually begins with 2 spaces indented by 3. Indent 1 space after the character, such as colon, comma, horizontal bar 4. Note 5 with a pound sign. Enclose 6. 6 in single quotation marks if special characters are included. Boolean values must be enclosed in quotation marks Docker Compse configuration common fields: field description build dockerfile context specify Dockerfile file name build image context path image specify image command execute command, override the default command container name specify container name, because the container name is unique, if you specify a custom name, you cannot scaledeploy to specify deployment and running service related configuration You can only use environment to add the environment variable networks to the network ports in Swarm mode to expose the container port, which is the same as-p, but the port cannot be lower than the 60volumes mount host path or the command volume restart restart policy. The default is no,always,no-failure. Unless-stopedhostname container hostname Docker Compse common commands: field description build rebuild service ps list container up create and start container exec execute command scale specify a service container startup quantity top display container process logs view container output down delete container / network / data volume and mirror stop/start/restart stop / start / restart service Compose command explains the basic use format docker- Compose [options] [COMMAND] [ARGS...] docker-compose option-- verbose outputs more debugging information-- version prints the version and exits-f -- file FILE uses specific compose template files Default is docker-compose.yml-p,-- project-name NAME specifies the project name, and default uses the directory name Docker consul Container Service to update and discover the CCS update and discovery topology diagram

ConsulConsul is an open source tool developed by HashiCorp, which is used to implement service discovery and configuration in distributed systems. 1.Consul supports health check and allows storage key value pairs of 2. 5%. 1.Consul supports health check. The consistency protocol uses the Raft algorithm to ensure the high availability of the service. Member management and message broadcasting use GOSSIP protocol, support ACL access control for easy deployment, and work seamlessly with lightweight containers such as Docker to build an automatically discovered Docker service architecture.

Establish a Consul service

1. Agent2.Consul agent that deploys and runs Consul on every node that provides services has two operating modes: ServerClient3.Server and Client, which are just the distinction between Consul cluster level, and have nothing to do with the application services built on Cluster. Consul commonly used commands-v data volumes-d are placed in the background to execute-I standard input-t assign the role of pseudo terminal Consul 1. When doing the layout, you need to modify the configuration file, so that multiple configuration files can be modified at the same time. Extensibility files are placed in vhost The way of couple Install Docker-ce engine # install dependency package yum install yum-utils device-mapper-persistent-data lvm2-y # set Ali Cloud Image Source yum-config-manager-- add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo# install Docker-ceyum install-y docker-ce# turn off firewall and enhanced security features systemctl stop firewalld.servicesetenforce launch Docker and set it to boot self-startup systemctl start docker.servicesystemctl Enable docker.service# checks whether related processes are enabled ps aux | grep docker# reloads daemon systemctl daemon-reload# restart service systemctl restart docker II. Deploy docker-compose# download and install composecurl-L https://github.com/docker/compose/releases/download/1.25.1-rc1/docker-compose-`uname-s`-`uname-m`-o / usr/local/bin/docker-compose# easy to call chmod + x / usr/local/bin/docker-compose# to establish compose site mkdir / root/docker_ compose3, deploy nginx+Tomcat# to establish compose file vim / root/docker-compose/docker-compose.yml#compose version number There are 1, 2, 3version: '3.3 domains # service name services: nginx:# hostname hostname: nginx# the directory needed to establish the image, Dockerfile script name build: context:. / nginx dockerfile: Dockerfile# mapping host port ports:-1216 Dockerfile# 80-1200 nginx dockerfile working group: same cluster one name networks:-test# directory for establishing dataset volumes: -. / wwwroot:/usr/local/nginx/html tomcat: hostname: tomcat build: context:. / tomcat Dockerfile: Dockerfile ports:-888 networks:-testnetworks: test:# needs to create a new image service needs to be placed in the compose work site [root@localhost docker_compose] # lsdocker-compose.yml nginx tomcat# creation container "- f" specifies the compose file docker-compose-f docker-compose.yml up-d 4. Docker-consul+template

Experimental environment

Name role IP address required environment package centos7- 1master192.168.142.128Docker-ce, Compose, Consul, Consul-templatecentos7-2slave1192.168.142.129Docker-ce, registratorcentos7-minslave2192.168.142.130Docker-ce, registratormaster end operation # install consultant [root @ localhost ~] # mkdir consul [root@localhost ~] # cd consul/ [root@localhost consul] # unzip consul_0.9.2_linux_ amd64.zip [root @ localhost consul] # lsconsul consul_0.9.2_linux_amd64.zip

# move files for easy identification

[root@localhost consul] # mv consul / usr/bin/

# set the agent and start it in the background

[root@localhost consul] # consul agent\

-server\

-bootstrap\

-ui\

-data-dir=/var/lib/consul-data\

-bind=192.168.142.128\

-client=0.0.0.0\

-node=consul-server01 & > / var/log/consul.log &

# View background processes

[root@localhost consul] # jobs

[1] + consul agent-server-bootstrap-ui-data-dir=/var/lib/consul-data-bind=192.168.142.128-client=0.0.0.0-node=consul-server01 & > / var/log/consul.log &

"agent" uses agent proxy function

"- server" provides server function

"- bootstrap" participates in the election of leader

"- ui" provides user interface

Storage location of the "- data-dir" parameter

"- bind" binding address

"- node" defines the node name

-check cluster status``` shell# View cluster information [root@localhost consul] # consul membersNode Address Status Type Build Protocol DCconsul-server01 192.168.142.128consul membersNode Address Status Type Build Protocol DCconsul-server01 alive server 0.9.2 2 dc1# filter leader information [root@localhost consul] # consul info | grep leader leader = true leader_addr = 192.168.142.128Viru 8300 obtain cluster information curl through httpd api 127.0.0.1:8500/v1/status/peers # view all services registered by cluster server member curl 127.0.0.1:8500/v1/status/leader # cluster Raf leadercurl 127.0.0.1:8500/v1/catalog/services #. Curl 127.0.0.1:8500/v1/catalog/nginx # View nginx service information curl 127.0.0.1:8500/v1/catalog/nodes # Cluster node details enable containers in the node server to automatically join # install registrator (registrar) # this tool can check the running status of the container and register automatically You can also log out of the service of the docker container [root@localhost ~] # docker run-d\-- name=registrator\-- net=host\-v / var/run/docker.sock:/tmp/docker.sock\-- restart=always\ gliderlabs/registrator:latest\-ip=192.168.142.129\ consul:#192.168.142.128:8500 to test whether the function is normal [root@localhost ~] # docker run-itd-pRV 83Ranger 80-- name test-01-h test01 nginx [root@localhost ~] # docker run-itd-proot@localhost 84V80-- name test-02-h test02 nginx [root@localhost ~] # docker run-itd-pRV 85V80-- name test-03-h test03 httpd [root@localhost ~] # docker run-itd-pRV 86V80-- name test-04-h test04 httpd verify whether the cluster has been established successfully and enter http:#192.168.142.128:8500 in the browser Click "NODES", and then click "consurl-seerver01" to see five services (graphical interface)

Fifth, establish template to act as an agent

Compile and install nginx1.12.0 manually

(the previous blog has detailed)

Installing consul-templatetemplate is a daemon, which is used to query consul cluster information in real time and to generate configuration files using templates in real time. [root@localhost ~] # unzip consul-template_0.19.3_linux_amd64.zip# enable template command [root@localhost ~] # mv consul-template / usr/bin/ write nginx configuration file template [root@localhost consul] # vim nginx.ctmpl # nginx configuration file template upstream http_backend {# server pool name {{range service "nginx"}} server {{.Address}: {{.Port}} # call variable: server node address and port {{end}

Server {

Listen 110

Server_name localhost 192.168.142.128; # Masterside address

Access_log / var/log/nginx/kgc.cn-access.log

Index index.html index.php

Location / {# request header information setting

Proxy_set_header HOST $host

Proxy_set_header X-Real-IP $remote_addr; # backend server IP

IP of proxy_set_header Client-IP $remote_addr; # client

Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # forward jump address

Proxy_pass http:#http_backend; # request to jump to the http_backend server pool

}

}

-modify configuration file ```shell# change nginx configuration file [root@localhost nginx-1.12.0] # vim / usr/local/nginx/conf/nginx.conf# insert the following content include vhost/\ * .conf under line 18 # add include to facilitate nginx to identify the configuration file generated by the template # there is no such file at this time Need to create [root@localhost nginx-1.12.0] # cd / usr/local/nginx/conf/ [root@localhost conf] # mkdir vhost # create virtual machine directory # enable template template (enter monitoring mode when enabled) [root@localhost ~] # consul-template-consul-addr 192.168.142.128Visual8500\-template "/ root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/ Local/nginx/sbin/nginx-s reload "\-- log-level=info"-consul-addr "# specify the address"-template "# from left to right: template file path, Generated configuration file name, overloaded nginx "- log-level" # write log level thank you for reading!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report