Multi-container Pods

When working with multi-container pods, you need to configure auto-instrumentation to handle multiple containers appropriately. The Operator provides flexible options for controlling which containers receive instrumentation and how multiple instrumentations interact.

Default Behavior

By default, when you annotate a pod for auto-instrumentation, the Operator injects instrumentation into the first container in the pod specification. This works well for single-container pods but may not be suitable for multi-container scenarios.

Specifying Target Containers

To control which containers receive instrumentation, use the container-names annotation:

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-app
  annotations:
    instrumentation.opentelemetry.io/inject-java: "true"
    instrumentation.opentelemetry.io/container-names: "app,worker"
spec:
  containers:
  - name: app
    image: myapp:latest
  - name: worker
    image: myworker:latest
  - name: sidecar
    image: mysidecar:latest

In this example, only the app and worker containers receive Java instrumentation, while the sidecar container remains uninstrumented.

Multiple Instrumentations in One Pod

You can apply different instrumentation types to different containers within the same pod:

apiVersion: v1
kind: Pod
metadata:
  name: polyglot-app
  annotations:
    instrumentation.opentelemetry.io/inject-java: "true"
    instrumentation.opentelemetry.io/inject-python: "true"
    instrumentation.opentelemetry.io/java-container-names: "java-app"
    instrumentation.opentelemetry.io/python-container-names: "python-worker"
spec:
  containers:
  - name: java-app
    image: myjavaapp:latest
  - name: python-worker
    image: mypythonworker:latest
  - name: nginx
    image: nginx:latest

This configuration applies:

  • Java instrumentation to the java-app container
  • Python instrumentation to the python-worker container
  • No instrumentation to the nginx container

Container Name Annotation Format

The container name annotation follows this pattern:

instrumentation.opentelemetry.io/<language>-container-names: "container1,container2"

Where <language> can be:

  • java
  • python
  • nodejs
  • dotnet
  • go
  • apache-httpd
  • sdk

Environment Variable Isolation

Each instrumented container receives its own set of instrumentation environment variables. You can configure container-specific settings using the Instrumentation CR:

spec:
  java:
    env:
      - name: OTEL_SERVICE_NAME
        value: java-service
  python:
    env:
      - name: OTEL_SERVICE_NAME
        value: python-service

Example: Multi-language Application

In the following example, myapp and myapp2 containers will be instrumented using Java and myapp3 using Python instrumentation:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment-with-multi-containers-multi-instrumentations
spec:
  selector:
    matchLabels:
      app: my-pod-with-multi-containers-multi-instrumentations
  replicas: 1
  template:
    metadata:
      labels:
        app: my-pod-with-multi-containers-multi-instrumentations
      annotations:
        instrumentation.opentelemetry.io/inject-java: "true"
        instrumentation.opentelemetry.io/java-container-names: "myapp,myapp2"
        instrumentation.opentelemetry.io/inject-python: "true"
        instrumentation.opentelemetry.io/python-container-names: "myapp3"
    spec:
      containers:
        - name: myapp
          image: myImage1
        - name: myapp2
          image: myImage2
        - name: myapp3
          image: myImage3

Important Notes

  • Go auto-instrumentation does not support multicontainer pods. When injecting Go auto-instrumentation the first container should be the only you want to instrument.
  • This type of instrumentation does not allow to instrument a container with multiple language instrumentations.
  • instrumentation.opentelemetry.io/container-names annotation is not used for this feature.