为什么Azure负载平衡器在AKS中创建,即使我使用AppGateway作为入口控制器?

epggiuax  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(143)

我已经使用以下Terraform在Azure中创建了一个Kubernetes集群。正如您所清楚看到的,我已经将AppGateway ID传递给了ingress_application_gateway。

# Create the Azure Kubernetes Service (AKS) Cluster
resource "azurerm_kubernetes_cluster" "kubernetes_cluster" {
  count                         = var.enable_kubernetes == true ? 1 : 0
  name                          = "aks-prjx-${var.subscription_type}-${var.environment}-${var.location}-${var.instance_number}"    
  location                      = var.location
  resource_group_name           = module.resource_group_kubernetes_cluster[0].name  # "rg-aks-spoke-dev-westus3-001"
  dns_prefix                    = "dns-aks-prjx-${var.subscription_type}-${var.environment}-${var.location}-${var.instance_number}" #"dns-prjxcluster"
  private_cluster_enabled       = false
  local_account_disabled        = true

  default_node_pool {
    name                        = "npprjx${var.subscription_type}" #"prjxsyspool" # NOTE: "name must start with a lowercase letter, have max length of 12, and only have characters a-z0-9."
    vm_size                     = "Standard_B8ms"
    vnet_subnet_id              = data.azurerm_subnet.aks-subnet.id
    # zones                     = ["1", "2", "3"]
    enable_auto_scaling         = true
    max_count                   = 3
    min_count                   = 1
    # node_count                = 3
    os_disk_size_gb             = 50
    type                        = "VirtualMachineScaleSets"
    enable_node_public_ip       = false
    enable_host_encryption      = false

    node_labels = {
      "node_pool_type"          = "npprjx${var.subscription_type}"
      "node_pool_os"            = "linux"
      "environment"             = "${var.environment}"
      "app"                     = "prjx_${var.subscription_type}_app"
    }
    tags = var.tags
  }

  ingress_application_gateway {
    gateway_id = azurerm_application_gateway.network.id
  }

  # Enabled the cluster configuration to the Azure kubernets with RBAC
  azure_active_directory_role_based_access_control { 
    managed                     = true
    admin_group_object_ids      = var.active_directory_role_based_access_control_admin_group_object_ids
    azure_rbac_enabled          = true #false
  }

  network_profile {
    network_plugin              = "azure"
    network_policy              = "azure"
    outbound_type               = "userDefinedRouting"
  }

  identity {
    type = "SystemAssigned"
  }  

  oms_agent {
    log_analytics_workspace_id  = module.log_analytics_workspace[0].id
  }

  timeouts {
    create = "20m"
    delete = "20m"
  }

  depends_on = [
    azurerm_application_gateway.network
  ]
}

我认为AppGateway将被用作入口网关。然而,AKS在尝试部署服务时创建了Azure负载均衡器,如下所述

apiVersion: v1
kind: Service
metadata:
  name: aks-helloworld 
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: aks-helloworld-two

未使用此负载平衡器和AppGateway是否有原因?我假设负载平衡器用于类型LoadBalancer,而应用网关用于Ingress

kr98yfug

kr98yfug1#

我尝试在我的环境中重现相同的情况,以创建具有应用程序网关入口控制器的服务:

既然您提到了服务类型:负载平衡器在您的yaml文件中,它正在创建负载平衡服务。为了使用Application Gateway Ingress Controller创建服务而不使用关联的负载平衡器,请按照以下步骤操作。
1.首先,您需要使用所需的副本和映像为应用程序创建部署。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-app
spec:
 replicas: 2
 selector:
  matchLabels:
  app: nginx
template:
 metadata:
   name: test-app
   labels:
     app: nginx
   spec:
     containers:
     - name: nginx
     image: "nginx:latest"
     ports:
     - containerPort: 80

用于检查已部署应用程序kubectl get deploy的cmd

2接下来,您可以使用type:ClusterIP为应用程序创建服务。
注意:如果要创建服务,请为应用程序键入:LoadBalancer**。此服务将使用LoadBalancer创建。

apiVersion: v1
kind: Service
metadata:
    name: nginx-service
     labels:
       app: nginx
   spec:
     selector:
       app: nginx
     ports:
       - port: 80
          targetPort: 80
          protocol: TCP

使用类型创建的服务:群集IP。
cmd检查服务:kubectl get svc

1.为应用程序创建入口资源,以便将流量路由到服务

apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: nginxapp
    annotations:
       kubernetes.io/ingress.class: azure/application-gateway
     spec:
        rules:
        - http:
         paths:
        - pathType: Exact
          path: /
          backend:
           service:
             name: nginx-service
           port:
             number: 80

入口创建成功。

1.创建所有资源后,应用程序网关入口控制器将把流量路由到服务,而不创建关联的负载平衡器。
应用程序正在使用应用程序网关公用IP成功运行。

相关问题