我们通过配置映射来做到这一点。我们将索引模板和索引生命周期策略存储为 json 格式。此外,执行脚本是配置映射的一部分,它通过 POST 将索引模板和 ilm 策略推送到 elasticsearch。由于现在所有内容都在卷中,因此我们创建了一个 cron 作业来执行此脚本。
例如这是索引模板
所有索引.json:
{
"index_patterns": ["abc*"],
"priority": 300,
"composed_of": ["template1", "template2"],
"version": 3
}
执行脚本.sh
curl -XPUT "http://some_url:9200/_index_template/all-indices" -H 'Content-Type: application/json' -d @/etc/elasticsearch-templates/all-indices.json
我们从 terraform 创建这些配置图。
kibana.yaml
...
podTemplate:
spec:
containers:
- name: kibana
volumeMounts:
- name: elasticsearch-templates
mountPath: /etc/elasticsearch-templates
readOnly: true
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "sleep 15 && sh /etc/elasticsearch-templates/execution-script.sh"]
volumes:
- name: elasticsearch-templates
configMap:
name: ilm-and-index-templates
然后cron作业执行脚本
apiVersion: batch/v1
kind: CronJob
metadata:
name: script-execution
spec:
schedule: "0 5 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: script-execution
image: alpine/curl:latest
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- sh /etc/elasticsearch-templates/execution-script.sh
volumeMounts:
- name: elasticsearch-templates
mountPath: /etc/elasticsearch-templates
readOnly: true
restartPolicy: OnFailure
volumes:
- name: elasticsearch-templates
configMap:
name: ilm-and-index-templates