【问题标题】:How would you use Hashicorp's Nomad 'template stanza' to generate an nginx config file through the Nomad job file?您将如何使用 Hashicorp 的 Nomad 'template stanza' 通过 Nomad 作业文件生成 nginx 配置文件?
【发布时间】:2017-08-15 08:32:08
【问题描述】:

假设 Consul 和 Nomad 已配置为在资源池上运行。您将如何仅出于生成目的而呈现模板文件,例如Nginx 'default.conf' 文件。

以下面的模板节配置为例; Nomad 无法生成 default.conf '文件';而是创建了一个 default.conf '目录'。

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

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

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

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

还是不能让模板节生成配置文件?

*附注使用 Nomad 构建 0.5.5**

【问题讨论】:

    标签: nginx docker consul consul-template nomad


    【解决方案1】:

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

    【讨论】:

    • 感谢您的示例,它按预期工作。另外,好点,我会检查一下gitter room以备将来查询。
    【解决方案2】:

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

    【讨论】:

    • 怎么做呢?把配置放在local里面?我的.nomad 文件旁边有配置文件。
    【解决方案3】:

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

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

    下面的 sn-p 是我如何让工作 nginx 作为我的 jenkins 实例的反向代理(全部基于 nomad)。此模板是多任务作业的一部分,由 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" ]
          }   
        }
    

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 2023-02-18
      • 2021-01-22
      • 1970-01-01
      • 2018-03-06
      • 2022-06-16
      • 2021-12-09
      • 2020-07-01
      • 2020-02-26
      相关资源
      最近更新 更多