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

dev*_*a.y 3 nginx docker consul consul-template nomad

假设 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"
}
Run Code Online (Sandbox Code Playgroud)

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

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

volumes = ["/path/to/job/folder/default.conf:/etc/nginx/conf.d/default.conf" ]
Run Code Online (Sandbox Code Playgroud)

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

*Ps 使用 Nomad 构建 0.5.5 **

小智 7

我只是整理了一个小 Nomad 工作来展示这个工作,所以你可能会有一个轻微的配置错误。为了让您自己运行该作业,我在此处将其作为要点提供。在同一个要点中,我有一个 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
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我可以查询该地址并查看 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>
Run Code Online (Sandbox Code Playgroud)

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

希望这对你有帮助!另外一定要查看社区页面以获得帮助。我们有一个 Gitter 房间和一个邮件列表。