Thanos-sidecar實例信息模板定義-sidecar_instance.yml
instance:
{{- if eq .EnvMap.POD_IP "113.125.219.21"}}
tags:
province: "福建省|吉林省"
{{- else if eq .EnvMap.POD_IP "113.125.219.22"}}
tags:
province: "江蘇省"
hash: 0
hash_modulus: 2
{{- else if eq .EnvMap.POD_IP "113.125.219.23"}}
tags:
province: "江蘇省"
hash: 1
hash_modulus: 2
{{- end}}
exclude_labels:
- hash
- hash_modulus
通過上面的語法定義好實例信息,其中:
-
EnvMap是環境變量集合,引用環境變量可通過.EnvMap.xxx。比如通過k8s向sidecar設置了環境變量POD_IP,則在模板中引用方法為.EnvMap.POD_IP -
實例信息格式說明
instance:
# 標簽信息
tags:
# 在這里自定義標簽
# tags中的標簽將被作為標識 __identify_${tag中值} (如:__identify_province)傳遞給query
# 查詢是可以添加類似 __identify_province="福建省" 標簽來為query定向指定后端,避免全局掃描
# 格式:key: value 如需作為query標識則不支持嵌套
exclude_labels:
# 數組,這里指定哪些標簽不傳遞給query
# 如上面的例子:最終只有 __identify_province 會傳遞到query注意:如果在
tags中定義了很多標簽,那么不需要傳遞給query的一定要在exclude_labels設置下,避免影響query性能
定義prometheus采集模板-prometheus.yaml
scrape_configs:
{{- if and (.Instance.Tags.province) (ne .Instance.Tags.province "")}}
- job_name: 'nginx-vtx-exporter'
metrics_path: /vod_vts
relabel_configs:
- source_labels: [__meta_consul_service_metadata_province]
regex: {{Instance.Tags.province}}
action: keep
{{- if and (.Instance.Tags.hash) (.Instance.Tags.hash_modulus)}}
- source_labels: [__meta_consul_service_metadata_node]
modulus: {{.Instance.Tags.hash_modulus}}
target_label: __tmp_hash
action: hashmod
- source_labels: [__tmp_hash]
regex: ^{{.Instance.Tags.hash}}$
action: keep
{{- end}}
....
- job_name: ''
....
{{- end}}
定義prometheus rule模板-*.tpl
groups:
- name: nginx_bandwidth_all_thanos_record_record
interval: 60s
rules:
# 如果做拆分了,就不在這里做省份粒度的預計算
{{- if or (not .Instance.Tags.hash) (not .Instance.Tags.hash_modulus)}}
- record: record_Rate5mDomainIspProvinceBW
expr: sum(record_Rate5mInstanceBW{node_module=~".*直播.*"})by(province,isp)
{{- end}}
rule模板命名一定要用
.tpl后綴才會生效
綜上所述,最后為sidecar指定命令參數:
--reloader.identify-config-file=/xxx/sidecar_instance.yml # 指定sidecar實例信息文件
--reloader.config-file=/xxx/prometheus.yaml # 指定prometheus配置模板文件,這個參數不變,里面的內容要變
--reloader.rule-dir=/xxx/rule # 指定規則目錄,參數不變
/xxx/rule 目錄中的內容說明:.tpl 后綴的就是模板文件,會生成對應的 .yml文件;.yml文件不經過模板化處理
同一個目錄下.tpl后綴的文件和.yml后綴的文件,出去后綴后的名稱不要相同,否則會互相覆蓋
Thanos-rule預計算配置管理
實例信息模板定義-rule_instance.yml
這部分與sidecar實例信息模板定義一樣
instance:
{{- if eq .EnvMap.POD_NAME "thanos-rule-0"}}
tags:
provinces:
- '福建省'
- '江蘇省'
{{- else if eq .EnvMap.POD_NAME "thanos-rule-1"}}
tags:
provinces:
- '遼寧省'
- '廣東省'
{{- end}}
exclude_labels:
- provinces
rule配置模板-*.tpl
groups:
# 如果有定義 provinces 則說明需要進行省粒度預計算
{{- if and (.Instance.Tags.provinces) (gt (len .Instance.Tags.provinces) 0)}}
- name: nginx_bandwidth_all_thanos_record_record
interval: 60s
rules:
{{- range $index, $province := .Instance.Tags.provinces}}
- record: record_Rate5mDomainIspProvinceBW
expr: sum(record_Rate5mInstanceBW{node_module=~".*直 播.*",__identify_province=~"{{$province}}"})by(host_group_level)
{{- end}}
{{- end}}
綜上所述,thanos-rule需要新增參數
--rule-template=/xxx/rule_instance.yml
-rule-file=/xxx/rule/*.yml指定規則目錄,其中,rule模板仍然是以.tpl為后綴。