您将如何使用 Hashicorp 的 Nomad 'template stanza' 通过 Nomad 作业文件生成 nginx 配置文件?

How would you use Hashicorp's Nomad 'template stanza' to generate an nginx config file through the Nomad job file?

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

以下面的模板节配置为例; Nomad 无法生成 default.conf 'file';而是创建 default.conf 'directory'。

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

我要么漏掉了一个技巧,要么误解了 'template stanza' 的功能。

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

volumes = ["/path/to/job/folder/default.conf:/etc/nginx/conf.d/default.conf" ]

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

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

我只是整理了一个小的 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 房间和一个邮件列表。

将生成的配置放在作业工作目录(alloc 目录)的 localsecrets 文件夹中要容易得多。这些文件夹将在容器中以 /secrets 和 /local 的形式提供。不需要卷挂载。

基于以下错误结束了上述解决方案:

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

下面的代码片段是我如何让 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" ]
      }   
    }