Ingress in K8s - Name based Routing with TLS

Apr 7, 2024 min read

captionless image

Preface:

In the previous article we saw how to use nginx ingress for routing. Today we will experiment with adding domain to our app and will also use TLS cert for the domain.

Getting started:

A valid domain is required for this experiment.

If you have domain outside of azure, then add the name server from azure.

Create dns zone. Change the name server in the providers dns management section to be able to manage dns records from azure itself.

captionless image

Now create a managed identity(You can use even service principals).

captionless image

Assign contributor role to the identity.

captionless image

Add any external dns. Update the json with azure ac details.

captionless image

Add the identity to vm scale set.

Create secret based on the azure.json and apply the external dns yaml. The yaml uses this secret.

captionless image

And apply the external dns.

captionless image

Now apply the ingress resource.

kctl apply -f named_ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
 name: host-ingress
 annotations:
  kubernetes.io/ingress.class: nginx
spec:
 ingressClassName: nginx
 rules:
 - host: hotel.sagarpanda.com
   http:
    paths:
    - path: /
      pathType: Exact
      backend:
       service:
         name: hotel
         port:
          number: 80

 - host: coffee.sagarpanda.com
   http:
    paths:
    - path: /
      pathType: Exact
      backend:
       service:
         name: coffee
         port:
          number: 80

Once we do that, we can see dns records getting created automatically.

captionless image

captionless image

get ip of the ingress and browse.

kctl get ingress

captionless image

There we go. Our ingress is working fine and routing works fine.

captionless image

Adding TLS:

Now that our app is set and working fine, lets add ssl certificate.

Note: Here I’m using lets encrypt cert. First we need to install the cert manager.

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.4/cert-manager.yaml

captionless image

Create a certIssuer with cert provider details.

apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-prod
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: your-email@example.com  #use valid mail or it wont work
    privateKeySecretRef:
      name: letsencrypt-prod
    solvers:
    - http01:
        ingress:
          class: nginx

Get the secret created.

captionless image

Update the ingress with the TLS details.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: host-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  ingressClassName: nginx
  rules:
  - host: hotel.sagarpanda.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hotel
            port:
              number: 80
  - host: coffee.sagarpanda.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: coffee
            port:
              number: 80
  tls:
  - hosts:
    - hotel.sagarpanda.com
    - coffee.sagarpanda.com
    secretName: sagarpanda-tls-secret-lqblm

And finally, we have TLS cert.

captionless image

View the cert details and validity.

captionless image

Noice 😁

There you go. Our app running on k8s with nginx ingress using our own domain.

Read more on K8s:

View list

0