如何使用 Terraform 数据块查找 AKS 外部 IP
how to find AKS external ip using terraform data block
我正在尝试在 k8s 中获取外部 IP 入口。有没有办法从地形数据块中获取详细信息。比如使用数据“azurerm_kubernetes_cluster”之类的?
我想到的解决方案(我确定这不是理想的解决方案)是使用 local-exec
,然后使用 kubectl 查询入口资源 in-cluster 以获取IP.
类似这样的事情(注意:还没有测试过,我也不使用 AKS,所以我不确定它是否会按预期工作)
resource "null_resource" "example1" {
provisioner "local-exec" {
command = "kubectl get ingress name-of-ingress-controller-lb | jq .status.loadBalancer.ingress[0].ip"
}
}
您可以使用 terraform 预先创建 Public IP 并将此 IP 分配给您的入口服务:
YAML:
apiVersion: v1
kind: Service
metadata:
annotations:
service.beta.kubernetes.io/azure-load-balancer-resource-group: myResourceGroup # only needed if the LB is in another RG
name: ingress-nginx-controller
spec:
loadBalancerIP: <YOUR_STATIC_IP>
type: LoadBalancer
相同但 Terraform 代码:
resource "kubernetes_service" "ingress_nginx" {
metadata {
name = "ingress-nginx-controller"
annotations {
"service.beta.kubernetes.io/azure-load-balancer-resource-group" = "${azurerm_resource_group.YOUR_RG.name}"
}
spec {
selector = {
app = <PLACEHOLDER>
}
port {
port = <PLACEHOLDER>
target_port = <PLACEHOLDER>
}
type = "LoadBalancer"
load_balancer_ip = "${azurerm_public_ip.YOUR_IP.ip_address}"
}
}
另一种解决方案是使用 DNS provider's dns_a_record_set
数据源在构建时解析 FQDN。与 IP 不同,FQDN 是从 azurerm_kubernetes_cluster 资源中导出的。
这将允许您通过 dns_a_record_set
的 addrs
属性使用生成的 IP 地址,如下所示(防火墙规则的目标需要 IP 的示例):
# not shown:
# * declaring dns provider in required_providers block
# * supporting resources eg azurerm_resource_group, etc
resource "azurerm_kubernetes_cluster" "this" {
...
}
data "dns_a_record_set" "aks_api_ip" {
host = azurerm_kubernetes_cluster.this.fqdn
}
resource "azurerm_firewall_network_rule_collection" "firewall_network_rule_collection" {
name = "ip_based_network_rules"
azure_firewall_name = azurerm_firewall.this.name
resource_group_name = azurerm_resource_group.this.name
priority = 200
action = "Allow"
rule {
name = "aks-nodes-to-control-plane"
description = "Azure Global required network rules: https://docs.microsoft.com/en-us/azure/aks/limit-egress-traffic"
source_addresses = azurerm_subnet.this.address_prefixes
destination_ports = [ "443", "1194", "9000" ]
destination_addresses = data.dns_a_record_set.aks_api_ip.addrs
protocols = [
"UDP",
"TCP"
]
}
...
}
以上方法适用于我的情况,并成功将正确的 IP 添加到规则目标。不需要 depends_on
,谢天谢地,Terraform 能够确定构建顺序。
我正在尝试在 k8s 中获取外部 IP 入口。有没有办法从地形数据块中获取详细信息。比如使用数据“azurerm_kubernetes_cluster”之类的?
我想到的解决方案(我确定这不是理想的解决方案)是使用 local-exec
,然后使用 kubectl 查询入口资源 in-cluster 以获取IP.
类似这样的事情(注意:还没有测试过,我也不使用 AKS,所以我不确定它是否会按预期工作)
resource "null_resource" "example1" {
provisioner "local-exec" {
command = "kubectl get ingress name-of-ingress-controller-lb | jq .status.loadBalancer.ingress[0].ip"
}
}
您可以使用 terraform 预先创建 Public IP 并将此 IP 分配给您的入口服务:
YAML:
apiVersion: v1
kind: Service
metadata:
annotations:
service.beta.kubernetes.io/azure-load-balancer-resource-group: myResourceGroup # only needed if the LB is in another RG
name: ingress-nginx-controller
spec:
loadBalancerIP: <YOUR_STATIC_IP>
type: LoadBalancer
相同但 Terraform 代码:
resource "kubernetes_service" "ingress_nginx" {
metadata {
name = "ingress-nginx-controller"
annotations {
"service.beta.kubernetes.io/azure-load-balancer-resource-group" = "${azurerm_resource_group.YOUR_RG.name}"
}
spec {
selector = {
app = <PLACEHOLDER>
}
port {
port = <PLACEHOLDER>
target_port = <PLACEHOLDER>
}
type = "LoadBalancer"
load_balancer_ip = "${azurerm_public_ip.YOUR_IP.ip_address}"
}
}
另一种解决方案是使用 DNS provider's dns_a_record_set
数据源在构建时解析 FQDN。与 IP 不同,FQDN 是从 azurerm_kubernetes_cluster 资源中导出的。
这将允许您通过 dns_a_record_set
的 addrs
属性使用生成的 IP 地址,如下所示(防火墙规则的目标需要 IP 的示例):
# not shown:
# * declaring dns provider in required_providers block
# * supporting resources eg azurerm_resource_group, etc
resource "azurerm_kubernetes_cluster" "this" {
...
}
data "dns_a_record_set" "aks_api_ip" {
host = azurerm_kubernetes_cluster.this.fqdn
}
resource "azurerm_firewall_network_rule_collection" "firewall_network_rule_collection" {
name = "ip_based_network_rules"
azure_firewall_name = azurerm_firewall.this.name
resource_group_name = azurerm_resource_group.this.name
priority = 200
action = "Allow"
rule {
name = "aks-nodes-to-control-plane"
description = "Azure Global required network rules: https://docs.microsoft.com/en-us/azure/aks/limit-egress-traffic"
source_addresses = azurerm_subnet.this.address_prefixes
destination_ports = [ "443", "1194", "9000" ]
destination_addresses = data.dns_a_record_set.aks_api_ip.addrs
protocols = [
"UDP",
"TCP"
]
}
...
}
以上方法适用于我的情况,并成功将正确的 IP 添加到规则目标。不需要 depends_on
,谢天谢地,Terraform 能够确定构建顺序。