current position:Home>Docker actual combat case 2: nginx load balancing

Docker actual combat case 2: nginx load balancing

2022-04-29 08:16:52hualaoshuan

1. build CentOS + Nginx Containers 、commit Submit 、 Copy files to each other in the container host ;

build Nginx Mirror image

  • Use the earliest image CentOS, Use interactive mode to enter and install ( Use yum Or compile and install )
  • Tips : Some operations are Dockerfile It's hard to write it out in ( Write completely or write completely without problem ), So sometimes you need to enter the container to operate the contents inside . After the operation is completed , Because containers don't hold content , Once the container is deleted , There is no content in it . So sometimes I put the code file in the host , utilize -v Parameters and containers are mapped , At this time, if the container is down or deleted by mistake , It doesn't matter , The real data is still kept in the host .
  • If you make changes in the container , To save , The operation is as follows
#  Open the terminal  1, Create temporary containers 
# --privileged  Start administrator privileges 
docker run -it --privileged --name tmp centos /usr/sbin/init

#  After running the above command , Will get stuck 
#  Open the terminal  2, Into the container 
docker exec -it tmp /bin/bash

#  install  Nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y

#  Set boot up 
systemctl enable nginx

#  start-up  Nginx
systemctl start nginx

# yum  Default installation , The configuration file will be in  /etc/  Under the table of contents 
#  Because some operations have been done on this container , So you need to save the container , You don't need to reinstall it next time 
#  So save this container as a new image , Need to use  docker commit
#  Put the modified container , Submit to a new image 
#  file :https://docs.docker.com/engine/reference/commandline/commit/#description
#  Exit the mirror first 
exit

#  This creates a new image 
#  Because I didn't use  Dockerfile, So I want to execute some commands when the container starts , such as  usr/sbin/init  And exposure  80  port 
#  These are all available in  Dockerfile  What's written in 
# -c: application  Dockfile  In order to create a  image, Because direct  commit  There may be problems , It must be used.  Dockerfile  The order in 
#  If there are multiple commands , Just write more  -c
docker commit -c 'CMD ["/usr/sbin/init"]' -c "EXPOSE 80" tmp centos:nginx

#  Next, based on the new image , Create a new container , Then map the files in the container to the host ( Such as configuration files )
#  The configuration file must be persistent , Can't be stored in a container without any treatment , So you need to copy 
#  First turn on the  Nginx  Copy the configuration file from the container 
# docker cp: Copy the files in the container to the host , You can also copy the host file to the container 
#  Reference documents :https://docs.docker.com/engine/reference/commandline/cp/
#  Now copy from container to host 
mkdir -p /home/[ user name ]/nginx/conf
docker cp tmp:/etc/nginx/nginx.conf /home/[ user name ]/nginx/conf/

#  Created before this time  tmp  The container can be deleted 
#  Create a new container called  mynginx
#  Host port  9090  Mapping to   Containers  80
#  The host  nginx.conf  And the corresponding in the container 
#  After that, you only need to change the configuration of the host  nginx.conf, There is no need to enter the container to change 
#  In practice , Try not to operate the container repeatedly , Containers are not persistent , It's impossible to make every change  commit  A new mirror image 
# centos:nginx  Is the name of the image just created 
#  After execution  docker images  A new image appears  mynginx
docker run --name mynginx --privileged \
-p 9090:80 -v /home/[ user name ]/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-d centos:nginx

2. Docker Compose And start multiple Web Containers ;

#  preparation 
#  Previously created  centos:jdk  Containers , It's equipped with apache  and  jdk, Reuse 
mkdir -p /home/[ user name ]/nginx/web1/
mkdir -p /home/[ user name ]/nginx/web2/
docker run --name web1 -d -p 8080:80 --privileged -v /home/[ user name ]/nginx/web1/:/var/www/html/ centos:jdk
docker run --name web2 -d -p 8081:80 --privileged -v /home/[ user name ]/nginx/web2/:/var/www/html/ centos:jdk

#  If  web1  I don't want the container anymore 
docker stop web1 && docker stop web1

#  summary , If you create a container , There will be many complex parameters , Map many directories , Many ports , And there will be dozens of containers in actual development 
#  It's impossible for every container to execute so many commands ( Hand copied 、 One by one , Delete containers one by one )
#  So you need to use choreography tools  Docker Compose:https://docs.docker.com/compose/overview/
# Docker  Building tools 、 Tools for container orchestration , Multiple containers can be configured and started , Suitable for complex business scenarios ( similar  js  Tools  webpad)
#  You need to create a profile , Write some parameters related to configuration and commands in the configuration file 
#  You can start multiple containers at one time according to complex requirements , It can also be closed and deleted at one time 
#  For example, there is only one  Dockerfile, Mirror image has nothing , It can still be used  Docker Compose  Create an image and start the container 
#  At the same time, it can also realize dependency management 
#  For example, the two containers created earlier  web1  and  web2  For load balancing , So before you create a container, you must have  Nginx  Containers 

# Docker Compose  install :https://docs.docker.com/compose/install/
# -L: Some websites have multiple jumps , Add this parameter to jump with the website 
# -o: Output the downloaded content to the file in the specified path 
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

#  Give executable permission to the downloaded file 
sudo chmod +x /usr/local/bin/docker-compose

#  View file version 
docker-compose -v

#  Need to make a configuration , There must be a configuration file called  yaml
# yaml  Be similar to  json  perhaps  xml, But the format is more concise , Use some indentation to arrange 
#  About  yaml  Online tools for checking :http://www.yamllint.com
#  Document guidance :https://docs.docker.com/compose/gettingstarted/#step-1-setup#step-3-define-services-in-a-compose-file
#  Create an empty folder 
mkdir -p /home/[ user name ]/mycompose
cd /home/[ user name ]/mycompose
vim docker-compose.yml
#  Write the following 
# services: service , It can also be considered as some tasks 
# ports: If more than one , You have to write a lot of lines 
# volumes: Represents mapping 
# version: Supported by  compose  edition , The latest support  version 3
#  There are also some parameters ,build( be used for  Dockerfile)、command(Dockerfile  Medium  CMD)
services: 
  web1: 
    container_name: web1
    image: "centos:jdk"
    ports: 
      - "8080:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/web1/:/var/www/html/"
  web2: 
    container_name: web2
    image: "centos:jdk"
    ports: 
      - "8081:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/web2/:/var/www/html/"
version: "3"
#  End of writing 
#  start-up :https://docs.docker.com/compose/reference/up/
# up  The command can start the content written in the previous configuration file 
#  Two created in the configuration file  service  All start , You can also specify a separate  service  start-up 
docker-compose up -d 

#  Just stop one container 
docker-compose stop web1

#  Delete container 
docker-compose rm web1

3. Docker network、 Container network 、 Containers access each other ;

The goal is : Three containers , One Nginx Two Web, Load balancing between containers

  • If you put Nginx Put it on the host , It's very simple , There is a port mapping between the host and the container , As long as Nginx Make a configuration in , You can finish it right away
  • If Nginx The container itself is also inside , It involves access between containers ( How to access each other ,IP What is it? )

One Nginx, Two Web Starting up Docker compose The configuration file

#  The configuration file 
services: 
  nginx: 
    container_name: mynginx
    image: "centos:nginx"
    ports: 
      - "9090:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
  web1: 
    container_name: web1
    image: "centos:jdk"
    ports: 
      - "8080:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/web1/:/var/www/html/"
  web2: 
    container_name: web2
    image: "centos:jdk"
    ports: 
      - "8081:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/web2/:/var/www/html/"
version: "3"

#  Start three containers in the background 
docker-compose up -d

Pass between containers IP Way to visit each other (docker network Network settings )

#  Check out the Internet 
docker network ls
#  return 
NETWORK ID          NAME                DRIVER              SCOPE
ab368b5af331        bridge              bridge              local
1f23995f905a        host                host                local
5c34a7a8f019        none                null                local
# bridge: Create and start a container , If it is not set and the network name is specified , It will be added automatically  bridge( Bridging mode )
#  You usually use  bridge  The mode does the interworking between the host and the container 
# host: Generally not used  host  Pattern , Affected  docker  The isolation mechanism of , Not very safe 
# none: There is no Internet 
# *  If no network is specified , Will be created to “ Folder name ” add “_default” Network of , It's a bridge network 

#  Check  network
docker network inspect bridge
# IPAM.Config: Including subnets and gateways 
# Containers.Name/EndpointID/MacAddress/IPv4Address/IPv6Address: When the container is started 
#  There will be containers to add  Containers  in , That's what the configuration file says 
#  It automatically assigns  IP  Address (IPv4)
#  Find a container , Interactively enter 
docker exec -it mynginx /bin/bash
#  There is no... In the container by default  ifconfig  Of , Can install , You can also operate as follows , see  IP
cat /etc/hosts
# ping  Of other containers  IP
ping 172.17.0.4
#  Access other containers ( Pass between containers  IP  Interworking )
# IP  The assignment of depends on the default bridging mode , Create a virtual network address 
curl http://172.19.0.4

#  Passed before  docker-compose  Started three containers , If no settings are made 
# docker network ls  Will appear automatically  NAME  by  mycompose_default
#  This part can also be customized , When created and started , Will create a network of their own 
#  If by a separate command , To start creating a network , And set up a subnet , Produce some  IP

#  First create a network , Reference resources :https://docs.docker.com/engine/reference/commandline/network_create/
docker network create -d bridge mynginx
#  Restart container , At this time, the created container will be added “mynginx” The network 
docker run -d --network=mynginx

# docker network create  The created network has no parameters , So it doesn't make any sense 
#  Generally speaking, you need to specify , For example, subnet 
#  The calculated address of the subnet mask :http://tool.chinaz.com/Tools/subnetmask
#  Set subnet address 
docker network create -d bridge --subnet=192.128.0.0/16 mynginx
#  Check out the list of networks 
docker network ls
#  List the network you just created 
NETWORK ID          NAME                DRIVER              SCOPE
d9fc9ab2d628        mynginx             bridge              local
...
#  View the specified network 
docker network inspect mynginx
#  return  IPAM.Config.Subnet  by  192.128.0.0/16
# Containers  It's empty , Because there are currently no containers joining this network 

#  modify  Compose  To configure , Add all three containers to the new network 
#  Reference link :https://docs.docker.com/compose/compose-file/#network-configuration-reference
#  How to  Compose  Set the basic network configuration file 
cd /home/[ user name ]/mycompose
vim docker-compose.yml
#  Modify the configuration file ( Root addition  networks, Three  service  Add separately  networks, A container can join several networks )
#  Be careful  mynginx  and  mynginx  Agreement 
# mynginx-net  and  mynginx-net  Agreement ( External network name )
networks: 
  mynginx-net: 
    external: 
      name: mynginx
services: 
  nginx: 
    container_name: mynginx
    image: "centos:nginx"
    networks: 
      - mynginx-net
    ports: 
      - "9090:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
  web1: 
    container_name: web1
    image: "centos:jdk"
    networks: 
      - mynginx-net
    ports: 
      - "8080:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/web1/:/var/www/html/"
  web2: 
    container_name: web2
    image: "centos:jdk"
    networks: 
      - mynginx-net
    ports: 
      - "8081:80"
    privileged: true
    volumes: 
      - "/home/hua/nginx/web2/:/var/www/html/"
version: "3"
#  preservation 
#  The first three containers (mynginx、web1、web2) It's stopped 
#  Due to the use of external containers , No need to use  up  Command to delete again 、 establish 、 Start the container 
#  Use it directly  start
docker-compose start

#  Check out the Internet 
# Ccontainers  Three containers appear ,IPv4Address  Joined the previously created network 
docker network inspect mynginx

#  Into the container ( The container name is the same as the network name )
docker exec -it mynginx /bin/bash
cat /etc/hosts

4. ( Case study )Docker compose Creating networks 、 Specify the container IP、 It's easy to start Nginx Load balancing ;

The goal is

  • stay compose up Automatically create a network when you start the container or when you start the container 、 You don't need to manually create... Externally . In actual combat 、 The less additional external operations, the better . Preferably one or two files 、 Start a command directly
  • Containers IP: After the container is created , Appoint IP. Because I'm doing load balancing 、 Or do something you need and IP Address related program call , There must be IP The reference of ( Randomly create IP It's not easy to control )

copyright notice
author[hualaoshuan],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290600598911.html

Random recommended