Skip to content

Commit

Permalink
Move MySQl from blue cluster to green cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
lens0021 committed Oct 6, 2024
1 parent 6e3078a commit 95d9a89
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 64 deletions.
25 changes: 5 additions & 20 deletions jobs/fastcgi.nomad
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@ variable "green" {
default = false
}

variable "blue_nomad_private_ip" {
type = string
default = ""
}

variable "mysql_password_mediawiki" {
type = string
default = ""
}

variable "green_include_mysql" {
type = bool
description = "Whether connect to the MySQL in the same Nomad cluster. Only effective when `green` is true"
default = false
}

locals {
blue = !var.green
}
Expand Down Expand Up @@ -67,8 +56,7 @@ job "fastcgi" {
network_mode = "host"
args = [
"-c",
var.green_include_mysql ? "echo -n 'Waiting for service'; until nslookup mysql.service.consul 127.0.0.1:8600 2>&1 >/dev/null; do echo '.'; sleep 2; done"
: "echo -n 'Waiting for service'; until nc -z ${var.blue_nomad_private_ip} 3306 < /dev/null; do echo '.'; sleep 2; done",
"echo -n 'Waiting for service'; until nslookup mysql.service.consul 127.0.0.1:8600 2>&1 >/dev/null; do echo '.'; sleep 2; done"
]
}

Expand Down Expand Up @@ -248,7 +236,7 @@ job "fastcgi" {
MEDIAWIKI_SKIP_IMPORT_SITES = "1"
MEDIAWIKI_SKIP_UPDATE = "1"

WG_DB_SERVER = var.green_include_mysql ? NOMAD_UPSTREAM_ADDR_mysql : "${var.blue_nomad_private_ip}:3306"
WG_DB_SERVER = NOMAD_UPSTREAM_ADDR_mysql
WG_DB_USER = "mediawiki"
WG_DB_PASSWORD = var.mysql_password_mediawiki
}
Expand All @@ -266,12 +254,9 @@ job "fastcgi" {
sidecar_service {
proxy {

dynamic "upstreams" {
for_each = var.green_include_mysql ? [{}] : []
content {
destination_name = "mysql"
local_bind_port = 3306
}
upstreams {
destination_name = "mysql"
local_bind_port = 3306
}

upstreams {
Expand Down
4 changes: 3 additions & 1 deletion terraform/backupbot.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
resource "nomad_job" "backupbot" {
depends_on = [nomad_job.mysql]
provider = nomad.green
count = 0
depends_on = [nomad_job.mysql_green]

detach = false
jobspec = file("../jobs/backupbot.nomad")
Expand Down
19 changes: 0 additions & 19 deletions terraform/mediawiki.tf
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
resource "nomad_job" "mysql" {
depends_on = [
data.nomad_plugin.ebs,
nomad_csi_volume_registration.mysql,
]

jobspec = file("../jobs/mysql.nomad")
detach = false

hcl2 {
allow_fs = true
}
}

resource "nomad_job" "mysql_green" {
provider = nomad.green
count = 0
depends_on = [
nomad_csi_volume_registration.mysql_green,
]
Expand Down Expand Up @@ -54,7 +39,6 @@ resource "nomad_job" "memcached_green" {

resource "nomad_job" "fastcgi" {
depends_on = [
nomad_job.mysql,
nomad_job.memcached,
]

Expand All @@ -79,16 +63,13 @@ resource "nomad_job" "fastcgi_green" {
allow_fs = true
vars = {
green = true
blue_nomad_private_ip = data.terraform_remote_state.aws.outputs.nomad_private_ip
mysql_password_mediawiki = var.mysql_password_mediawiki
green_include_mysql = false
}
}
}

resource "nomad_job" "http" {
depends_on = [
data.nomad_plugin.ebs,
nomad_csi_volume_registration.caddycerts,
]

Expand Down
24 changes: 0 additions & 24 deletions terraform/volumes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,8 @@ resource "nomad_job" "plugin_ebs_nodes_green" {
}
}

data "nomad_plugin" "ebs" {
plugin_id = "aws-ebs0"
wait_for_healthy = true
}

import {
id = "mysql@default"
to = nomad_csi_volume_registration.mysql
}
resource "nomad_csi_volume_registration" "mysql" {
depends_on = [data.nomad_plugin.ebs]
plugin_id = "aws-ebs0"
volume_id = "mysql"
name = "mysql"
external_id = data.terraform_remote_state.aws.outputs.ebs_mysql_id

capability {
access_mode = "single-node-writer"
attachment_mode = "file-system"
}
}

resource "nomad_csi_volume_registration" "mysql_green" {
provider = nomad.green
count = 0
plugin_id = "aws-ebs0"
volume_id = "mysql_green"
name = "mysql_green"
Expand All @@ -73,7 +50,6 @@ import {
to = nomad_csi_volume_registration.caddycerts
}
resource "nomad_csi_volume_registration" "caddycerts" {
depends_on = [data.nomad_plugin.ebs]
plugin_id = "aws-ebs0"
volume_id = "caddycerts"
name = "caddycerts"
Expand Down

0 comments on commit 95d9a89

Please sign in to comment.