Home > database >  Kubernetes annotations are not applied
Kubernetes annotations are not applied

Time:02-17

In my kubernetes deployment file I have a annotation as below

spec:
  template:
    metadata:
      annotations:
        prometheus.io/port: "24231"
        prometheus.io/scrape: "true"

But when i apply the deployment file it will be replaced with

spec:
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-app
        version: 4.0.5-164

Not sure why my annotations are not coming. It is getting replaced with data from a metadata section as shown below

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-app
    appid: xxxxx-xxxx-xxxx
    groupid: DEFAULT
    version: 4.0.5-164

K8s version 1.18

CodePudding user response:

Not sure where you are putting annotations

spec:
  template:
    metadata:

ideally it should in metadata of deployment or pod

apiVersion: v1
kind: Pod
metadata:
  name: annotations-demo
  annotations:
    imageregistry: "https://hub.docker.com/"
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

something like

metadata:
      name: annotations-demo
      annotations:
        imageregistry: "https://hub.docker.com/"

it should be first metadata section instead of spec.template one

CodePudding user response:

you are showing us different parts of the deployment manifest here, so I think you are confusing the different metadata sections in the same file.

the first section, .metadata, is applied to the deployment itself.

the .spec.template.metadata section is applied to the pods that are created by the deployment and those annotations will not appear in the top .metadata section of the deployment.

summary:

if you want to specify labels/annotations for the deployment, use the .metadata section.

if you want to specify labels/annotations that are applied to your pod, use the .spec.template.metadata section.

if you want to specify labels/annotations for both, specify them in both places.

example:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name:  MYAPP
  # labels/annotations that are applied to the deployment
  labels:
    app:  MYAPP
    appid: xxxxx-xxxx-xxxx
    groupid: DEFAULT
    version: 4.0.5-164
  annotations:
    whatever: isapplied
spec:
  ...
  template:
    metadata:
      # labels/annotations that are applied to the pods
      labels:
        app:  MYAPP
        appid: xxxxx-xxxx-xxxx
        groupid: DEFAULT
        version: 4.0.5-164
      annotations:
        prometheus.io/port: "24231"
        prometheus.io/scrape: "true"
  • Related