preface

Containerization, cloud native is getting more and more intense, and there are many new concepts. With the explosion of information comes layers of fog. I tried to understand its veins from the perspective of capacity expansion. After practical exploration, I organized and formed an introductory course, including the following four articles.

  • The path to containerization practices – from Docker to istio – containerization of applications using Docker
  • The path to containerization practices – from Docker to Istio ii – using Compose to deploy applications
  • The Path to containerization practices – From Docker to Istio iii – Kubernetes Orchestration applications
  • The Path to containerization practice – From Docker to ISTIO 4 – ISTIO Management Application

This is the second installment of the docker2istio directory for deploying an application using Compose.

compose

Compose is a tool for defining and running multi-container Docker applications, written in Python.

Deploy the application and test

Write application deployment files

Compose docker-comemage. yml:

version: '3'Services: Redis: image: redis:4- Alpine3.8 restart: Always FlaskApp: depends_on: - Redis build:./app image: Flaskapp :0.0.2 links: -redis nginx: Image :1.15.8- Alpine Depends_on: -FlaskApp Volumes: - ./nginx:/etc/nginx/conf.d restart: always ports: -"80:80"
    environment:
      - NGINX_PORT=80
    links:
      - flaskapp

Copy the code

Here are a few things described:

  1. In turn, startredisflaskappnginxThree services. Service sequence bydepends_onDecision.
  2. usebuildCommand to automatically compileFlaskapp: hundreds.
  3. uselinksCommand describing inter-service dependencies.
  4. useportsExport port, usedvolumesMount data volumes.

This process makes the process of starting the container in Part 1 more semantic and clearer.

Start the application

To start the application, use the docker-compose up command:

Creating network "docker2istio_default"With the default driver Building flaskApp Step 1/5: FROM Python :3.6-alpine --> 1D981AF1e3b4 Step 2/5: WORKDIR /code ---> Using cache ---> 7f2b07b16752 Step 3/5 : RUN pip install redis flask ---> Using cache ---> 79e39b6c2e93 Step 4/5 : ADD . /code ---> 4266029c0709 Step 5/5 : CMD ["python"."flaskapp.py"]
 ---> Running in56e799a2fb61 Removing intermediate container 56e799a2fb61 ---> 1a61773c4c07 Successfully built 1a61773c4c07 Successfully Tagged flaskapp: hundreds WARNING: Imagefor service flaskapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker2istio_redis_1 ... done
Creating docker2istio_flaskapp_1 ... done
Creating docker2istio_nginx_1    ... done
Attaching to docker2istio_redis_1, docker2istio_flaskapp_1, docker2istio_nginx_1
flaskapp_1  |  * Serving Flask app "flaskapp" (lazy loading)
flaskapp_1  |  * Environment: production
flaskapp_1  |    WARNING: Do not use the development server ina production environment. flaskapp_1 | Use a production WSGI server instead. flaskapp_1 | * Debug mode: On redis_1 | 1:09 Apr 12:06:15 C. 892# oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo09 Apr 12:06:15 redis_1 | 1: C. 893# Redis version=4.0.12, bits=64, commit=00000000, Modified =0, PID =1, just started09 Apr 12:06:15 redis_1 | 1: C. 893# Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.confFlaskapp_1 | * Running on http://0.0.0.0:5000/ (Press CTRL + C to quit) flaskapp_1 | * Restarting withstatflaskapp_1 | * Debugger is active! Redis_1 | 1: M 09 Apr 12:06:15 standalone = 894 * Running mode, the port = 6379 redis_1 | 1: M 09 Apr 12:06:15. 894# WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.Redis_1 | 1: M 09 Apr 12:06:15. 894# Server initializedThe Debugger flaskapp_1 | * PIN: 323-612-506 redis_1 | 1: M 09 Apr 12:06:15. 894# WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as  root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.Redis_1 | 1: M 09 Apr 12:06:15. 894 * Ready to accept connectionsCopy the code

The startup log shows the process of creating a network, compiling an image, and starting a container

To access the application

Docker-compose ps: docker-compose ps

         Name                        Command               State         Ports
-------------------------------------------------------------------------------------
docker2istio_flaskapp_1   python flaskapp.py               Up
docker2istio_nginx_1      nginx -g daemon off;             Up      0.0.0.0:80->80/tcp
docker2istio_redis_1      docker-entrypoint.sh redis ...   Up      6379/tcp
Copy the code

Compose also uses docker, docker ps is also available:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES C96FD468C415 NGINX :1.15.8-alpine"Nginx - g 'daemon of..."3 minutes ago Up 3 minutes 0.0.0.0:80->80/ TCP Docker2istio_nginx_1 b61d1d0ca201 flaskApp :0.0.2"python flaskapp.py"3 minutes ago Up 3 minutes Docker2IStio_FlaskApp_1 73A2359655D2 Redis :4- AlPINE3.8"Docker - entrypoint. S..."   3 minutes ago       Up 3 minutes        6379/tcp             docker2istio_redis_1
Copy the code

Docker-compose PS is composed for application layer.

Then access the service:

➜ ~ curl http://127.0.0.1 Hello World by 172.19.0.3 from 172.19.0.1! This page has been accessed once.Copy the code

Expansion and application

Docker-compose up –scale FlaskApp =2

View the expansion result:

➜  docker2istio docker-compose ps
         Name                        Command               State         Ports
-------------------------------------------------------------------------------------
docker2istio_flaskapp_1   python flaskapp.py               Up
docker2istio_flaskapp_2   python flaskapp.py               Up
docker2istio_nginx_1      nginx -g daemon off;             Up      0.0.0.0:80->80/tcp
docker2istio_redis_1      docker-entrypoint.sh redis ...   Up      6379/tcp
Copy the code

Refresh access application

➜ docker2istio curl http://127.0.0.1 Hello World by 172.20.0.4 from 172.20.0.1! This page has been accessed 101 times. ➜ docker2istio curl http://127.0.0.1 Hello World by 172.20.0.3 from 172.20.0.1! This page has been accessed 102 times.Copy the code

Observe the service log output at the same time:

Nginx_1 | 172.22.0.1 - [10 / Apr / 2019:01:38:02 + 0000]"The GET/HTTP / 1.0"200, 76,"-" "ApacheBench / 2.3" "-"The DEBUG flaskapp_1 | [the 01:38:02 2019-04-10, 140]in65: flaskapp: Hello out 172.22.0.3 172.22.0.1 flaskapp_1 | 172.22.0.5-10 / Apr / 2019 01:38:02"The GET/HTTP / 1.0"200 - flaskapp_1 | DEBUG [the 01:38:02 2019-04-10, 141]in63: flaskapp: Hello out 172.22.0.3 172.22.0.1 flaskapp_2 | DEBUG [the 01:38:02 2019-04-10, 145]in flaskapp: hello inFlaskapp_1 | 172.22.0.5-10 / Apr / 2019 01:38:02"The GET/HTTP / 1.0"200 - nginx_1 | 172.22.0.1 - [10 / Apr / 2019:01:38:02 + 0000]"The GET/HTTP / 1.0"200, 76,"-" "ApacheBench / 2.3" "-"The DEBUG flaskapp_1 | [the 01:38:02 2019-04-10, 150]in flaskapp: hello inThe DEBUG flaskapp_2 | [the 01:38:02 2019-04-10, 151]in flaskapp: hello inNginx_1 | 172.22.0.1 - [10 / Apr / 2019:01:38:02 + 0000]"The GET/HTTP / 1.0"200, 76,"-" "ApacheBench / 2.3" "-"The DEBUG flaskapp_2 | [the 01:38:02 2019-04-10, 153]in67: flaskapp: Hello out 172.22.0.4 172.22.0.1 flaskapp_2 | 172.22.0.5-10 / Apr / 2019 01:38:02"The GET/HTTP / 1.0"200 - nginx_1 | 172.22.0.1 - [10 / Apr / 2019:01:38:02 + 0000]"The GET/HTTP / 1.0"200, 76,"-" "ApacheBench / 2.3" "-"The DEBUG flaskapp_2 | [the 01:38:02 2019-04-10, 156]in69: flaskapp: Hello out 172.22.0.4 172.22.0.1 flaskapp_1 | DEBUG [the 01:38:02 2019-04-10, 156]in flaskapp: hello inFlaskapp_2 | 172.22.0.5-10 / Apr / 2019 01:38:02"The GET/HTTP / 1.0"200 - flaskapp_1 | DEBUG [the 01:38:02 2019-04-10, 159]in flaskapp: hello inThe DEBUG flaskapp_2 | [the 01:38:02 2019-04-10, 161]in flaskapp: hello inNginx_1 | 172.22.0.1 - [10 / Apr / 2019:01:38:02 + 0000]"The GET/HTTP / 1.0"200, 76,"-" "ApacheBench / 2.3" "-"The DEBUG flaskapp_1 | [the 01:38:02 2019-04-10, 160]inFlaskapp: Hello out 172.22.0.3 172.22.0.1 70:Copy the code

Compared with pure Docker, capacity expansion becomes simple.

Note that the nginx service must be restarted during capacity expansion. Otherwise, service traffic will not be allocated to the new container even if the container is expanded to multiple containers. Docker-compose Down can be used to compose the docker-compose down container.

docker-compose scale Note: This command is deprecated. Use the up command with the –scale flag instead. Beware that using up with –scale flag has some subtle differences with the scale command as it incorporates the behaviour of up command.

Clean up the application

Use docker-compose Down to clean up your app with one click

Removing docker2istio_nginx_1    ... done
Removing docker2istio_flaskapp_2 ... done
Removing docker2istio_flaskapp_1 ... done
Removing docker2istio_redis_1    ... done
Removing network docker2istio_default
Copy the code

conclusion

Compose already implements automatic shift for container expansion:

  1. More intuitive control of container startup sequence and dependencies.
  2. Convenient to use and expand capacity.

But Compose’s automatic transmission, at best a motorbike version, works well as a deployment solution for small/test applications. For large/formal applications, there are also the following disadvantages:

  1. Capacity expansion cannot be seamless and services need to be restarted.
  2. Compose alone does not support multi-machine connectivity.

To achieve multi-machine deployment and expansion, we need to use Kubernetes’ container choreography scheme. From deployment to choreography, one-dimensional understanding, it seems that the number of containers that can be maintained has grown. Compared with Docker’s Swarm + Machine scheme, Kubernetes has been the de facto standard in the field of container choreography, which is worth learning.

recommended

  1. Harbor Harbor application includes several services, and the recommended deployment mode is Compose.

  2. Gogs – drone – docker. Docker Compose demonstrates how to quickly build a CI system using Docker Compose.