First, start chapter 1 by creating the Microservices directory in the home directory.

cd ~ && mkdir microservices && cd microservices
Copy the code

Nginx01, nginx02, and nginx03 are used as reverse proxy servers to evenly forward requests to nginx02 and nginx03.

mkdir -p ./nginx/nginx01 ./nginx/nginx02 ./nginx/nginx03
Copy the code

The display effect is shown below.

Nginx ├─ nginx ├─ nginx ├─ nginx ├─ nginxCopy the code

Copy the configuration file in the nginx image to each subdirectory for mounting. Create a temporary container, copy the configuration file to the host directory, and then delete the temporary container.

docker run --name tmpnginx -d nginx:latest
docker cp tmpnginx:/etc/nginx/nginx.conf ~/microservices/nginx/nginx01
docker cp tmpnginx:/etc/nginx/nginx.conf ~/microservices/nginx/nginx02
docker cp tmpnginx:/etc/nginx/nginx.conf ~/microservices/nginx/nginx03
docker cp tmpnginx:/etc/nginx/conf.d ~/microservices/nginx/nginx01
docker cp tmpnginx:/etc/nginx/conf.d ~/microservices/nginx/nginx02
docker cp tmpnginx:/etc/nginx/conf.d ~/microservices/nginx/nginx03
docker rm -f tmpnginx
Copy the code

The nginx directory is shown below.

Nginx ├ ─ ─ nginx01 │ ├ ─ ─ the conf. D │ │ └ ─ ─ the default. The conf │ └ ─ ─ nginx. Conf ├ ─ ─ nginx02 │ ├ ─ ─ the conf. D │ │ └ ─ ─ the default. The conf │ └ ─ ─ ├─ ├─ nginx.conf, ├─ nginxCopy the code

Create a file docker-comemage. yml in the root directory, create three Web services, and map the configuration files to the corresponding files in the container.

version: '3'

services:
  web01:  # service name
    image: nginx:latest # image
    container_name: web01 # container name
    ports:  # mapping port number, former host port, latter container port
      - 8080: 80
    volumes: The former host directory, the latter container directory
      - ./nginx/nginx01/nginx.conf:/etc/nginx/nginx.conf # config file
      - ./nginx/nginx01/conf.d:/etc/nginx/conf.d # extend configuration directory
      - ./nginx/html:/usr/share/nginx/html # HTML directory

  web02:
    image: nginx:latest
    container_name: web02
    volumes:
      - ./nginx/nginx02/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/nginx02/conf.d:/etc/nginx/conf.d
      - ./nginx/html:/usr/share/nginx/html

  web03:
    image: nginx:latest
    container_name: web03
    volumes:
      - ./nginx/nginx03/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/nginx03/conf.d:/etc/nginx/conf.d
      - ./nginx/html:/usr/share/nginx/html
Copy the code

Open the nginx/nginx01 / conf. D/default. Conf. On the top of the article to join upstream configuration, web02 and web03 docker – compose. Yml container_name name defined in the container.

upstream backend {
    server web02:80;
    server web03:80;
}
Copy the code

Add proxy_pass to location/to forward requests to backend.

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    proxy_pass http://backend;  # append line
}
Copy the code

When the configuration is complete, run the following command to start the container.

cd ~/microservices
docker-compose up
Copy the code

Success if the following information is displayed.

Recreating microservices_web01_1 ... done
Recreating microservices_web02_1 ... done
Recreating microservices_web03_1 ... done
Attaching to web02, web01, web03
Copy the code

Create an index. HTML file in the nginx directory and type Hello world! Run again.

Microservices ├ ─ ─ docker - compose. Yml └ ─ ─ nginx ├ ─ ─ HTML │ └ ─ ─ index. The HTML ├ ─ ─ nginx01 │ ├ ─ ─ the conf. D │ │ └ ─ ─ the default. The conf │ └ ─ ─ nginx. Conf ├ ─ ─ nginx02 │ ├ ─ ─ the conf. D │ │ └ ─ ─ the default. The conf │ └ ─ ─ nginx. Conf └ ─ ─ nginx03 ├ ─ ─ the conf. D │ └ ─ ─ default. Conf └ ─ ─ nginx. ConfCopy the code

Now to test this, go to localhost:8080 in your browser and watch the log printed by the terminal.

Web01 | 172.24.0.1 - [26 / Jun / 2019:01:48:28 + 0000]"The GET/HTTP / 1.1" 304 0 "-" "Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"Web02 | 172.24.0.2 - [26 / Jun / 2019:01:48:28 + 0000]"The GET/HTTP / 1.0" 304 0 "-" "Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
Copy the code

The preceding content indicates that the request is forwarded to Web02 through Web01.

Web01 | 172.24.0.1 - [26 / Jun / 2019:04:42:36 + 0000]"The GET/HTTP / 1.1" 304 0 "-" "Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"Web03 | 172.24.0.2 - [26 / Jun / 2019:04:42:36 + 0000]"The GET/HTTP / 1.0" 304 0 "-" "Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" "-"
Copy the code

So far, the basic load balancing deployment is complete. Web01 is used to balance requests to Web02 and Web03. This method is called polling.


Welcome to Github Open Source Books

  • PHPer plays Golang
  • Building microservices with Your bare hands