Use "variables" in kubernetes service definition

7/12/2020

the following is the service definition

# Source: ingress-nginx/templates/controller-service.yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: http
    service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: '60'
    service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: 'true'
    service.beta.kubernetes.io/aws-load-balancer-ssl-cert: arn:aws:iam::xxx:server-certificate/project-ssl-cert2xxx01
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: https
    service.beta.kubernetes.io/aws-load-balancer-type: elb
    service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags: 'Name=nginx-ingress-${ENVIRONMENT}-elb,owner=devops,k8s=true'
  labels:
    helm.sh/chart: ingress-nginx-2.11.0
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/version: 0.34.0
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: controller
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: tohttps
    - name: https
      port: 443
      protocol: TCP
      targetPort: http
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/component: controller

there is this line in .metadata.annotations containing ${ENVIRONMENT}:

service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags: 'Name=nginx-ingress-${ENVIRONMENT}-elb,owner=devops,k8s=true'

So what is the best (and proper way) to pass this into kubectl apply?

-- DmitrySemenov
kubernetes
kubernetes-ingress

1 Answer

7/22/2020

Environment variables are not directly supported on templates used by kubectl.

There are some tools that can help with configuration templating:

  • You can use Helm to deploy your resources and make use of Helm Values to separate the configs for each environment.
  • Use kustomize to change variant configs on your template before applying it.
  • Kapitan can handle templating too (although it's an exaggerated solution for your problem).

  • Or you can do some text-processing before applying your template (e.g., sed 's/${ENVIRONMENT}/development/g' controller-service.yaml | kubectl apply -f -)

-- Eduardo Baitello
Source: StackOverflow