• Stars
    star
    155
  • Rank 232,987 (Top 5 %)
  • Language
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Initialize Docker Swarm with Ansible

ansible-docker-swarm

Say Thanks! Slack Status Chat on Slack

Setup Docker Swarm with Ansible.

In this setup I have a client node, which will be my jump box, as it will be used to ssh with the docker user to my swarm nodes with passwordless ssh access.

Pre-Check

Hosts file:

$ cat /etc/hosts
10.0.8.2 client
192.168.1.10 swarm-manager
192.168.1.11 swarm-worker-1
192.168.1.12 swarm-worker-2

SSH Config:

$ cat ~/.ssh/config 
Host client
  Hostname client
  User root
  IdentityFile /tmp/key.pem
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

Host swarm-manager
  Hostname swarm-manager
  User root
  IdentityFile /tmp/key.pem
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

Host swarm-worker-1
  Hostname swarm-worker-1
  User root
  IdentityFile /tmp/key.pem
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

Host swarm-worker-2
  Hostname swarm-worker-2
  User root
  IdentityFile /tmp/key.pem
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

Install Ansible:

$ apt install python-setuptools -y
$ easy_install pip
$ pip install ansible

Ensure passwordless ssh is working:

$ ansible -i inventory.ini -u root -m ping all
client | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
swarm-manager | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
swarm-worker-2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
swarm-worker-1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Deploy Docker Swarm

$ ansible-playbook -i inventory.ini -u root deploy-swarm.yml 
PLAY RECAP 

client                     : ok=11   changed=3    unreachable=0    failed=0   
swarm-manager              : ok=18   changed=4    unreachable=0    failed=0   
swarm-worker-1             : ok=15   changed=1    unreachable=0    failed=0   
swarm-worker-2             : ok=15   changed=1    unreachable=0    failed=0   

SSH to the Swarm Manager and List the Nodes:

$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
0ead0jshzkpyrw7livudrzq9o *   swarm-manager       Ready               Active              Leader              18.03.1-ce
iwyp6t3wcjdww0r797kwwkvvy     swarm-worker-1      Ready               Active                                  18.03.1-ce
ytcc86ixi0kuuw5mq5xxqamt1     swarm-worker-2      Ready               Active                                  18.03.1-ce

Create a Nginx Demo Service:

$ docker network create --driver overlay appnet
$ docker service create --name nginx --publish 80:80 --network appnet --replicas 6 nginx
$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
k3vwvhmiqbfk        nginx               replicated          6/6                 nginx:latest        *:80->80/tcp

$ docker service ps nginx
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
tspsypgis3qe        nginx.1             nginx:latest        swarm-manager       Running             Running 34 seconds ago                       
g2f0ytwb2jjg        nginx.2             nginx:latest        swarm-worker-1      Running             Running 34 seconds ago                       
clcmew8bcvom        nginx.3             nginx:latest        swarm-manager       Running             Running 34 seconds ago                       
q293r8zwu692        nginx.4             nginx:latest        swarm-worker-2      Running             Running 34 seconds ago                       
sv7bqa5e08zw        nginx.5             nginx:latest        swarm-worker-1      Running             Running 34 seconds ago                       
r7qg9nk0a9o2        nginx.6             nginx:latest        swarm-worker-2      Running             Running 34 seconds ago   

Test the Application:

$ curl -i http://192.168.1.10
HTTP/1.1 200 OK
Server: nginx/1.15.0
Date: Thu, 14 Jun 2018 10:01:34 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 05 Jun 2018 12:00:18 GMT
Connection: keep-alive
ETag: "5b167b52-264"
Accept-Ranges: bytes

Delete the Swarm:

$ ansible-playbook -i inventory.ini -u root delete-swarm.yml 

PLAY RECAP 
swarm-manager              : ok=2    changed=1    unreachable=0    failed=0   
swarm-worker-1             : ok=2    changed=1    unreachable=0    failed=0   
swarm-worker-2             : ok=2    changed=1    unreachable=0    failed=0   

Ensure the Nodes is removed from the Swarm, SSH to your Swarm Manager:

$ docker node ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

Thanks a lot to lucj, make sure to checkout his repo.

Stargazers over time

Stargazers over time

More Repositories

1

cheatsheets

My Cheatsheet Repository
Python
361
star
2

docker-monitoring-stack-gpnc

Grafana Prometheus Node-Exporter cAdvisor - Docker Monitoring Stack
Makefile
97
star
3

drone-gitea-on-docker

DroneCI and Gitea on Docker
Shell
46
star
4

terraformfiles

Terraform Examples
HCL
43
star
5

grafana-observability-primer

Grafana Observability Primer
Python
38
star
6

loki-alerts-docker

Alerting with Loki v2 on Docker Example
Shell
33
star
7

dockerfiles

Dockerfiles Repo
Dockerfile
27
star
8

docker-promtail-loki

Docker Compose Stack with Grafana Loki and Promtail
Makefile
27
star
9

awesome-list

My Awesome List of anything cool that my curious mind finds interesting
27
star
10

aws-terraform-cicd-java-springboot

Terraform: AWS CICD with CodePipeline, CodeBuild and ECS and a Springboot App
HCL
23
star
11

drone-ci-testing

Repository with Drone CI Examples
Go
19
star
12

golang-autoclicker

Auto Clicker in Golang to Simulate Mouse Clicks
Go
19
star
13

k3s-on-lxd

K3S on LXD
Shell
18
star
14

aws-serverless-url-shortener

Serverless URL Shortener on AWS with API Gateway, Lambda, DynamoDB and Python
Python
14
star
15

code-examples

Example Code that I use for Referencing when needed
Python
14
star
16

loki-docker-nginx-example

13
star
17

flask-reminders

App with Python Flask, MongoDB and Elasticsearch that I use to save URL's
HTML
10
star
18

woodpecker-ci-demo

woodpecker-ci-demo
Shell
10
star
19

terraform-packer-ansible-nginx-basic

Example of using Terraform, Packer and Ansible to Deploy Nginx
HCL
9
star
20

docker-loki-distributed-minio

Distributed Loki setup with BoltDB and Minio using Docker
Python
9
star
21

docker-glitchtip-traefik

Using Glitchtip on Dokcer with Traefik
Python
9
star
22

elasticsearch-demo

Tutorial on Getting Started with Elasticsearch
8
star
23

scripts

My Scripts Repository
Shell
8
star
24

mongodb-with-python-tutorial

MongoDB with Pymongo Tutorial
8
star
25

python-flask-app-kubernetes

Python Flask App Deploy to Kubernetes
Python
8
star
26

quick-starts

Collection of quick starts on docker, terraform, ansible, etc
HCL
7
star
27

k3m

Helper tool to run Rancher's k3s on Canonical's Multipass
Shell
6
star
28

learnanything-tech

Learn Anything in Tech
HTML
6
star
29

docker-logging-loki-fuentbit

Docker Container Logging to Loki with Fluent Bit
6
star
30

cryptocurrency-prices-grafana

Fetching Cryptocurrency Prices from Coingecko and Displaying them on Grafana
Python
6
star
31

loki-minio-docker

Basic example of Loki, Minio and BoltDB on Docker
Dockerfile
6
star
32

ansible-playbooks

Repo for Anisble Playbooks
Jinja
6
star
33

ruanbekker.github.io

My Blog on GitHub
HTML
5
star
34

traefik-kubernetes-scaleway-demo

Traefik as an Ingress Controller on Kubernetes
5
star
35

coingecko-prometheus-exporter

Prometheus Exporter to return CoinGecko Crypto Currency Results
Python
5
star
36

faas-coinmarketcap

FaaS Function for Getting info from CoinMarketCap API
Python
5
star
37

awesome-docker-compose

My Collection of Docker Compose Files
Makefile
4
star
38

terraform-docker-container-example

Docker Provider example using Terraform
HCL
4
star
39

bitcoin-full-node-demo

Setup and Usage of Bitcoin Core using the testnet chain using Docker
Shell
4
star
40

datadog-python-flask-example

Python Flask Example with Datadog
Python
4
star
41

thanos-cluster-setup

Multi-Node Thanos Cluster Setup
Shell
4
star
42

data-generation-scripts

a Home for my Data Generations Scripts
Python
4
star
43

docker-swarm-terraform-scaleway

Docker Swarm Cluster on Scaleway with Terraform
HCL
4
star
44

flask-url-shortener-ui

URL Shortener UI in Python Flask
HTML
4
star
45

grafana-tempo-loki-tracing

Grafana Distributed Tracing Example with: Tempo, Prometheus, Loki, Grafana and Python Flask
Python
3
star
46

docker-swarm-prometheus-grafana

Prometheus, Grafana, Node Exporter, cAdvisor Stack for Docker Swarm
Shell
3
star
47

drone-gitea-traefik-docker-blogpost

Blogpost on Drone Server and Drone Runner for Gitea on Docker
Shell
3
star
48

argocd-workshop

Experimenting with ArgoCD
Smarty
3
star
49

blog-assets

Code, Snippets, Files from Blogposts that I mention
Shell
3
star
50

monitoring-grafana-prometheus-local

Local DNS Stack: Grafana with Prometheus and Traefik using localdns
Python
3
star
51

flask-mysql-guestbook

Python Flask and MySQL
Roff
3
star
52

awesome-cheap-hosting

Awesome List with Cheap Hosting Providers
3
star
53

concourse-swarm-app-demo

Concourse with Docker Swarm Demo
Shell
3
star
54

drone-localstack-terraform

Using Terraform to provision Mock Infra with Localstack on DroneCI
HCL
3
star
55

terraform-ansible-vultr

Using Terraform and Ansible for Vultr Instances
HCL
3
star
56

kubernetes-hands-on-demo

Kubernetes Hands-On Demo
Python
2
star
57

project-management-demo

Managing Day-to-Day things with Github
2
star
58

awesome-learning

Repository with Curated Learning Content
2
star
59

ansible-playground

Collection of Ansible Resources
2
star
60

ssh-reverse-tunnel

Tool to Reach Servers with No Public Route
2
star
61

bash-setup

Setup for Bash Terminals / Shells
Vim Script
2
star
62

ansible-macbook-setup

Installs homebrew packages via Ansible
2
star
63

python-flask-examples

Repository with Python Flask Examples
HTML
2
star
64

multipassfiles

Collection of Multipass Setups
Makefile
2
star
65

rbkr-cli

Personal CLI Project to learn Go
Go
2
star
66

flask-sessions-demo

Python Flask Sessions Demo
HTML
2
star
67

webhook-for-alertmanager

Basic Python Flask Webhook for Alertmanager
Python
2
star
68

docker-remote-tunnel

Run Remote Docker commands via a SSH Tunnel
Shell
2
star
69

devopstools-container

Container Image with all your Favourite DevOps CLI Tools
Dockerfile
2
star
70

terraform-lxd-example

Terraform with LXD Example
HCL
2
star
71

golang-build-small-images

Demo of Building Small Golang Images
Go
2
star
72

docker-java-springboot-hello-world

Hello World example with Java Springboot and Docker
Java
1
star
73

docker-java-springboot-bookstore-api

Example: Java Spring Boot Bookstore API with Docker, MySQL and Liquibase
Java
1
star
74

grafana-dashboards-grafonnet-demo

Example generating Grafana Dashboards as Code using Grafonnet
Jsonnet
1
star
75

flask-api-structured-primer

Structured Flask API with SQLAlchemy, Swagger, Unit Tests, CodeCoverage, etc
Python
1
star
76

docker-raneto-knowledge-base

Raneto is a Knowledgebase platform for Node.js that uses static Markdown files to power the knowledgebase
Dockerfile
1
star
77

nodejs-jenkins-demo

CI/CD with Jenkins and Nodejs
JavaScript
1
star
78

docker-orangescrum

OrangeScrum on Docker
PHP
1
star
79

docker-selfhosted-server

My Hobby Server for Self-Hosted Applications on Docker
1
star
80

discord-minecraft-python-bot

Discord Bot in Python
Python
1
star
81

links

CSS
1
star
82

traefik-armhf

Traefik on ARM
Dockerfile
1
star
83

concourse-tutorial

Concourse Tutorial
Shell
1
star
84

telegram-bots-collection

Collection of Telegram Bots in Mainly Python
Python
1
star
85

mongodb-to-elasticsearch

Script to Ingest Data from MongoDB to Elasticsearch
Python
1
star
86

docker-php-nginx-mysql

Nginx, PHP, MySQL Stack on Docker
PHP
1
star
87

docker-htpc-media-server

HTPC with Docker Compose
Shell
1
star
88

awesome-tools

awesome-tools
1
star
89

python-image-encoder

Python Script to Encode Images to Base64 and Stores them into MongoDB and Redis
1
star
90

docker-swarm-pistack

Repository for my Raspberry Pi Docker Swarm Stack
1
star
91

twitter-python-sentiment-analysis-elk

Sentiment Analysis with Python, Twitter and ELK
Python
1
star
92

docker-apache-airflow

Apache Airflow using Docker Compose
Python
1
star
93

docker-maxwell-daemon-streaming

Maxwell Daemon Streaming with Docker Compose
Shell
1
star
94

go-hostname

Basic Go Web Application to return the hostname
Go
1
star
95

docker-bitcoind

Bitcoin Core running on Docker with Prometheus and Grafana
Dockerfile
1
star
96

python-web-scraper

Web Scraper that Scrapes my Blogs and Ingest the Data into Elasticsearch
Python
1
star
97

terraform-localstack-examples

Terraform on Localstack Examples
HCL
1
star
98

bitcoin-testnet-terraform-ansible

Terraform and Ansible to provision a Bitcoin Testnet on AWS
1
star
99

awesome-selfhosted

My List of Awesome Self-Hosted Applications
1
star
100

terraform-ansible-aws-prometheus

Setup Prometheus with Thanos Sidecar on AWS using Terraform and Ansible
HCL
1
star