在YAML中的Kubernetes服务上设置注释的正确方法是什么

时间:2019-04-11 16:36:54

标签: kubernetes yaml kubernetes-service

我正在尝试在Kubernetes中的Service对象上设置注释,但是由于某些原因(即使k8s主服务器接受了我的请求),也未设置注释。这是我的YAML文件:

---
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

然后我可以毫无错误地应用文件:

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

但是查询对象时返回的注释均不包含

[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

我可以手动设置注释,并且它们会保留:

[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>
  ...

我做错了吗?在YAML文件中的Kubernetes服务对象上设置注释的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

事实证明您不能在注释中使用布尔值,这里是正在进行的bug report。注释值必须是字符串,因此您需要通过将值放在方括号'...'中来对其进行清理。

我部署了您的YAML,并且可以使用:

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