Deployment of Application on CEP

To deploy application on CEP, we will be using the docker stack deploy command. Docker stack deployment requires a compose file in yaml format, having compose file version 3 or above.

compose file have the services, network, and other configs related to deployment.

A basic compose file is as follows :

version: "3"
services:
  mongo:
    image: registry.oecloud.local/alpine-mongo:latest
  web: 
    image: registry.oecloud.local/sample-app:latest
    depends_on:
      - mongo   

you can deploy this application using docker stack deploy -c <<compose-file.yml>> STACK_NAME

There is some other configs related to deployment we can specify in compose file services.

DEPLOY

Specify configuration related to the deployment and running of services, you can specify all options supported by docker service create/update commands, this includes:

REPLICAS

If the service is replicated (which is the default), specify the number of containers that should be running at any given time.

version: '3'
services:
  worker:
    image: registry.oecloud.local/sample-app:latest
    deploy:
      mode: replicated
      replicas: 3

RESOURCES

you can configure the resource constraints as following:

version: '3'
services:
  redis:
    image: registry.oecloud.local/sample-app:latest
    deploy:
      resources:
        limits:
          cpus: '0.1'
          memory: 1024M

RESTART_POLICY

Configures if and how to restart containers when they exit.

  • condition: One of none, on-failure or any (default: any).
  • delay: How long to wait between restart attempts, specified as a duration (default: 0).
  • max_attempts: How many times to attempt to restart a container before giving up (default: never give up)
  • window: How long to wait before deciding if a restart has succeeded, specified as a duration (default: decide immediately).
version: "3"
services:
  redis:
    image: registry.oecloud.local/sample-app:latest
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s

UPDATE_CONFIG(Rolling Update)

Configures how the service should be updated. Useful for configuring rolling updates.

  • parallelism: The number of containers to update at a time.
  • delay: The time to wait between updating a group of containers.
  • failure_action: What to do if an update fails. One of continue or pause (default: pause).
  • monitor: Duration after each task update to monitor for failure (ns|us|ms|s|m|h) (default 0s).
  • max_failure_ratio: Failure rate to tolerate during an update.
version: '3'
services:
  vote:
    image: registry.oecloud.local/sample-app:latest
    depends_on:
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s

LOGGING

CEP includes graylog as by default centralized logging system, you can configure application for logging using following.

version: "3"
services:
  web:
    image: registry.oecloud.local/sample-app:latest
    deploy:
      mode: replicated
      replicas: 1
    logging:
      driver: gelf
      options:
        gelf-address: "udp://<graylog_host>:12201"

For more logging related info please check Logging Guide

ENVIRONMENT

To add environment variables in service, you need to add config environment. Any boolean values; true, false, yes no, need to be enclosed in quotes to ensure they are not converted to True or False by the YML parser.

version: "3"
services:
  web:
    image: registry.oecloud.local/sample-app:latest
    deploy:
      mode: replicated
      replicas: 1
    environement:
      DB_HOST: "10.0.0.1"
      DB_USER: "user"
      DB_PASSWORD: "pass"

NETWORKS

docker stack deploy by default creates a overlay network and attach services to that network. If you need to create user defined network, the top-level networks key lets you specify networks to be created. Then to allow services to join network you need to add network config in service.

version: "3"
services:
  web:
    image: registry.oecloud.local/sample-app:latest
    networks:
      - sample-network
    deploy:
      mode: replicated
      replicas: 1
networks:
  sample-network:

If there is some external network, then you need to join then provide a property external to true under networks.

version: "3"
services:
  web:
    image: registry.oecloud.local/sample-app:latest
    networks:
      - sample-network
      - <<external_network_name>>
    deploy:
      mode: replicated
      replicas: 1
networks:
  sample-network:
  <<external_network_name>>:
    external: true

Accessing Services

To access the application from outside either you can publish port, or can join service to network router_network and provide two environment variables VIRTUAL_HOST, SERVICE_PORTS.

version: "3"
services:
  web:
    image: registry.oecloud.local/sample-app:latest
    deploy:
      mode: replicated
      replicas: 1
    environment:
      VIRTUAL_HOST: "https://sampleapp.oecloud.io,sampleapp.oecloud.io"
      SERVICE_PORTS: "3000"
      FORCE_SSL: "yes"
    networks:
      - sample-network
      - router_network
networks:
  sample-network:
  router_network:
    external: true

here VIRTUAL_HOST will be the url for your application and SERVICE_PORTS is the port on which application is exposed. you can give multiple entries for VIRTUAL_HOST env variable seperated by comma.

Sample Compose file

Below is the sample compose file to deploy on CEP, and having all configuration specified above.

version: "3"
services:
  mongo:
    image: registry.oecloud.local/alpine-mongo:latest
    networks:
      - sample-app-network
  web:
    image: registry.oecloud.local/sample-app:latest
    depends_on:
      - mongo
    deploy:
      mode: replicated
      replicas: 1
      update_config:
        delay: 60s
    environment:
      VIRTUAL_HOST: "https://sampleapp.oecloud.io,sampleapp.oecloud.io"
      SERVICE_PORTS: "3000"
      FORCE_SSL: "yes"
    logging:
      driver: gelf
      options:
        gelf-address: "udp://<graylog_host>:12201"
    networks:
      - sample-app-network
      - router_network
networks:
  sample-app-network:
  router_network:
    external: true

For more detailed information about compose file, please click here.