您如何使用Hashicorp的Nomad'模板节'通过Nomad作业文件生成nginx配置文件?

时间:2017-03-23 00:16:49

标签: nginx docker consul consul-template nomad

假设Consul和Nomad已配置为在资源池上运行。如何渲染模板文件的唯一目的是生成例如一个Nginx'default.conf'文件。

使用下面的模板节配置,作为示例; Nomad无法生成default.conf'文件';而是创建default.conf'目录'。

Toggle

我要么错过了一个技巧,要么误解了'模板节'的功能。

模板生成目录而不是文件的问题之一是,您无法将目录挂载到配置文件路径。因此,运行使用Nomad docker驱动程序和示例“docker”卷配置的任务会导致错误。

template {
    source        = "/path/to/tmp.ctmpl"
    destination   = "folder/default.conf"
    change_mode   = "restart"
    change_signal = "SIGINT"
}

或者模板节是否无法生成配置文件?

* P.S。使用Nomad build 0.5.5 **

3 个答案:

答案 0 :(得分:4)

我只是把一个小的Nomad工作放在一起显示这个工作,所以你可能会有轻微的配置错误。为了让您自己运行这项工作,我已将其作为gist here提供。在同一个要点中,我有一个nginx.conf,它有nginx监听Nomad作业文件中的任何端口。

这是Nomad工作:

job "nginx" {
  datacenters = ["dc1"]
  type = "service"
  group "cache" {
    count = 1
    task "redis" {
      driver = "docker"
      config {
        image = "nginx:1.11.10"
        volumes = ["new/default.conf:/etc/nginx/conf.d/default.conf" ]
        network_mode = "host"
      }

      artifact {
        source = "https://gist.githubusercontent.com/dadgar/2dcf68ab5c49f7a36dcfe74171ca7936/raw/c287c16dbc9ddc16b18fa5c65a37ff25d2e0e667/nginx.conf"
      }

      template {
        source        = "local/nginx.conf"
        destination   = "new/default.conf"
        change_mode   = "restart"
      }

      resources {
        network {
          mbits = 10
          port "nginx" {
            static = 8080
          }
        }
      }
    }
  }
}

然后我可以查询该地址并看到nginx绑定到该端口,因此正在挂载的模板正常工作。

$ curl http://127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

如果您查看要点,我会显示正在渲染和正确安装的文件。

希望这对你有所帮助!另外,请务必查看community page以获取帮助。我们有Gitter房间和邮件列表。

答案 1 :(得分:0)

localsecrets文件夹中生成的配置放在作业的工作目录(alloc目录)中要容易得多。这些文件夹在容器中可用作/ secrets和/ local。无需安装卷。

答案 2 :(得分:0)

最终基于以下错误遵循上述解决方案:

“您是否尝试将目录挂载到文件上(反之亦然)?检查指定的主机路径是否存在并且是预期的类型”

以下代码片段是我如何将 nginx 作为我的 jenkins 实例(所有基于游牧者)的反向代理。此模板是多任务作业的一部分,由 jenkins 和 nginx 组成。

希望这对我过去几天处于相同情况的其他人有用。

task "nginx" {

  driver = "docker"

  resources {
    cpu    = 75 
    memory = 75
  }   

  service {
    name = "jenkins-nginx"
    tags = ["urlprefix-/jenkins-nginx"]
    port = "http" 
    check {
      name     = "nginx port alive"
      type     = "http"
      path     = "/login"
      interval = "10s"
      timeout  = "2s"
    }   
  }   

      template {
        change_mode     = "restart"
        destination     = "local/default.conf"
        data = <<EOH
    upstream jenkins {
      server {{ env "NOMAD_ADDR_jenkins" }}; 
    }   
    server {
        listen {{ env "NOMAD_PORT_http" }}; 

        location / { 
               proxy_redirect              off;
               proxy_pass_header           Server;
               proxy_set_header            X-Real-IP $remote_addr;
               proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header            Host $http_host;
               proxy_set_header            X-NginX-Proxy true;
               proxy_set_header            X-Accel-Buffering no; 
               proxy_connect_timeout       5;  
               proxy_http_version          1.1;
               proxy_read_timeout          240;
               proxy_intercept_errors      on; 
               keepalive_timeout           3600;
               proxy_set_header Connection ''; 
               chunked_transfer_encoding   off;
               proxy_buffering             off;
               proxy_cache                 off;
               proxy_pass                  http://jenkins;
        }   
    }   
EOH
      }   

      config {
        image = "nginx"

        network_mode = "host"
        ports = ["http","https"]

        volumes = [ "local/default.conf:/etc/nginx/conf.d/default.conf" ]
      }   
    }