作为更大模块的一部分,我想部署一个nginx
容器并替换其默认的nginx.conf
。新配置应使用在部署时生成的Terraform resources '数据构建。有办法吗?
答案 0 :(得分:1)
按照以下步骤,我设法用动态生成的nginx.conf替换了标准nginx.conf:
template_file
数据源 逐步:
创建名为 nginx-conf.tpl 的nginx.conf模板:
events {
worker_connections 4096; ## Default: 1024
}
http {
server {
listen 80;
listen [::]:80;
server_name ${server_name};
location /_plugin/kibana {
proxy_pass https://${elasticsearch_kibana_endpoint};
}
location / {
proxy_pass https://${elasticsearch_endpoint};
}
}
}
使用以下Terraform代码解析 nginx-conf.tpl 模板:
data "template_file" "nginx" {
template = "${file("${path.module}/nginx-conf.tpl")}"
vars = {
elasticsearch_endpoint = "${aws_elasticsearch_domain.example-name.endpoint}"
elasticsearch_kibana_endpoint = "${aws_elasticsearch_domain.example-name.kibana_endpoint}"
server_name = "${var.server_name}"
}
}
创建一个ConfigMap并使用nginx.conf
键将解析的模板存储在那里:
resource "kubernetes_config_map" "nginx" {
metadata {
name = "nginx"
}
data = {
"nginx.conf" = data.template_file.nginx.rendered
}
}
最后,将ConfigMap密钥安装为容器卷:
# ...
spec {
# ...
container {
# ...
volume_mount {
name = "nginx-conf"
mount_path = "/etc/nginx"
}
}
volume {
name = "nginx-conf"
config_map {
name = "nginx"
items {
key = "nginx.conf"
path = "nginx.conf"
}
}
}
}
# ...
就是这样。 Nginx服务器将开始使用提供的配置。
有用的链接:Kubernetes ConfigMap as volume,Terraform temple_file data source doc。