Kubernetes: Reference a section or value inside a manifest

A colleague showed me this neat trick so I didn’t have to write the same information twice in a Ingress manifest file. I needed to map two hosts to the same path, and instead of duplicate the same information twice we can just reference the “http” section in the previous definition in the second host.


apiVersion: networking.k8s.io/v1
kind: Ingress
  name: my-ingress
  namespace: default
  ingressClassName: nginx
    - host: my-first.domain.se
      http: &http-paths
          - path: /my-application
            pathType: Prefix
                name: my-application-service
                  number: 8080
    - host: my-second.domain.se
      http: *http-paths

The trick here is to use the &-sign to mark a place in manifest that you want to reference. In this example I named the reference “&http-paths”. When we later define the second host (my-second.domain.se) we can just de-reference the reference with the *-sign, here show as “*http-paths”. This will “copy” the whole http section with path, service and port, from the first definition and “paste” it into the second host section.
In Kubernetes this will be de-referenced and look like I put the same information in both hosts

Tested on Tanzu Kubernetes v1.22

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.