【问题标题】:Pre-deploying Kubernetes loadbalancer with terraform on DigitalOcean?在 DigitalOcean 上使用 terraform 预部署 Kubernetes 负载均衡器?
【发布时间】:2019-07-04 03:43:57
【问题描述】:

我正在学习如何使用 terraform 在 DO 上创建 k8s 集群,我一直在尝试获取我创建的单个 K8s 节点的 ID,并从负载均衡器中引用它。

这样做的主要原因是我可以在.tf 文件中声明 FQDN。

首先,这里是集群声明:


variable "digitalocean_token" {}

provider "digitalocean" {
  token = "${var.digitalocean_token}"
}

resource "digitalocean_kubernetes_cluster" "foo" {
  name    = "foo"
  region  = "nyc1"
  version = "1.12.1-do.2"

  node_pool {
    name       = "woker-pool"
    size       = "s-1vcpu-2gb"
    node_count = 1
  }
}

这里是负载均衡器声明:

resource "digitalocean_loadbalancer" "foo" {
  name = "k8s-lb.nyc1"
  region = "nyc1"

  forwarding_rule {
    entry_port = 80
    entry_protocol = "http"

    target_port     = 80
    target_protocol = "http"
  }
  droplet_ids = ["${digitalocean_kubernetes_cluster.foo.node_pool.0.id}"]
}

output "loadbalancer_ip" {
  value = "${digitalocean_loadbalancer.foo.ip}"
}

resource "digitalocean_record" "terraform" {
  domain = "example.com" # "${digitalocean_domain.example.name}"
  type   = "A"
  name   = "terraform"
  value  =  "${digitalocean_loadbalancer.foo.ip}"
}

# Output the FQDN for the record
output "fqdn" {
  value = "${digitalocean_record.terraform.fqdn}"
}

我猜digitalocean_loadbalancer 资源可能仅设置为与单个液滴一起使用?


这里是输出错误:当我运行terraform apply:

* output.loadbalancer_ip: Resource 'digitalocean_loadbalancer.foo' not found for variable 'digitalocean_loadbalancer.foo.ip'

* digitalocean_record.terraform: Resource 'digitalocean_loadbalancer.foo' not found for variable 'digitalocean_loadbalancer.foo.ip'

* digitalocean_loadbalancer.foo: droplet_ids.0: cannot parse '' as int: strconv.ParseInt: parsing "d4292e64-9c0a-4afb-83fc-83f239bcb4af": invalid syntax

铂。 2

我添加了一个 digitalocean_droplet 资源,以查看传递给负载均衡器的 id 类型。

resource "digitalocean_droplet" "web" {
  name      = "web-1"
  size      = "s-1vcpu-1gb"
  image     = "ubuntu-18-04-x64"
  region    = "nyc1"
}

digitalocean_kubernetes_cluster.foo.node_pool.0.id = '6ae6a787-d837-4e78-a915-cb52155f66fe'

digitalocean_droplet.web.id = 132533158

【问题讨论】:

  • 这些错误消息引用了 k8s-foo 资源名称,您提供的配置中没有这些名称。请更新配置以匹配导致这些错误的原因。
  • 修正了错字并添加了一些额外的研究项目。

标签: kubernetes load-balancing terraform digital-ocean


【解决方案1】:

因此,digitalocean_loadbalancer 资源有一个可选的droplet_tag 参数,可用于为创建的节点/液滴提供一个通用标签。

但是,当在 Kubernetes 中声明负载均衡器时,仍会创建一个新的负载均衡器。因此,至少目前看来,在 digitalocean 上无法使用 terraform 定义域/CNAME 记录

【讨论】:

    【解决方案2】:

    您为负载均衡器 droplet id 使用了错误的属性引用。

    droplet_ids = ["${digitalocean_kubernetes_cluster.foo.node_pool.0.id}"]

    这将使用node_pool id 链接here

    你真正需要做的是使用node_pool nodes id,它被引用here

    droplet_ids = "${digitalocean_kubernetes_cluster.foo.node_pool.0.nodes}"
    

    您将遇到的下一个问题是这会返回一个地图列表,您需要从中构建一个 id 列表。恐怕我目前不确定如何解决这个问题,但这应该会推动你前进。

    但是,从您的回答看来,您想要做的是为您的负载均衡器更新 DNS。

    您可以使用digitalocean provider 执行此操作external-dns

    只需将其部署为 pod,指定所需的配置,并确保设置了参数 --source=service

    如果您想更进一步,并允许使用特定主机名更新 DNS,请部署一个入口控制器(如 nginx-ingress)并为您的应用程序指定入口。 external-dns 部署(如果您设置了--source=ingress)将从您的入口获取主机名并为您更新 DNS。

    【讨论】:

    • 谢谢,我刚刚发现使用普通的droplet标签是最简单的方法,但是,k8s与这里声明的LB没有关系。
    • 您最初的问题似乎没有包括您实际尝试做的事情 - 这似乎是为使用负载均衡器部署的应用程序更新 DNS,对吧?我已更新我的答案以包含该信息。
    猜你喜欢
    • 2019-05-27
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多