在【Prometheus】EC2环境中构建prometheus和thanos(CentOS8)
首先
由于经常在网络上看到EC2和本地文章,我想提供一下能够持续存储 Prometheus 的 thanos。虽然有其他文章详细解释 Prometheus 和 thanos 的各个组件,但是这里简洁地说明一下至少在 EC2 上如何操作。
(因为不确定是否可以放置链接,如果有兴趣可以自行搜索“thanos”)
普罗米修斯
安装RPM
为简化步骤,使用packagecloud提供的RPM进行安装。
curl -s https://packagecloud.io/install/repositories/prometheus-rpm/release/script.rpm.sh | sudo bash
yum install prometheus2-2.25.2-1.el8.x86_64
yum install node_exporter-1.1.2-1.el8.x86_64
创建配置文件
# my global config
global:
scrape_interval: 60s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 60s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
external_labels:
cluster: prometheus
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: node
static_configs:
- targets: ['localhost:9100']
服务设置
systemctl enable node_exporter
systemctl enable prometheus
systemctl start node_exporter
systemctl start prometheus
无限手套的狂者
现在画面上已经出现了Prometheus,所以我们要继续安装Thanos。继续使用RPM时,要使用Packagecloud的包。
灭霸安装
yum install thanos-0.18.0-1.el8.x86_64
存储设置
这次使用S3作为存储。由于没有特殊配置,就按照常规方式创建并创建IAM用户。
以下是IAM用户的策略。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement",
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:DeleteObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::[バケット名]/*",
"arn:aws:s3:::[バケット名]"
]
}
]
}
根据创建的S3信息生成设置文件。
type: S3
config:
bucket: "[バケット名]"
endpoint: "s3.amazonaws.com"
region: "ap-northeast-1"
access_key: "[アクセスキー]"
insecure: false
signature_version2: false
secret_key: "[シークレットキー]"
put_user_metadata: {}
http_config:
response_header_timeout: 2m
insecure_skip_verify: false
trace:
enable: false
list_objects_version: ""
part_size: 67108864
sse_config:
type: ""
kms_key_id: ""
kms_encryption_context: {}
encryption_key: ""
thanos的侧边配置设置
由于这次只使用了一台t2.micro来完成所有的设置,所以如果要进行分开设置,需要更改每个URL等等。
THANOS_SIDECAR_OPTS='--tsdb.path=/var/lib/prometheus/data --prometheus.url=http://localhost:9090 --objstore.config-file /etc/prometheus/s3.conf'
THANOS_QUERY_OPTS='--http-address=0.0.0.0:10904 --grpc-address=0.0.0.0:10903 --store=localhost:10901'
THANOS_STORE_OPTS='--data-dir=/var/lib/thanos/store --http-address=0.0.0.0:10906 --grpc-address=0.0.0.0:10905 --objstore.config-file /etc/prometheus/s3.conf'
THANOS_COMPACT_OPTS='--data-dir=/var/lib/thanos/compact --http-address=0.0.0.0:10912 --objstore.config-file /etc/prometheus/s3.conf --wait'
以下是Thanos的设置,但如果保持这样,它将无法启动,所以请确保与Prometheus方面的tsdb.XXX-block-duration保持一致。
PROMETHEUS_OPTS='--config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data --storage.tsdb.min-block-duration=1d --storage.tsdb.max-block-duration=1d'
服务设置
systemctl restart prometheus
systemctl enable thanos-sidecar
systemctl enable thanos-query
systemctl enable thanos-store
systemctl enable thanos-compact
systemctl start thanos-sidecar
systemctl start thanos-query
systemctl start thanos-store
systemctl start thanos-compact
不久之后,数据将完全流向S3。
就此结语
虽然容器正处于全盛时代,但并非所有人都能使用Kubernetes环境,希望能对某个地方的某个人有所帮助。因为能进入EC2,说明也可以在其他云端或本地使用。