Home > Software design >  Cannot add linkerd inject annotation in deployment file
Cannot add linkerd inject annotation in deployment file

Time:10-29

I have a kubernetes deployment file user.yaml -

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-deployment
  namespace: stage
spec:
  replicas: 1
  selector:
    matchLabels:
      app: user
  template:
    metadata:
      labels:
        app: user
      annotations:
        prometheus.io/scrape: 'true'
        prometheus.io/port: '9022'
    spec:
      nodeSelector:
        env: stage
      containers:
        - name: user
          image: <docker image path>
          imagePullPolicy: Always
          resources:
            limits:
              memory: "512Mi"
              cpu: "250m"
            requests:
              memory: "256Mi"
              cpu: "200m"
          ports:
            - containerPort: 8080
          env:
            - name: MODE
              value: "local"
            - name: PORT
              value: ":8080"
            - name: REDIS_HOST
              value: "xxx"
            - name: KAFKA_ENABLED
              value: "true"
            - name: BROKERS
              value: "xxx"
      imagePullSecrets:
        - name: regcred

---
apiVersion: v1
kind: Service
metadata:
  namespace: stage
  name: user
spec:
  selector:
    app: user
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

---
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: user
  namespace: stage
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: user-deployment
  minReplicas: 1
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50
    - type: Resource
      resource:
        name: memory
        target:
          type: AverageValue
          averageValue: 300Mi

This deployment is already running with linkerd injected with command cat user.yaml | linkerd inject - | kubectl apply -f -

Now I wanted to add linkerd inject annotation (as mentioned here) and use command kubectl apply -f user.yaml just like I use for a deployment without linkerd injected.

However, with modified user.yaml (after adding linkerd.io/inject annotation in deployment) -

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-deployment
  namespace: stage
spec:
  replicas: 1
  selector:
    matchLabels:
      app: user
  template:
    metadata:
      labels:
        app: user
      annotations:
        prometheus.io/scrape: 'true'
        prometheus.io/port: '9022'
        linkerd.io/inject: enabled
    spec:
      nodeSelector:
        env: stage
      containers:
        - name: user
          image: <docker image path>
          imagePullPolicy: Always
          resources:
            limits:
              memory: "512Mi"
              cpu: "250m"
            requests:
              memory: "256Mi"
              cpu: "200m"
          ports:
            - containerPort: 8080
          env:
            - name: MODE
              value: "local"
            - name: PORT
              value: ":8080"
            - name: REDIS_HOST
              value: "xxx"
            - name: KAFKA_ENABLED
              value: "true"
            - name: BROKERS
              value: "xxx"
      imagePullSecrets:
        - name: regcred

---
apiVersion: v1
kind: Service
metadata:
  namespace: stage
  name: user
spec:
  selector:
    app: user
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

---
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: user
  namespace: stage
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: user-deployment
  minReplicas: 1
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 50
    - type: Resource
      resource:
        name: memory
        target:
          type: AverageValue
          averageValue: 300Mi

When I run kubectl apply -f user.yaml, it throws error -

service/user unchanged
horizontalpodautoscaler.autoscaling/user configured
Error from server (BadRequest): error when creating "user.yaml": Deployment in version "v1" cannot be handled as a Deployment: v1.Deployment.Spec: v1.DeploymentSpec.Template: v1.PodTemplateSpec.Spec: v1.PodSpec.Containers: []v1.Container: v1.Container.Env: []v1.EnvVar: v1.EnvVar.v1.EnvVar.Value: ReadString: expects " or n, but found 1, error found in #10 byte of ...|,"value":1},{"name":|..., bigger context ...|ue":":8080"}

Can anyone please point out where I have gone wrong in adding annotation?

Thanks

CodePudding user response:

Try with double quotes like below

annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9022"
        linkerd.io/inject: enabled

CodePudding user response:

In addition to @Harsh Manvar answer, I would like to brief some more points:

The metadata in an annotation can be small or large, structured or unstructured, and can include characters not permitted by labels. Annotations, like labels, are key/value maps:

"metadata": {
  "annotations": {
    "key1" : "value1",
    "key2" : "value2"
  }
}

The keys and the values in the map must be strings. In other words, you cannot use numeric, boolean, list or other types for either the keys or the values.

Check some Syntax and Character Set for annotations.

Also, Simply adding the annotation will not automatically mesh existing pods. After setting the annotation, you will need to recreate or update any resources (e.g. with kubectl rollout restart) to trigger proxy injection. (Often, a rolling update can be performed to inject the proxy into a live service without interruption.)

For More details check Meshing a service with annotations.

  • Related