Files
2026-04-08 20:22:14 +07:00

83 lines
2.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Урок 3.2 — Автоматическое добавление sidecar контейнеров
## Файлы
| Файл | Описание |
|------|----------|
| `inject-fluent-bit.yaml` | Opt-in injection Fluent Bit (по аннотации) |
| `fluent-bit-configmap.yaml` | ConfigMap с конфигурацией Fluent Bit |
| `inject-prometheus-exporter.yaml` | Opt-in injection Prometheus exporter |
## Подготовка
```bash
# Применить ConfigMap (нужен до создания подов с Fluent Bit)
kubectl apply -f fluent-bit-configmap.yaml
# Применить политики injection
kubectl apply -f inject-fluent-bit.yaml
kubectl apply -f inject-prometheus-exporter.yaml
```
## Тестирование Fluent Bit injection
```bash
# Под БЕЗ аннотации — sidecar не добавится
kubectl apply -f test-resources/pod-no-logging.yaml
kubectl get pod pod-no-logging -o jsonpath='{.spec.containers[*].name}'
# Вывод: app
# Под С аннотацией — sidecar добавится автоматически
kubectl apply -f test-resources/pod-with-logging.yaml
kubectl get pod pod-with-logging -o jsonpath='{.spec.containers[*].name}'
# Вывод: app fluent-bit
# Проверить конфигурацию sidecar
kubectl describe pod pod-with-logging | grep -A 30 "fluent-bit:"
# Удалить тесты
kubectl delete pod pod-no-logging pod-with-logging
```
## Тестирование Prometheus exporter injection
```bash
kubectl apply -f test-resources/pod-with-monitoring.yaml
kubectl get pod pod-with-monitoring \
-o jsonpath='{.spec.containers[*].name}'
# Вывод: app prometheus-exporter
# Проверить аннотации
kubectl get pod pod-with-monitoring \
-o jsonpath='{.metadata.annotations}' | jq .
kubectl delete pod pod-with-monitoring
```
## Opt-in vs Opt-out
```yaml
# Opt-in: срабатывает только при наличии аннотации
preconditions:
- key: "{{ request.object.metadata.annotations.\"logging.company.com/enabled\" }}"
operator: Equals
value: "true"
# Opt-out: срабатывает везде, кроме явно исключённых
preconditions:
- key: "{{ request.object.metadata.annotations.\"logging.company.com/disabled\" || 'false' }}"
operator: NotEquals
value: "true"
```
## Защита от дублирования
```bash
# Попробовать создать под, который уже содержит fluent-bit
kubectl apply -f test-resources/pod-already-has-sidecar.yaml
# Sidecar НЕ будет дублирован (precondition NotIn защищает)
kubectl get pod pod-already-has-sidecar \
-o jsonpath='{.spec.containers[*].name}'
```