Skip to content

Instantly share code, notes, and snippets.

@Mayeu
Last active December 1, 2022 22:35
Show Gist options
  • Save Mayeu/9a5ad12d009c45b8a3fc2436f7e71004 to your computer and use it in GitHub Desktop.
Save Mayeu/9a5ad12d009c45b8a3fc2436f7e71004 to your computer and use it in GitHub Desktop.
Swarm Mastery Using Terraform - Assignement 1

Swarm Mastery Using Terraform - Assignement 1

Edit the providers section in the main.tf file to correctly connect to your Swarm

Then run me with:

$ terraform init
$ terraform plan
$ terraform apply

Ports 80, 8080, and 5001 are published on all node of the swarm.

provider "docker" {
host = "<put your host here>"
}
# Volumes
resource "docker_volume" "db-data" {
name = "db-data"
}
# Networks
resource "docker_network" "backend" {
name = "backend"
driver = "overlay"
}
resource "docker_network" "frontend" {
name = "frontend"
driver = "overlay"
}
# The vote app
resource "docker_service" "vote" {
name = "vote"
task_spec {
container_spec {
image = "bretfisher/examplevotingapp_vote"
}
networks = [docker_network.frontend.id]
}
mode {
replicated {
replicas = 2
}
}
endpoint_spec {
mode = "vip"
ports {
target_port = "80"
published_port = "80"
publish_mode = "ingress"
}
}
}
# The Redis app
resource "docker_service" "redis" {
name = "redis"
task_spec {
container_spec {
image = "redis:3.2"
}
networks = [docker_network.frontend.id]
}
mode {
replicated {
replicas = 1
}
}
}
# The worker
resource "docker_service" "worker" {
name = "worker"
task_spec {
container_spec {
image = "bretfisher/examplevotingapp_worker:java"
}
networks = [docker_network.frontend.id, docker_network.backend.id]
}
mode {
replicated {
replicas = 1
}
}
}
# The DB
resource "docker_service" "db" {
name = "db"
task_spec {
container_spec {
image = "postgres:9.4"
env = {
POSTGRES_HOST_AUTH_METHOD = "trust"
}
mounts {
target = "/var/lib/postgresql/data"
source = docker_volume.db-data.name
type = "volume"
}
}
networks = [docker_network.backend.id]
}
mode {
replicated {
replicas = 1
}
}
}
# The result
resource "docker_service" "result" {
name = "result"
task_spec {
container_spec {
image = "bretfisher/examplevotingapp_result"
}
networks = [docker_network.backend.id]
}
mode {
replicated {
replicas = 1
}
}
endpoint_spec {
mode = "vip"
ports {
target_port = "80"
published_port = "5001"
publish_mode = "ingress"
}
}
}
# viz
resource "docker_service" "viz" {
name = "viz"
task_spec {
container_spec {
image = "dockersamples/visualizer"
mounts {
target = "/var/run/docker.sock"
source = "/var/run/docker.sock"
type = "bind"
}
}
placement {
constraints = [
"node.role==manager",
]
}
}
mode {
replicated {
replicas = 1
}
}
endpoint_spec {
mode = "vip"
ports {
target_port = "8080"
published_port = "8080"
publish_mode = "ingress"
}
}
}
terraform {
required_providers {
docker = {
source = "terraform-providers/docker"
version = "~> 2.7.2"
}
}
required_version = ">= 0.13"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment