首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Hashicorp的Nomad 'template节‘通过Nomad作业文件生成nginx配置文件?

如何使用Hashicorp的Nomad 'template节‘通过Nomad作业文件生成nginx配置文件?
EN

Stack Overflow用户
提问于 2017-03-23 00:16:49
回答 4查看 8.8K关注 0票数 5

假设领事和Nomad被配置为运行在一个资源池上。如何将模板文件呈现为生成例如Nginx 'default.conf‘文件的唯一目的。

例如,使用下面的模板节配置;Nomad无法生成一个default.conf‘文件’;相反,创建了一个default.conf‘目录’。

代码语言:javascript
复制
template {
    source        = "/path/to/tmp.ctmpl"
    destination   = "folder/default.conf"
    change_mode   = "restart"
    change_signal = "SIGINT"
}

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

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

代码语言:javascript
复制
volumes = ["/path/to/job/folder/default.conf:/etc/nginx/conf.d/default.conf" ]

或者不可能让模板节生成一个配置文件?

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-24 06:06:34

我刚刚整理了一个小Nomad作业,显示这个工作,所以您可能有一个轻微的配置错误。为了让你自己运行这个工作,我已经把它作为一个要点在这里来使用。在相同的要点中,我有一个nginx.conf,它让nginx侦听Nomad作业文件中的任何端口。

这是游牧民族的工作:

代码语言:javascript
复制
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绑定到该端口,因此正在挂载的模板工作正常。

代码语言:javascript
复制
$ 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>

如果您查看gist,我将显示正在呈现和正确挂载的文件。

希望这对你有帮助!另外,一定要查看社区网页以获得帮助。我们有一间吉特房间和一张邮寄名单。

票数 10
EN

Stack Overflow用户

发布于 2018-03-16 01:16:32

只需将在localsecrets文件夹中生成的配置放在作业的工作目录(alloc目录)中就容易多了。这些文件夹在容器中可以作为/secrets和/local使用。不需要安装卷。

票数 2
EN

Stack Overflow用户

发布于 2021-07-02 09:54:20

最后,根据以下错误执行上述解决方案:

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

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

希望在过去的几天里,这将对处于同样情况的其他人有所帮助。

代码语言:javascript
复制
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" ]
      }   
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42965004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档