How to create nginx ingress in terraform - aks
Asked Answered
P

5

6

How can i create a nginx ingress in azure kubernetes using terraform, earlier in this link , i remember seeing some steps as a mandatory installation for all setups, right now it seems to be removed and there is a specific way of installing for aks in this link, should i rewrite all these to adapt to terraform or is there any other smart way of installing nginx ingress for aks through terraform

Ptolemaeus answered 10/6, 2020 at 11:22 Comment(0)
O
9

You could try using Terraform's helm provider.

provider "helm" {
    kubernetes {
        host     = azurerm_kubernetes_cluster.your_cluster.kube_config.0.host
        client_key             = base64decode(azurerm_kubernetes_cluster.your_cluster.kube_config.0.client_key)
        client_certificate     = base64decode(azurerm_kubernetes_cluster.your_cluster.kube_config.0.client_certificate)
        cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.your_cluster.kube_config.0.cluster_ca_certificate)
    }  
}

data "helm_repository" "stable" {
  name = "stable"
  url  = "https://kubernetes-charts.storage.googleapis.com"
}

resource "helm_release" "nginix_ingress" {
    name      = "nginx_ingress"
    repository = data.helm_repository.stable.metadata.0.name
    chart     = "stable/nginx-ingress"
    namespace = "kube-system"
}

If your cluster is already created, you will have to import it as well using a data source. helm_release also supports custom values. Here is the link if you need more information.

October answered 11/6, 2020 at 14:14 Comment(2)
Thanks for the reply, this seems to have created the ingress in kube-system namespace, but is it advisable to have that in that namespace? And also i am getting 502 Bad GatewayPtolemaeus
@Ptolemaeus I have always installed nginx in kube-system and I have never had any problems with it. For the 502 Bad Gateway error, check the logs of the nginx pod.October
C
2

Updated answer

resource "helm_release" "nginix-ingress" {
  name      = "nginix-ingress"    
  repository = "https://charts.bitnami.com/bitnami"    
  chart     = "nginx"
  namespace = "kube-system"
}
Clearcole answered 13/12, 2021 at 13:0 Comment(0)
S
1

There is a nice tutorial Create an Application Gateway ingress controller in Azure Kubernetes Service. And you can check GitHub for Application Gateway Ingress Controller.

If you are using Terraform version 0.12 or higher you can use terraform provider kubernetes example.

As for the Terraform documentation you should check Data source kubernetes_ingress and Resource kubernetes_ingress.

If you provide more details I'll update the answer.

Supererogate answered 10/6, 2020 at 13:52 Comment(1)
Please note, that nginx-ingress and ingress nginx are not the same. The latter is the community version, the former is maintained by F5 NGINX. For ref, see more here nginx.com/blog/…Mender
S
1

I offer an alternative, in my opinion better, way to provision Kubernetes services like Nginx ingress using Terraform.

My kustomize based modules have two main benefits over using helm based modules:

  1. Patching instead of templating makes maintaining custom configuration easier across new upstream versions
  2. My kustomization provider, unlike the helm provider, shows detailed diffs and even destroy/recreate during terraform plan

If you're interested, here's a detailed comparison between my kustomize based Nginx ingress Terraform module and a helm based module.

Using the module is straight forward:

# require and configure provider
terraform {
  required_providers {
    kustomization = {
      source = "kbst/kustomization"
    }
  }
}

provider "kustomization" {
  alias = "example"

  kubeconfig_path = "~/.kube/config"
}
# call module
module "example_nginx" {
  providers = {
    # we're using the alias provider we configured above
    kustomization = kustomization.example
  }

  source  = "kbst.xyz/catalog/nginx/kustomization"
  version = "1.2.1-kbst.0" # find the latest version on https://www.kubestack.com/catalog/nginx

  # the configuration here assumes you're using Terraform's default workspace
  # use `terraform workspace list` to see the workspaces
  configuration_base_key = "default"
  configuration = {
    default = {
      replicas = [{
        name  = "ingress-nginx-controller"
        count = 5
      }]
    }
  }
}

The example module call uses kustomize's replicas attribute to change the replicas of the Nginx ingress controller.

The module's (I have them for more than just Nginx) allow defining the kustomization as part of the module call. They also bundle an upstream release, and you control the version using the version attribute.

Documentation for all available kustomization attributes can be found on the Kubestack website.

I maintain these modules as part of Kubestack, my open-source framework for platform teams working with Terraform and Kubernetes.

Senate answered 30/7, 2022 at 5:17 Comment(0)
L
0

I think adding http_application_routing_enabled with true should create nginx ingress for your cluster.

Lever answered 11/8, 2023 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.