Deploy Docker Swarm on AWS EC2 via cloud-formation templates - Step 8 - Cleanup
In this step we cleanup the resources that we creater for the Docker Swarm cluster on EC2.
This post is part of a thread that includes these steps:
- Network Setup
- Storage
- Roles
- Manager Instance
- Worker Launch Template
- Worker Instances
- Docker Swarm
- Cleanup (this post)
Cleanup
Docker Swarm
Login to manager machine
Switch user to the worker
user:
Remove all nodes from Swarm
Destroy Swarm
On manager machine:
Clean up dangling networks
After destroying the Swarm make sure that you do not have dangling networks ingress
and docker_gwbridge
:
If you see ingress
or docker_gwbridge
, force remove them:
Also you have to do the same for all nodes:
pssh --hosts=$HOME/nodes/hosts --inline 'docker network rm docker_gwbridge'
pssh --hosts=$HOME/nodes/hosts --inline 'docker network rm ingress'
Remove swift_default
network:
Docker Cleanup
These commands will remove all stopped containers and all images that do not have a running container on all nodes:
# stop and femove containers
pssh --timeout 300 --hosts=$HOME/nodes/hosts --inline \
'docker container prune --force'
# delete unused images
pssh --timeout 300 --hosts=$HOME/nodes/hosts --inline \
'docker image prune --all --force'
# check root file system
pssh --timeout 300 --hosts=$HOME/nodes/hosts --inline \
'df --human-readable /ebs/docker'
Route 53 Cleanup Script
Start in the project directory:
Create a folder route53
and a route53-delete-record.sh
file in it.
Copy and paste this code into route53-delete-record.sh
:
#!/usr/bin/env bash
# =============================================================================================================
# Usage:
# ./route53-delete-record.sh [HostedZoneName] [Hostname] [Type]
#
# Example:
# ./route53-delete-record.sh example.org dummy.example.org
# ./route53-delete-record.sh example.org dummy.example.org TXT
# ./route53-delete-record.sh example.org dummy.example.org txt
# ./route53-delete-record.sh example.org dummy.example.org CNAME
# =============================================================================================================
# output coloring
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CLEAR=$(tput sgr0)
# put your value here
# note that jq can work with env var
HOSTED_ZONE=${1:-example.org}
DNS_NAME=${2:-test.example.org}
DNS_TYPE=${3:-A}
[[ -z "$HOSTED_ZONE" ]] && HOSTED_ZONE=example.org
# add . to the end
DNS_NAME="$DNS_NAME."
# capitalize
DNS_TYPE=${DNS_TYPE^^}
echo Deleting record: \'$DNS_TYPE\' \'$DNS_NAME\' from hosted zone \'$HOSTED_ZONE\' ...
# find Zone ID
ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name $HOSTED_ZONE --output json \
| jq .HostedZones[].Id --raw-output \
| awk -F / '{print $3}')
if [[ -z "$ZONE_ID" ]]; then
echo ${RED}$HOSTED_ZONE hosted zone not found!$CLEAR
exit 1
fi
echo Zone ID: $YELLOW$ZONE_ID$CLEAR
echo
# find resource record set
RECORD_SETS=$(aws route53 list-resource-record-sets --hosted-zone-id=$ZONE_ID --output json \
| jq '.ResourceRecordSets[] | select ((.Name == '\"$DNS_NAME\"') and (.Type=='\"$DNS_TYPE\"'))')
if [[ -z "$RECORD_SETS" ]]; then
echo ${RED}$DNS_NAME $DNS_TYPE record not found!$CLEAR
exit 1
fi
echo Resource Record Sets:
jq <<< "$RECORD_SETS"
echo
# prepare the change batch value
CHANGE_BATCH=$(cat << EOF
{
"Comment": "delete this record",
"Changes": [
{
"Action": "DELETE",
"ResourceRecordSet":
$RECORD_SETS
}
]
}
EOF
)
echo Change batch:
jq <<< "$CHANGE_BATCH"
echo
# perform the deletion
aws route53 change-resource-record-sets --hosted-zone-id=$ZONE_ID --change-batch "$CHANGE_BATCH"
Make the script executable:
Delete AWS resources
Instances
Worker
Manager
IAM Roles / Instance Profiles
Worker
Manager
Elastic Block Storage (EBS)
Route 53
./route53/route53-delete-record.sh swift.internal manager.swift.internal
./route53/route53-delete-record.sh swift.internal worker-1.swift.internal
./route53/route53-delete-record.sh swift.internal worker-2.swift.internal
VPC
Congratulations!
We are done with Step 8. Cleanup
. This is the final step of this series.
Here are all the steps again for reference:
- Network Setup
- Storage
- Roles
- Manager Instance
- Worker Launch Template
- Worker Instances
- Docker Swarm
- Cleanup (this post)