Help Center/ Application Service Mesh/ FAQs/ Adding a Service/ How Do I Inject a Sidecar for the Pod Created Using a Job or CronJob?
Updated on 2024-05-23 GMT+08:00

How Do I Inject a Sidecar for the Pod Created Using a Job or CronJob?

Prerequisites

  • Ensure that ASM 1.15.5-r3 or later is used to create service meshes.
  • By default, the sidecar is not injected for the pod created using a job or CronJob. If sidecar injection is required, choose Labels and Annotations in Advanced Settings and set sidecar.istio.io/inject to true for Pod Label.

The following is an example CronJob:

kind: CronJob
apiVersion: batch/v1
metadata:
  name: mycronjob
  namespace: default
spec:
  schedule: '*/1 * * * *'
  jobTemplate:
    spec:
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: mycronjob
            sidecar.istio.io/inject: 'true'
...
  • Before using a job or CronJob, you need to run a specific command in the target container to exit the sidecar.

Exiting a Sidecar After a Job or CronJob Is Complete

Call curl -sf -XPOST http://127.0.0.1:15000/quitquitquit to exit istio-proxy after a job or CronJob is complete.

The following is an example CronJob:

kind: CronJob
apiVersion: batch/v1
metadata:
  name: mycronjob
  namespace: default
spec:
  schedule: '*/1 * * * *'
  concurrencyPolicy: Forbid
  suspend: false
  jobTemplate:
    metadata:
      creationTimestamp: null
    spec:
      template:
        metadata:
          creationTimestamp: null
          labels:
            app: cronjob1
            sidecar.istio.io/inject: 'true'
            version: v1
        spec:
          containers:
            - name: mycronjob-1
              image: 'busybox:latest'
              command:
                - /bin/bash
                - '-c'
              args:
                - |
                  trap "curl --max-time 2 -s -f -XPOST http://127.0.0.1:15000/quitquitquit" EXIT
                  while ! curl -s -f http://127.0.0.1:15020/healthz/ready; do sleep 1;done
                  sleep 2
                  date; echo Hello from the Kubernetes cluster<Your Job command>
...