titan-iac/services/logging/opensearch-ism-job.yaml

60 lines
2.7 KiB
YAML

# services/logging/opensearch-ism-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: opensearch-ism-setup-3
namespace: logging
spec:
backoffLimit: 3
ttlSecondsAfterFinished: 3600
template:
spec:
restartPolicy: OnFailure
nodeSelector:
node-role.kubernetes.io/worker: "true"
hardware: rpi5
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: hardware
operator: In
values:
- rpi5
containers:
- name: apply
image: alpine:3.20
command: ["/bin/sh", "-c"]
args:
- |
set -euo pipefail
apk add --no-cache curl >/dev/null
OS_URL="http://opensearch-master.logging.svc.cluster.local:9200"
for attempt in $(seq 1 60); do
if curl -s -o /dev/null -w "%{http_code}" "${OS_URL}" | grep -q "200"; then
break
fi
sleep 5
done
if ! curl -s -o /dev/null -w "%{http_code}" "${OS_URL}" | grep -q "200"; then
echo "OpenSearch did not become ready in time" >&2
exit 1
fi
policy='{"policy":{"description":"Delete logs after 180 days","schema_version":1,"default_state":"hot","states":[{"name":"hot","actions":[],"transitions":[{"state_name":"delete","conditions":{"min_index_age":"180d"}}]},{"name":"delete","actions":[{"delete":{}}],"transitions":[]}]}}'
curl -sS -X PUT "${OS_URL}/_plugins/_ism/policies/logging-180d" \
-H 'Content-Type: application/json' \
-d "${policy}" >/dev/null
kube_template='{"index_patterns":["kube-*"],"priority":200,"template":{"settings":{"index.number_of_shards":1,"index.number_of_replicas":0,"index.refresh_interval":"30s","plugins.index_state_management.policy_id":"logging-180d"},"mappings":{"properties":{"@timestamp":{"type":"date"}}}}}'
curl -sS -X PUT "${OS_URL}/_index_template/kube-logs" \
-H 'Content-Type: application/json' \
-d "${kube_template}" >/dev/null
journal_template='{"index_patterns":["journald-*"],"priority":200,"template":{"settings":{"index.number_of_shards":1,"index.number_of_replicas":0,"index.refresh_interval":"30s","plugins.index_state_management.policy_id":"logging-180d"},"mappings":{"properties":{"@timestamp":{"type":"date"}}}}}'
curl -sS -X PUT "${OS_URL}/_index_template/journald-logs" \
-H 'Content-Type: application/json' \
-d "${journal_template}" >/dev/null