Hello everyone,
I’m feeling really dumb but I can’t make a simple scenario working. I spent hours in the docs and on the web trying to find a solution but I give up!
I’m testing using a very simple scenario:
I want several WordPress docker images connecting to a single MySQL database. (later I’ll integrate Traefik but let’s not get ahead of ourselves)
I don’t understand how to make those two independent jobs communicate with each other. I’m sure I’m confused with the different networking layers.
I’m running the following on MacOS with consul agent -dev
and nomad agent -dev
Is sidecar the right tool for this?
Here are my 2 jobs:
Mysql:
job "mysql" {
datacenters = ["dc1"]
group "leader" {
network {
# Request for a static port
port "mysql" { to = 3306}
}
task "mysql" {
driver = "docker"
config {
image = "mysql"
ports=["mysql"]
}
env {
MYSQL_ROOT_PASSWORD = "root"
MYSQL_DATABASE = "wordpress"
MYSQL_USER = "wpuser"
MYSQL_PASSWORD = "wppass"
}
service {
name = "mysql"
tags = ["global"]
port = "mysql"
check {
name = "mysql ping"
type = "tcp"
interval = "30s"
timeout = "2s"
}
}
resources {
cpu = 500 #Mhz
memory = 512 #MB
}
}
}
}
Here is Wordpress:
job "wordpress" {
datacenters = ["dc1"]
group "wordpress" {
network {
port "http" { to = 80}
}
task "wordpress" {
driver = "docker"
config {
image = "wordpress"
ports=["http"]
}
env {
WORDPRESS_DB_HOST = "127.0.0.1:22100"
WORDPRESS_DB_NAME = "wordpress"
WORDPRESS_DB_USER = "wpuser"
WORDPRESS_DB_PASSWORD = "wppass"
}
service {
name = "wordpress"
port = "http"
check {
name = "500 error check"
type = "http"
protocol = "http"
path = "/"
interval = "30s"
timeout = "2s"
}
}
resources {
cpu = 500 # Mhz
memory = 256 # MB
}
}
}
}
Question #1, why is MySQL unreachable from the WordPress job even with the ADDR hardcoded?
I know the DB works as I’m able to access my host on 127.0.0.1:22100
Question #2 how do I get around making it dynamic so that of course, the MYSQL ports are not hardcoded?
I’ve setup DNS resolution according to the docs:
dig @localhost -p 8600 mysql.service.consul. SRV
; <<>> DiG 9.10.6 <<>> @localhost -p 8600 mysql.service.consul. SRV
; (2 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13424
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;mysql.service.consul. IN SRV
;; ANSWER SECTION:
mysql.service.consul. 0 IN SRV 1 1 22100 7f000001.addr.dc1.consul.
;; ADDITIONAL SECTION:
7f000001.addr.dc1.consul. 0 IN A 127.0.0.1
Patricks-MacBook-Pro.local.node.dc1.consul. 0 IN TXT "consul-network-segment="
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue May 18 11:27:46 EDT 2021
;; MSG SIZE rcvd: 177
If you kindly could point in the right direction on how to go about this, it’d be much appreciated!
Thank you