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

How to use Docker Compose to open nginx

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

Share

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

How to use Docker Compose to open nginx? I believe that most people have not yet learned this skill, in order to let you learn, to give you a summary of the following content, do not say much, let's move on.

Docker Compose configuration common fields build dockerfile context specify Dockerfile file name build image context path image specify image command execute command, override default command container name specify container name, because container name is unique, if you specify custom name, you cannot scaledeploy to specify deployment and running service related configuration, you can only use environment to add environment variable networks to network ports to expose container port in swarm mode But the port cannot be lower than 60volumes mount host or command volume, like data volume restart restart policy hostname container hostname Docer Compose common commands build rebuild service ps list container up create and start container exec execute command scale specify a service container startup number top display container process logs view container output down delete container, network The data volume and mirror stop/start/restart stop / start / restart service Compose command describes the docker-compose option-- verbose outputs more debug information-- version print version and exit-- fmaiko file uses a specific compose template file, which defaults to docker-compose.yml-p, specifies the project name, and defaults to the directory name ConsulConsul. It is an open source tool launched by HashCorp. The characteristics of Consul used to implement service and configuration of distributed systems Consul supports health check, allows storage key value pair consistency protocol to use Raft algorithm, is used to ensure high availability member management and message broadcasting of services using GOSSIP protocol, and supports ACL access control list Work seamlessly with docker to build an automatically discovered Docker service architecture to build Consul services. Consul is deployed on every node that provides services. AgentConsul agent has two operation modes, ServerClientServer and Client, which are just the distinction at the Conusl cluster level and have nothing to do with the application services built on top of Cluster.

Consul cluster architecture, discover and register docker containers in nginx, and monitor the status of node servers.

The nginx in the consul server server acts as a reverse proxy to poll the containers in the server pool. When users access the proxy port, they can access multiple containers behind it, and our port is mapped twice. Configuration files can be modified uniformly on the consul server

-docker-compose container choreography-

# nginx package is available in the shared directory of the host We mount to / opt/ directory mount.cifs / / 192.168.100.25/LNMP / opt/# to create container orchestration directory mkdir / root/compose_ngin# to create nginx container image cd compose_nginx/mkdir nginxcd nginx/cd / opt/cp nginx-1.12.0.tar.gz / root/compose_nginx/nginx/cd / root/compose_nginx/nginx/# plus nginx startup script vim run.sh #! / bin/bash/usr/local/nginx / sbin/nginx [root@localhost nginx] # vim DockerfileFROM centos:7MAINTAINER this is nginx RUN yum-y updateRUN yum-y install wget pcre-devel zlib-devel make zlib gcc gcc-c++ openssl-devel net-toolsRUN 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.confWORKDIR / root/nginxADD run.sh / run.shRUN chmod 755 / run.shCMD [" / run.sh "] write docker-compose choreography and install nginx [root @ localhost compose_nginx] # vim docker-compose.ymlversion: '3'services: nginx: hostname: nginx build: context:. / nginx dockerfile: Dockerfile ports:-1216 run.shCMD 80-1217 run.shCMD 443 networks:- Chen volumes: -. / wwwroot:/usr/local/nginx/htmlnetworks: chen: [root@localhost compose_nginx] # mount.cifs / / 192.168.100.25/compose / mnt/Password for root@//192.168.100.25/compose: [root@localhost compose_nginx] # cd / mnt/ [root@localhost mnt] # lsconsul_0.9.2_linux_amd64.zip consul-template_0.19.3_linux_amd64.zip docker-compose [root@localhost mnt] # cp-p docker-compose / usr/local/bin/ [root@localhost mnt] # chmod + x / usr/local/bin/docker-compose # enable route forwarding [root@localhost compose_nginx] # vim / etc/sysctl.confnet.ipv4.ip_forward=1 [root@localhost compose_nginx] # sysctl-pnet.ipv4.ip_forward = 1 use orchestration tool to open nginx [root@localhost compose_nginx] # docker-compose-f docker-compose.yml up-d [ Root@localhost compose_nginx] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES66d7cee5692d compose_nginx_nginx "/ run.sh" 11 seconds ago Up 9 seconds 0.0.0.0 aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES66d7cee5692d compose_nginx_nginx 1216-> 80/tcp 0.0.0.0 lsdocker-compose.yml nginx wwwroot 1217-> 443/tcp compose_nginx_nginx_1 We made the data volume and we wrote a default home page to [root@localhost compose_nginx] # lsdocker-compose.yml nginx wwwroot [root@localhost compose_nginx] # cd wwwroot/ [root@localhost wwwroot] # vim index.htmlthis is chen web!

-consul deployment -

Automatically discover and register multiple docker container nodes. If you want to be managed by consul, you must register. Agent will submit a registration request when the search conditions are met. Consul server automatically discovers to process the registration request and monitor the status of the container. Template template [root@localhost ~] # mkdir consul [root@localhost ~] # cd / mnt/ [root@localhost mnt] # lsconsul_0.9.2_linux_amd64.zip consul-template_0.19.3_linux_amd64.zip docker-compose [root@localhost mnt] # cp consul_0.9.2_linux_amd64.zip / root/consul/ [root@localhost mnt] # cd / root/consul/ [root@localhost consul] # lsconsul_0.9.2_linux_amd64.zip [root@localhost consul] # unzip consul_0.9.2_linux_amd64.zip Archive: consul_0.9.2_linux_amd64.zip inflating: consul [root@localhost consul] # mv consul / usr/bin/# uses conusl agent proxy function [root@localhost consul] # consul agent\ # specify server function # participant Leader # ui Interface # Parameter specify location # bind Local address # face all nodes # Local Node name mixed output to Log Run >-server\ >-bootstrap\ >-ui\ >-data-dir=/var/lib/consul-data\ >-bind=192.168.136.142\ >-client=0.0.0.0\ >-node=consul-server01 & > / var/log/consul.log & [1] 3795colors to view cluster information [root@localhost consul] # consul membersNode Address Status Type Build Protocol DCconsul-server01 192.168.136.142data-dir=/var/lib/consul-data 8301 alive There is only one server 0.9.22 dc1# now. It is the boss [root@localhost consul] # consul info | grep leader leader = true leader_addr = 192.168.136.142 grep leader leader 8300 second node server (create multiple containers) Container Service automatically joins nginx cluster [root@localhost] # docker run-d\ >-- name=registrator\ # specify container name >-- net=host\ # specify host >-v / var/run/docker.sock:/tmp/docker.sock \ # specify the data volume host directory Container directory >-- restart=always\ # restart > gliderlabs/registrator:latest\ # specify your local image >-ip=192.168.136.167\ # specify your local address > consul://192.168.136.142:8500 # specify the address of consul # two nginx service containers [root@localhost ~] # docker run-itd-name test-01-h test01 nginxroot@localhost ~] # docker run-itd-docker run 8480 -- name test-02-h test02 nginx# two apache service 5 containers [root@localhost ~] # docker run-itd-p name test-04 88name test-04 80-- name test-03-h test02 httpd [root@localhost ~] # docker run-itd-p apache 89 name test-04 80-- name test-04-h test04 httpd# View all containers [root@localhost ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES00a94fe6fd0c httpd "httpd-foreground" 6 seconds ago Up 5 seconds 0.0.0.0 seconds 89-> 80/tcp test-04b47b89287e43 httpd "httpd-foreground" 38 seconds ago Up 37 seconds 0.0.0.0: 88-> 80/tcp test-039b695e1d8660 nginx "nginx-g 'daemon of..." About a minute ago Up About a minute 0.0.0.0 nginx 84-> 80/tcp test-023cbf17118dab nginx "nginx-g 'daemon of …" 2 minutes ago Up 2 minutes 0.0.0.0 80/tcp test-01055caf398060 gliderlabs/registrator:latest 83-> 80/tcp test-01055caf398060 gliderlabs/registrator:latest "/ bin/registrator-I …" 3 minutes ago Up 3 minutes registrator verifies that http and nginx services are registered with consul

The consul server Consul-Templatee is a daemon that queries Consul cluster information in real time and updates any number of specified templates on the file system to generate configuration files. After the update is completed, you can choose to run the shell command to perform the update operation. Reloading nginx,Consul-template can query the service catalog in Consul, key This powerful abstract function and query language template such as key-values make Consul-template particularly suitable for dynamic creation of configuration files [root@localhost ~] # cd / root/ [root@localhost ~] # lsanaconda-ks.cfg compose_nginx initial-setup-ks.cfg public document template music chen consul download picture desktop video [root@localhost ~ ] # cd consul/ [root@localhost consul] # lsconsul_0.9.2_linux_ amd64.zip [root @ localhost consul] # # prepare the temlate nginx template file [root@localhost consul] # vim nginx.ctmplupstream http_backend {range service "nginx"}} server {{.Address}: {{.port}} {{end}} server {listen 1216; server_name localhost 192.168.136.167; 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-Forwarded-For $proxy_add_x_forwarded_for Proxy_pass http://http_backend; }} # install nginx [root @ localhost nginx-1.12.0] # yum install gcc gcc-c++ pcre-devel zlib-devel-y [root@localhost nginx-1.12.0] #. / configure-- prefix=/usr/local/nginx [root@localhost nginx-1.12.0] # make & & make install [root@localhost nginx-1.12.0] # vim / usr/local/nginx/conf/nginx.conf#nginx has events,http,if,location, field 19 include vhost/*.conf [root@localhost nginx-1.12.0] # cd / usr/local/nginx/conf/ [root@localhost conf] # mkdir vhost [root@localhost consul] # cd / var/log/ [root@localhost log] # mkdir / var/log/nginx [root@localhost log] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ [root@localhost log] # nginx [root@localhost log] # netstat-ntap | grep nginxtcp 00 0.0.0 .0:80 0.0.0.0 LISTEN 107612/nginx: ma [root@localhost opt] # cd / mnt/ [root@localhost mnt] # cp consul-template_0.19.3_linux_amd64.zip / root/ [root@localhost mnt] # cd / root/ [root@localhost] # unzip consul-template_0.19.3_linux_amd64.zip [root@localhost] # mv consul-template / usr/bin / # enable template [root@localhost ~] # consul-template-consul-addr 192.168.136.142 template 8500\ >-template "/ root/consul/nginx.ctmpl:/usr/local/nginx/conf/vhost/kgc.conf:/usr/local/nginx/sbin/nginx-s reload"\ >-log-level=info#coul server is monitored Let's open another terminal and the same server.

# Verification

# add a nginx container node and add a nginx container node to test the service discovery and configuration update function / / register dockers run-itd-p itd 85 80-- name test-05-h test05 nginxdocker logs-f test-01docker logs-f test-02docker logs-f test-05 on the registrator server

The above is to use Docker Compose to open nginx method introduction, detailed use of their own use to know the specific essentials. If you want to read more related articles, you are welcome to follow the industry information channel!

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