docker compose of docker three swordsmen (configure haproxy+nginx load balancing)

1, Introduction to docker compose

1. The application system of microservice architecture generally includes several microservices, and each microservice will generally deploy multiple instances. If each microservice needs to be started and stopped manually, it can be imagined that the efficiency is low and the amount of maintenance is large.
2. docker compose is an orchestration service based on pyhton language. It is a tool for defining and running complex applications on docker. It allows users to deploy distributed applications in clusters.
3. Users can easily define a multi container application with a configuration file, and then install all the dependencies of the application with one instruction to complete the construction.
4. It solves the problem of how to manage orchestration between containers.

  • There are two important concepts in docker compose:
  • Service: an application container can actually include several container instances running the same image.
  • Project: a complete business unit composed of a group of associated application containers, which is located in docker-compose.com Defined in the YML file.

2, docker compose practice

1. Compose docker installation

  • Download the composition in advance or directly from the official website. The composition is a binary file and needs to be placed in the / usr/local/bin / directory with execution permission
curl -L https://github. COM / docker / compose / releases / download / 1.21.2/docker-compose - ` uname - s ` - ` uname - M ` > / root / docker ## download from the official website
mv docker-compose-Linux-x86_64-1.27.0 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

2,docker-compose.yml attribute

  • Image: Specifies the image name or image ID. if the image does not exist locally, compose will try to pull the image
  • Build: Specifies the path to the folder where the Dockerfile is located. compose will use it to automatically build the image and then use the image
  • Command: overrides the command executed by default after the container is started
  • Links: links to containers in other services
  • Ports: port mapping
  • Expose:: expose port information
  • Volumes: volume mount path settings

See the official website for more properties: https://docs.docker.com/compose/compose-file/

3. Docker compose configuring haproxy+nginx load balancing

(1) Overall structure of compose

(2) Create docker compose YML file

mkdir /root/compose  ##Create project directory
cd /root/compose
vim docker-compose.yml

version: "3.8"
services:
  web1:
    image: nginx
    networks:
      - webnet
    volumes:
      - /root/compose/web1:/usr/share/nginx/html  ##The mount data volume is the default publishing file for nginx

  web2:
    image: nginx
    networks:
      - webnet
    volumes:
      - /root/compose/web2:/usr/share/nginx/html  ##The mount data volume is the default publishing file for nginx

  haproxy:
    image: haproxy
    networks:
      - webnet
    volumes:
      - /root/compose/haproxy:/usr/local/etc/haproxy  ##The mounted data volume is the configuration file that needs to be modified for haproxy
    ports:
      - "80:80"  ##Port maps the local 80 port to the container's 80 port

networks:
  webnet:  ##Specify the network connection method as webnet

(3) Edit haproxy CFG configuration file

mkdir /root/compose/haproxy
cd /root/compose/haproxy
vim haproxy.cfg

#
# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
#
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.
#

global
        maxconn         65535
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0
        uid             200
        gid             200
        daemon

defaults
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  5s
        stats uri       /admin/stats



# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear  ##Listen to port 80 of all ip addresses
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem

        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend dynamic

# The static backend backend for 'Host: img', /img and /css.
backend dynamic
        balance         roundrobin
        server          a web1:80 check inter 1000
        server          b web2:80 check inter 1000

(4) Write default publishing content

mkdir /root/compose/web1 /root/compose/web2
cd /root/compose
echo web1 > web1/index.html
echo web2 > web2/index.html

(5) Start service

  • The docker compose command must be run under the project compose, - d means entering the background
cd /root/compose
docker-compose up -d

(6) View process

docker-compose ps

(7) Access test

curl 172.25.36.1

4. docker compose common commands

  • Build: build or rebuild services
  • kill: force the service container to stop
  • logs: view the output of the service
  • Port: print the public port of the binding
  • ps: list all containers
  • Pull: pull the image required by the service
  • rm: delete stopped service content
  • up: build and start the container

Keywords: Linux Operation & Maintenance Docker Load Balance Nginx

Added by spiffy577 on Mon, 17 Jan 2022 00:14:58 +0200