Container dependency in job file

595 views
Skip to first unread message

Denriful

unread,
Jun 7, 2017, 12:43:29 AM6/7/17
to Nomad
I would like to run some service in nomad cluster that requires database connection. For simple example - Wordpress.
In docker-compose we can link two docker instances (frontend + db) via "depends_on" directive.
But how this can be accomplished in nomad job file?
Just a sample docker-compose that i need to convert to nomad job:
version: 2

services:

  db:

    image: mysql:5.7

  volumes:

    db_data:/var/lib/mysql

  restart: always

  environment:

    MYSQL_ROOT_PASSWORD: wordpress

    MYSQL_DATABASE: wordpress

    MYSQL_USER: wordpress

    MYSQL_PASSWORD: wordpress

  wordpress:

   depends_on:

      db

   image: wordpress:latest

  ports:

  8000:80

  restart: always

  environment:

    WORDPRESS_DB_HOST: db:3306

    WORDPRESS_DB_PASSWORD: wordpress

volumes:

  db_data:


msch...@hashicorp.com

unread,
Jun 7, 2017, 1:37:11 PM6/7/17
to Nomad
In Nomad you would probably use Consul to discover the address of the database and Vault to retrieve the database credentials. A Wordpress job file might look something like:

job "wordpress" {
  datacenters = ["dc1"]
  group "wordpress" {
    # Scale count to handle your expected traffic
    count = 2

    task "wordpress" {
      driver = "docker"
      config {
        image = "wordpress:latest"
        port_map {
          http = 80
        }
      }

      resources {
        cpu    = 500 # 500 MHz
        memory = 256 # 256MB
        network {
          mbits = 10
          port "http" {
            static = "8000"
          }
        }
      }

      service {
        name = "wordpress"
        port = "http"
        check {
          name     = "alive"
          type     = "http"
          interval = "10s"
          timeout  = "2s"
        }
      }

      # env = true requires Nomad  0.6
      template {
        data          = <<EOF
WORDPRESS_DB_HOST={{ key "service/mysql" }}
{{ with secret "database/creds/mysql" }}
WORDPRESS_DB_USER={{ .Data.username }}
WORDPRESS_DB_PASSWORD={{ .Data.password }}
EOF
        change_mode   = "restart"
        env           = true
      }

      vault {
        policies      = ["database"]
        change_mode   = "signal"
        change_signal = "SIGHUP"
      }
    }
  }
}

Note that to configure Wordpress via environment variables requires some recent additions to Nomad, so you'll either need to build from master or wait for the upcoming 0.6 release. You could use the env stanza to hardcode the username and password, but discourage that because it's hard to keep that secure.

If you can alter the Docker image to read from a templated configuration file, you can write that from Nomad today using the template stanza: https://www.nomadproject.io/docs/job-specification/template.html

It's more involved to setup Wordpress+MySQL in Nomad, but you have much greater flexibility and a lot more features available.

Hope that helps!

Abhishek Das

unread,
Mar 22, 2018, 3:45:50 PM3/22/18
to Nomad
I have similar kind of situation where I have Rabbitmq server running on docker and an java app requires IP of rabbitmq in its configuration. I am struggling to figure out that how do I pass on Rabbitmq IP to this config file. From the mentioned example I understand we can use template, however how are we storing the key service/mysql in Consul? Is it auto registered from Mysql job or do we need to manually update it? 

Regards,
Abhishek

Chelsea Komlo

unread,
Mar 26, 2018, 8:26:41 AM3/26/18
to Abhishek Das, Nomad
Hi Abishek,

You can use a script to submit the Nomad job. This script could first query the Consul API and inject this value as an environment variable to the Nomad job.

See https://www.nomadproject.io/docs/runtime/interpolation.html for more information about interpolated values in Nomad.

Let us know how else we can help,
Chelsea

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/nomad/issues
IRC: #nomad-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Nomad" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nomad-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nomad-tool/29c65574-791f-4edf-b271-07b4a63de7a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages