# Kubernetes Operator

> Deploy OpenTelemetry Operator for automatic instrumentation and observability in Kubernetes clusters

Source: https://last9.io/docs/integrations/containers-and-k8s/kubernetes-operator/

Deploy OpenTelemetry Operator, Collector, and Cluster Monitoring via automated setup script.

## Prerequisites

- Kubernetes cluster (v1.19+), kubectl configured, helm 3.9+
- Cluster admin access
- Last9 credentials from the [Integrations](https://app.last9.io/integrations) page

## Installation

```bash
curl -O https://raw.githubusercontent.com/last9/last9-k8s-observability/main/last9-otel-setup.sh
chmod +x last9-otel-setup.sh
```

**Complete Setup (Recommended)**

```bash
./last9-otel-setup.sh \
  endpoint="$last9_otlp_endpoint" \
  token="$last9_otlp_auth_header" \
  monitoring-endpoint="$last9_remote_write_url" \
  username="$last9_remote_write_username" \
  password="$last9_remote_write_password"
```

**Traces Only**

```bash
./last9-otel-setup.sh operator-only \
  endpoint="$last9_otlp_endpoint" \
  token="$last9_otlp_auth_header"
```

**Logs Only**

```bash
./last9-otel-setup.sh logs-only \
  endpoint="$last9_otlp_endpoint" \
  token="$last9_otlp_auth_header"
```

**Monitoring Only**

```bash
./last9-otel-setup.sh monitoring-only \
  monitoring-endpoint="$last9_remote_write_url" \
  username="$last9_remote_write_username" \
  password="$last9_remote_write_password"
```

## Verification

```bash
kubectl get pods -n last9
```

## Resource Enrichment

The operator-deployed OpenTelemetry Collector includes the `k8sattributes` processor with the required RBAC pre-configured. Every span and metric flowing through the collector is automatically stamped with the following resource attributes — no application or manifest changes needed:

- `k8s.pod.name`
- `k8s.pod.uid`
- `k8s.namespace.name`
- `k8s.deployment.name`
- `k8s.node.name`
- `k8s.container.name`

These power Last9 features that key off pod identity:

- **Discover → Services → Infrastructure Metrics** — pod-grouped CPU, memory, network, restart, and CPU-throttling panels from cAdvisor + kube-state-metrics
- **Discover → Kubernetes** — service-to-pod correlation in the resource graph
- **Trace filters** — filter by `k8s.pod.name`, `k8s.namespace.name`, and other `k8s.*` labels

If you ship traces directly from your application to Last9 without going through the operator-managed collector — for example, a self-managed gateway collector, or direct OTLP from the SDK — see the per-language Kubernetes Resource Attributes guide for the Downward API alternative:

- [Go](/docs/integrations/frameworks/go/kubernetes-resource-attributes/)
- [Ruby](/docs/integrations/frameworks/ruby/kubernetes-resource-attributes/)

## Auto-Instrumentation

Add the annotation to your deployment:

**Java**

```yaml
annotations:
  instrumentation.opentelemetry.io/inject-java: "last9/l9-instrumentation"
```

**Node.js**

```yaml
annotations:
  instrumentation.opentelemetry.io/inject-nodejs: "last9/l9-instrumentation"
```

**Python**

```yaml
annotations:
  instrumentation.opentelemetry.io/inject-python: "last9/l9-instrumentation"
```

**.NET**

```yaml
annotations:
  instrumentation.opentelemetry.io/inject-dotnet: "last9/l9-instrumentation"
```

Then restart your deployment:

```bash
kubectl rollout restart deployment/my-app -n <namespace>
```

## Advanced Configuration

Set environment and cluster name:

```bash
./last9-otel-setup.sh \
  endpoint="$last9_otlp_endpoint" \
  token="$last9_otlp_auth_header" \
  monitoring-endpoint="$last9_remote_write_url" \
  username="$last9_remote_write_username" \
  password="$last9_remote_write_password" \
  env=production \
  cluster=my-cluster
```

Deploy on tainted nodes:

```bash
./last9-otel-setup.sh \
  endpoint="$last9_otlp_endpoint" \
  token="$last9_otlp_auth_header" \
  monitoring-endpoint="$last9_remote_write_url" \
  username="$last9_remote_write_username" \
  password="$last9_remote_write_password" \
  tolerations-file=/absolute/path/to/tolerations.yaml
```

## Uninstall

```bash
./last9-otel-setup.sh uninstall-all
```

---

## Troubleshooting

```bash
# Operator not starting
kubectl logs -n opentelemetry-operator-system deployment/opentelemetry-operator-controller-manager

# App not instrumented
kubectl describe pod <your-app-pod> | grep instrumentation.opentelemetry.io
kubectl get instrumentation -n last9 -o yaml

# Collector issues
kubectl logs -n last9 <collector-pod-name> | grep -i error
```

Please get in touch with us on [Discord](https://discord.com/invite/Q3p2EEucx9) or [Email](mailto:support@last9.io) if you have any questions.
