地形:使用 azurerm_kubernetes_cluster 创建 AKS 群集时指定网络安全组规则



我正在使用 Terraform azurerm 提供程序版本 1.19 来创建 AKS 群集。我想在创建集群时指定网络安全组规则,但我无法弄清楚如何引用创建的安全组,因为生成的安全组被赋予了一个带有随机数的名称。

像这样:

AKS-代理池-33577837-NSG

有没有办法引用创建的 nsg 或至少输出名称中使用的随机数?

用于创建群集的配置:

resource "azurerm_resource_group" "k8s" {
name     = "${var.resource_group_name}"
location = "${var.location}"
}
resource "azurerm_kubernetes_cluster" "k8s" {
name                = "${var.cluster_name}"
location            = "${azurerm_resource_group.k8s.location}"
resource_group_name = "${azurerm_resource_group.k8s.name}"
dns_prefix          = "${var.dns_prefix}"
kubernetes_version  = "${var.kubernetes_version}"
linux_profile {
admin_username = "azureuser"
ssh_key {
key_data = "${file("${var.ssh_public_key}")}"
}
}
agent_pool_profile {
name    = "default"
count   = "${var.agent_count}"
vm_size = "${var.vm_size}"
os_type = "Linux"
}
service_principal {
client_id     = "${var.client_id}"
client_secret = "${var.client_secret}"
}
tags {
source      = "terraform"
environment = "${var.environment}" 
}
}

这将生成一个安全组,我想向其添加其他规则。这是我想添加的规则,以便可以检查nginx控制器的活体探针。

resource "azurerm_network_security_rule" "nginx_liveness_probe" {
name                        = "nginx_liveness"
priority                    = 100 
direction                   = "Inbound"
access                      = "Allow"
protocol                    = "Tcp"
source_port_range           = "*"
destination_port_range      = "${var.nginx_liveness_probe_port}"
source_address_prefix       = "*"
destination_address_prefix  = "*"
resource_group_name         = "${azurerm_kubernetes_cluster.k8s.node_resource_group}"
network_security_group_name = How do I reference the auto-generated nsg ?
description = "Allow access to nginx liveness probe"
}

回答您问题的解决方案:

data "azurerm_resources" "example" {
resource_group_name = azurerm_kubernetes_cluster.example.node_resource_group
type = "Microsoft.Network/networkSecurityGroups"
}
output name_nsg {
value = data.azurerm_resources.example.resources.0.name
}
resource "azurerm_network_security_rule" "example" {
name                        = "example"
priority                    = 100
direction                   = "Outbound"
access                      = "Allow"
protocol                    = "Tcp"
source_port_range           = "*"
destination_port_range      = "*"
source_address_prefix       = "*"
destination_address_prefix  = "*"
resource_group_name         = azurerm_kubernetes_cluster.example.node_resource_group
network_security_group_name = data.azurerm_resources.example.resources.0.name
}

.. 然后以同样的方式添加所有规则。

一般来说,最好并建议使用 Azure Kubernetes Service 对 Kubernetes 服务的创建做出反应的自动化方式,而不是使用更多的 Terraform(尽管下面的 Kubernetes yaml 也可以与 Kubernetes Terraform 提供程序一起使用):

网络安全组筛选 VM 的流量,例如 AKS 节点。创建服务(如负载均衡器)时,Azure 平台会自动配置所需的任何网络安全组规则。不要手动配置网络安全组规则来筛选 AKS 群集中 Pod 的流量。定义任何所需的端口和转发作为 Kubernetes 服务清单的一部分,并让 Azure 平台创建或更新相应的规则。您还可以使用网络策略(如下一节所述)自动将流量筛选规则应用于 Pod。

在当前设置中,通过简单地为您想要披露的端口创建 Kubernetes 服务,应该是可能的。

例如,当我部署入口控制器时,创建 Kubernetes 服务会触发创建 IP 地址/负载均衡器及其 NSG:

apiVersion: v1
kind: Service
metadata:
name: ingress-ingress-nginx-controller
namespace: ingress
spec:
loadBalancerSourceRanges:
- 8.8.8.8/32
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
app.kubernetes.io/component: controller
app.kubernetes.io/instance: ingress
app.kubernetes.io/name: ingress-nginx
type: LoadBalancer

通过创建映射到所需 pod 端口的 Kubernetes 服务(类型负载均衡器),并指定了 loadBalancerSourceRanges,可以为自定义目标指定类似的设置。

apiVersion: v1
kind: Service
metadata:
name: mycustomservice
namespace: myownnamespace
spec:
loadBalancerSourceRanges:
- 8.8.8.8/32 # your source IPs
- 9.9.9.9/32
ports:
- name: myaccessport
port: 777
protocol: TCP
targetPort: mydestinationport
selector:
app.kubernetes.io/name: myapp
type: LoadBalancer

另请参阅 azurerm 提供程序 GitHub 中的问题

您的 AKC 已添加到您使用 Terraform 配置的azurerm_resource_group,我假设。 如果是这样,可以将具有任意数量azurerm_network_security_rule的自定义azurerm_network_security_group添加到该资源组,如此处所述。

例:

resource "azurerm_resource_group" "test" {
name     = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_network_security_group" "test" {
name                = "acceptanceTestSecurityGroup1"
location            = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_network_security_rule" "test" {
name                        = "test123"
priority                    = 100
direction                   = "Outbound"
access                      = "Allow"
protocol                    = "Tcp"
source_port_range           = "*"
destination_port_range      = "*"
source_address_prefix       = "*"
destination_address_prefix  = "*"
resource_group_name         = "${azurerm_resource_group.test.name}"
network_security_group_name = "${azurerm_network_security_group.test.name}"
}

遗憾的是,网络安全组数据源上需要name参数,并且似乎不支持通配符,否则也可以选择通配符。

这里有点晚了,但刚刚遇到了这个问题。因此,对于仍在寻找解决方案的任何人来说,这就是我最终为获取 AKS NSG 名称所做的工作:

将此添加到预配 AKS 的 *.tf 文件:

resource "azurerm_network_security_rule" "http" {
name                        = "YOUR_NAME"
priority                    = 102
direction                   = "Inbound"
access                      = "Allow"
protocol                    = "Tcp"
source_port_range           = "80"
destination_port_range      = "*"
source_address_prefixes     = "${var.ips}"
destination_address_prefix  = "${azurerm_public_ip.ingress.ip_address}"
resource_group_name         = "${azurerm_kubernetes_cluster.test.node_resource_group}"
network_security_group_name = "${data.external.aks_nsg_name.result.output}"
depends_on = ["azurerm_resource_group.test"]
}
# get the NSG name
data "external" "aks_nsg_name" {
program = [
"bash",
"${path.root}/scripts/aks_nsg_name.sh"
]
depends_on = [azurerm_resource_group.test]
}

在项目中创建aks_nsg_name.sh并添加以下内容:

#!/bin/bash 
OUTPUT=$(az network nsg list --query [].name -o tsv | grep aks-agentpool | head -n 1)
jq -n --arg output "$OUTPUT" '{"output":$output}'

最新更新