How do I deploy my Symfony API - - Deploy
Workflow
workflows:
version: 2
build_and_deploy-workflow:
jobs:
- build
- deploy_to_live:
requires:
- deploy_to_live_approval
filters:
branches:
only:
- master
- deploy_to_live_approval:
type: approval
requires:
- build
build
: is the main job; the one explained in the second article and responsible for pushing the images to the docker registry.deploy_to_live
: is the job for the deploy to live (will talk about it in a moment); this job will be executed only for the branch namedmaster
and
before running requires a successful completion of a job calleddeploy_to_live_approval
.deploy_to_live_approval
: is an "type = approval" job, and its completion is just a button on the Circle CI web interface; this allow us to effectively decide if deploying to live or not.
The deploy job
version: 2
executorType: machine
jobs:
build: # this is the job that pushes the images to the registry
# ...
deploy_to_live: # this is the job that effectively deploys to live
working_directory: ~/my_ap
environment:
- DOCKER_HOST: "tcp://myapp-manager.yyy.local:2375"
steps:
- *helpers_system_basic
- *helpers_docker
- run: sudo apt-get -qq -y install openvpn
- checkout
- add_ssh_keys:
fingerprints:
- "af:83:39:00:ad:af:83:39:00:ad:af:83:39:00:ad:99" # import VPN private key
- run:
name: Connect to VPN
command: |
sudo openvpn --daemon --cd .circleci/vpn-live --config my-vpn-config.ovpn
while ! (echo "$DOCKER_HOST" | sed 's/tcp:\/\///'|sed 's/:/ /' |xargs nc -w 2) ; do sleep 1; done
- deploy:
name: Deploy
command: |
docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASS
docker stack deploy live --compose-file=docker-compose.live.yml --with-registry-auth
Step by step
Preparation
environment:
- DOCKER_HOST: "tcp://myapp-manager.yyy.local:2375"`
- *helpers_system_basic # use basic system configurations helper
- *helpers_docker # use basic docker installation helper
- run: sudo apt-get -qq -y install openvpn # install openvpn client
- checkout # checkout the source code
Credentials
- add_ssh_keys:
fingerprints:
- "af:83:39:00:ad:af:83:39:00:ad:af:83:39:00:ad:99"
VPN
- run:
name: Connect to VPN
command: |
sudo openvpn --daemon --cd .circleci/vpn-live --config my-vpn-config.ovpn
while ! (echo "$DOCKER_HOST" | sed 's/tcp:\/\///'|sed 's/:/ /' |xargs nc -w 2) ; do sleep 1; done
The deploy
- deploy:
name: Deploy
command: |
docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASS
docker stack deploy live --compose-file=docker-compose.live.yml --with-registry-auth
export DB_USER="$LIVE_DB_USER"
export DB_PWD="$LIVE_DB_PWD"
docker stack deploy live --compose-file=docker-compose.live.yml --with-registry-auth
source exports_live_vars.sh
docker stack deploy live --compose-file=docker-compose.live.yml --with-registry-auth
The docker-compose.live.yml
# docker-compose.live.yml
version: '3.3'
services:
php:
image: goetas/api-php:master
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 30s
restart_policy:
condition: on-failure
www:
image: goetas/api-nginx:master
deploy:
replicas: 6
update_config:
parallelism: 2
delay: 30s
restart_policy:
condition: on-failure
ports:
- "80:80"
- Deploy 4 containers for each service. Hopefully they will be uniformly distributed across the cluster, but this is not guaranteed. Docker offers a
placement
configuration option to instruct the scheduler on how to distribute containers across the cluster. - When updating the containers (as example a second deploy) update two containers and wait for 30 seconds before updating other two containers.
Updating the container here means: download latest image, stop and remove old container, create and start the container using the new image. - Restart the containers if they fail. In case of "weird" errors that should not happen anyway but they will.
Comments
Post a Comment