What is the correct way to set annotations on a Kubernetes Service in YAML

4/11/2019

I am trying to set annotations on my Service object in Kubernetes, but for some reason (even tho the k8s master accepts my request) the annotations are not being set. Here is my YAML file:

---
apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
    app: myapp
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-ssl-cert: 'arn:aws:acm:us-west-2:<redacted>:certificate/<redacted>'
    service.beta.kubernetes.io/aws-load-balancer-ssl-negotiation-policy: 'ELBSecurityPolicy-TLS-1-2-2017-01'
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: 'http'   
    service.beta.kubernetes.io/aws-load-balancer-additional-resource-tags: 'app=myapp'
    service.beta.kubernetes.io/aws-load-balancer-access-log-enabled: true
spec:
  type: LoadBalancer
  selector:
    app: myapp-web
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000

I can then apply the file without error:

[ben@ubuntu simplenexus.com]$ kc apply -f deploy/k8s/service.yaml
service/myapp created

However querying the object returns with none of the annotations:

[ben@ubuntu simplenexus.com]$ kc get svc myapp -o yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"myapp"},"name":"myapp","namespace":"default"},"spec":{"ports":[{"port":80,"protocol":"TCP","targetPort":3000}],        "selector":{"app":"myapp-web"},"type":"LoadBalancer"}}
  creationTimestamp: "2019-04-11T16:27:45Z"
  labels:
    app: myapp
  name: myapp
  namespace: default
  resourceVersion: "2085328"
  selfLink: /api/v1/namespaces/default/services/myapp
  uid: <redacted>
spec:
  clusterIP: <redacted>
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 32118
    port: 80
    protocol: TCP
    targetPort: 3000
  selector:
    app: myapp-web
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - hostname: <redacted>.us-west-2.elb.amazonaws.com

I can manually set annotations, and they stay:

[ben@ubuntu simplenexus.com]$ kc annotate svc myapp newannot=success
service/myapp annotated
[ben@ubuntu simplenexus.com]$ kc get svc myapp -o yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"myapp"},"name":"myapp","namespace":"default"},"spec":{"ports":[{"port":80,"protocol":"TCP","targetPort":3000}],        "selector":{"app":"myapp-web"},"type":"LoadBalancer"}}
    newannot: success
  creationTimestamp: "2019-04-11T16:27:45Z"
  labels:
    app: myapp
  name: myapp
  namespace: default
  resourceVersion: "2085328"
  selfLink: /api/v1/namespaces/default/services/myapp
  uid: <redacted>
  ...

Am I doing this wrong? What's the proper way to set annotations on a Kubernetes Service object in a YAML file?

-- Freedom_Ben
kubernetes
kubernetes-service
yaml

1 Answer

4/12/2019

It turns out you can't use boolean values in annotations, here's ongoing bug report for it. Annotation values must be strings, so you'll need to sanitize it by putting values in brackets '...'.

I deployed your YAML and it worked with:

    service.beta.kubernetes.io/aws-load-balancer-access-log-enabled: 'true'
-- MWZ
Source: StackOverflow