This commit is contained in:
2022-04-18 11:21:20 +08:00
commit 45a7af638f
210 changed files with 8997 additions and 0 deletions

250
prometheus/ADD/ccmd Executable file
View File

@@ -0,0 +1,250 @@
#!/bin/bash
##################################################
# ENV #
# - PROMETHEUS_OPTS #
# - ALERTMANAGER_OPTS #
# - GRAFANA_OPTS #
# - LOKI_OPTS #
# Mount dir #
# - /etc/prometheus #
# - /var/log/prometheus #
# - /var/lib/prometheus #
##################################################
set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT
PIDS=
GOT_SIGTERM=
LOG_DIR='/var/log/prometheus'
DATA_DIR='/var/lib/prometheus'
CONF_DIR='/etc/prometheus'
function Print {
local file=/dev/null
[ '-f' = "$1" ] && file=$2 && shift && shift
date +"[%F %T] $*" | tee -a $file
}
function Quit {
local running
while running= ; do
pkill -f sleep && running=1 && Print killing sleep ...
pkill -f grafana-server && running=1 && Print killing grafana-server ...
pkill -f prometheus && running=1 && Print killing prometheus ...
pkill -f alertmanager && running=1 && Print killing alertmanager ...
pkill -f loki && running=1 && Print killing loki ...
[ -z "$running" ] && break
sleep 1
done
Print Container stopped.
test -n "$GOT_SIGTERM"
}
function SideCar {
local md5= last_md5=$(find $CONF_DIR -maxdepth 1 -type f \
-regex ".*\.yml\|.*\.tmpl" | xargs -I ^ md5sum ^ | md5sum)
while sleep 10; do
md5=$(find $CONF_DIR -maxdepth 1 -type f \
-regex ".*\.yml\|.*\.tmpl" | xargs -I ^ md5sum ^ | md5sum)
[ "$md5" != "$last_md5" ] \
&& last_md5=$md5 \
&& Print Reload conf ... \
&& pkill -HUP -f prometheus \
&& pkill -HUP -f alertmanager
done
}
function Init {
mkdir -p $DATA_DIR/{tsdb,alertmanager,grafana,loki}
[ -f $CONF_DIR/prometheus.yml ] || echo 'global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_timeout: 10s
alerting:
alertmanagers:
- static_configs:
- targets:
- 127.0.0.1:9093
rule_files:
# - node_rules.yml
scrape_configs:
#- job_name: prometheus
# honor_labels: true
# static_configs:
# - targets:
# - 127.0.0.1:9100
# labels:
# host: 127.0.0.1
#- job_name: nodes
# static_configs:
# - targets:
# - ip_1:9100
# - ip_2:9100
# metric_relabel_configs:
# - source_labels: [instance]
# target_label: host
# regex: "([^:]+):.+"
' > $CONF_DIR/prometheus.yml
[ -f $CONF_DIR/alertmanager.yml ] || echo 'global:
resolve_timeout: 10m
templates:
# - xxxx.tmpl
route:
group_by: [alertname]
group_wait: 10s
group_interval: 10s
repeat_interval: 1m
receiver: alert
receivers:
- name: alert
inhibit_rules:
- source_match:
severity: emergency
target_match_re:
severity: "warning|critical"
equal: [host]
' > $CONF_DIR/alertmanager.yml
[ -f $CONF_DIR/grafana.ini ] \
|| cp -af /usr/share/grafana/conf/sample.ini $CONF_DIR/grafana.ini
[ -d $CONF_DIR/provisioning ] \
|| cp -af /usr/share/grafana/conf/provisioning $CONF_DIR/provisioning
[ -f $CONF_DIR/loki.yml ] || echo "
auth_enabled: false
server:
http_listen_port: 3100
grpc_listen_port: 9096
log_level: warn
ingester:
wal:
enabled: true
dir: $DATA_DIR/loki/db/wal
lifecycler:
address: 127.0.0.1
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 1h
max_chunk_age: 1h
chunk_target_size: 1048576
chunk_retain_period: 30s
max_transfer_retries: 0
schema_config:
configs:
- from: 2020-10-24
store: boltdb-shipper
object_store: filesystem
schema: v11
index:
prefix: index_
period: 24h
storage_config:
boltdb_shipper:
active_index_directory: $DATA_DIR/loki/db/boltdb-shipper-active
cache_location: $DATA_DIR/loki/db/boltdb-shipper-cache
cache_ttl: 24h
shared_store: filesystem
filesystem:
directory: $DATA_DIR/loki/db/chunks
compactor:
working_directory: $DATA_DIR/loki/db/boltdb-shipper-compactor
shared_store: filesystem
limits_config:
reject_old_samples: true
reject_old_samples_max_age: 168h
chunk_store_config:
max_look_back_period: 0s
table_manager:
retention_deletes_enabled: false
retention_period: 0s
ruler:
storage:
trapype: local
local:
directory: $DATA_DIR/loki/db/rules
rule_path: $DATA_DIR/loki/db/rules-temp
alertmanager_url: http://127.0.0.1:9093
ring:
kvstore:
store: inmemory
enable_api: true
" > $CONF_DIR/loki.yml
}
function StartProc {
Print Start alertmanager ...
alertmanager \
--config.file=$CONF_DIR/alertmanager.yml \
--storage.path=$DATA_DIR/alertmanager \
--web.external-url=http://0.0.0.0:9093/alertmanager/ \
${ALERTMANAGER_OPTS:-} &>> $LOG_DIR/alertmanager.out &
PIDS="$PIDS $!"
sleep 2
Print Start prometheus ...
prometheus \
--config.file=$CONF_DIR/prometheus.yml \
--web.external-url=prometheus \
--web.console.templates=/usr/share/prometheus/consoles \
--web.console.libraries=/usr/share/prometheus/console_libraries \
--storage.tsdb.path=$DATA_DIR/tsdb \
${PROMETHEUS_OPTS:-} &>> $LOG_DIR/prometheus.out &
PIDS="$PIDS $!"
sleep 2
Print Start grafana-server ...
grafana-server \
-homepath /usr/share/grafana \
-config $CONF_DIR/grafana.ini \
${GRAFANA_OPTS:-} web &>> $LOG_DIR/grafana.out &
PIDS="$PIDS $!"
sleep 2
Print Start loki ...
loki \
--config.file=$CONF_DIR/loki.yml \
${LOKI_OPTS:-} &>> $LOG_DIR/loki.out &
PIDS="$PIDS $!"
sleep 2
Print Start sidecar ...
SideCar &
PIDS="$PIDS $!"
}
function Main {
local pid=
Init
StartProc
trap "GOT_SIGTERM=1; Print Got SIGTERM ..." SIGTERM
while [ -z "$GOT_SIGTERM" ] && sleep 1; do
for pid in $PIDS; do
[ ! -e /proc/$pid ] && Print Unexpected error! && exit
done
done
}
# Start here
Main

View File

@@ -0,0 +1,21 @@
# 部署单节点 prometheus
- 根据实际环境修改
- docker-compose.yml
- 创建目录
```
grep '\<source:' docker-compose.yml | cut -d: -f2 | xargs mkdir -p
```
- 启动
```
docker-compose up -d
```
- 修改各组件的配置文件
- 重新启动
```
docker-compose restart
```

View File

@@ -0,0 +1,22 @@
version: "3.7"
services:
prometheus:
image: harbor.colben.cn/general/prometheus
container_name: prometheus
restart: "on-failure"
stop_grace_period: 5m
privileged: true
networks:
prometheus:
volumes:
- type: bind
source: ./prometheus/conf
target: /etc/prometheus
- type: bind
source: ./prometheus/data
target: /var/lib/prometheus
- type: bind
source: ./prometheus/log
target: /var/log/prometheus

59
prometheus/Dockerfile Normal file
View File

@@ -0,0 +1,59 @@
ARG ARCH
FROM harbor.colben.cn/general/photon$ARCH
MAINTAINER Colben colbenlee@gmail.com
ARG MACH
ADD --chown=root:root ADD/ /opt/
RUN prometheus_version=2.25.2 \
&& alertmanager_version=0.21.0 \
&& grafana_version=7.5.4 \
&& loki_version=2.2.1 \
&& tdnf -y install unzip \
&& echo "Downloading prometheus-$prometheus_version.linux-$MACH.tar.gz ..." \
&& curl -sSLO https://github.com/prometheus/prometheus/releases/download/v$prometheus_version/prometheus-$prometheus_version.linux-$MACH.tar.gz \
&& echo "Downloading alertmanager-$alertmanager_version.linux-$MACH.tar.gz ..." \
&& curl -sSLO https://github.com/prometheus/alertmanager/releases/download/v$alertmanager_version/alertmanager-$alertmanager_version.linux-$MACH.tar.gz \
&& echo "Downloading grafana-$grafana_version.linux-$MACH.tar.gz ..." \
&& curl -sSLO https://dl.grafana.com/oss/release/grafana-$grafana_version.linux-$MACH.tar.gz \
&& echo "Downloading v$loki_version/loki-linux-$MACH.zip ..." \
&& curl -sSLO https://github.com/grafana/loki/releases/download/v$loki_version/loki-linux-$MACH.zip \
&& echo 'Installing prometheus ...' \
&& mkdir -p /etc/prometheus /var/lib/prometheus /var/log/prometheus /usr/share/prometheus \
&& tar zxf prometheus-$prometheus_version.linux-$MACH.tar.gz \
&& cd prometheus-$prometheus_version.linux-$MACH \
&& mv consoles console_libraries /usr/share/prometheus/ \
&& mv prometheus promtool /usr/bin/ \
&& cd - \
&& echo 'Installing alertmanager ...' \
&& tar zxf alertmanager-$alertmanager_version.linux-$MACH.tar.gz \
&& cd alertmanager-$alertmanager_version.linux-$MACH \
&& mv alertmanager amtool /usr/bin/ \
&& cd - \
&& echo 'Installing grafana ...' \
&& tar zxf grafana-$grafana_version.linux-$MACH.tar.gz \
&& cd grafana-$grafana_version/ \
&& mv bin/grafana-cli bin/grafana-server /usr/bin/ \
&& rm -rf LICENSE NOTICE.md README.md VERSION bin \
&& cd - \
&& mv grafana-$grafana_version /usr/share/grafana \
&& sed -i -e '/^instance_name *=/cinstance_name = mygrafana' \
-e '/^data *=/cdata = /var/lib/prometheus/grafana' \
-e '/^logs *=/clogs = /var/log/prometheus' \
-e '/^plugins *=/cplugins = /var/lib/prometheus/grafana/plugins' \
-e '/^root_url *=/s/$/grafana/' \
-e '/^provisioning *=/cprovisioning = /etc/prometheus/provisioning' \
-e '/^reporting_enabled *=/creporting_enabled = false' \
-e '/^check_for_updates *=/ccheck_for_updates = false' \
-e '/^disable_gravatar *=/cdisable_gravatar = true' \
-e '/^external_enabled *=/cexternal_enabled = false' \
-e '/^mode *=/cmode = console' \
-e '/^config_file *=/cconfig_file = /etc/prometheus/ldap.toml' \
-e '/^serve_from_sub_path *=/cserve_from_sub_path = true' \
/usr/share/grafana/conf/defaults.ini \
&& echo 'Installing loki ...' \
&& unzip -q loki-linux-$MACH.zip \
&& mv loki-linux-$MACH /usr/bin/loki \
&& tdnf -y erase unzip \
&& rm -rf /var/cache/tdnf prometheus* alertmanager* grafana* loki* \
&& echo 'Built completely.'
CMD ["/opt/ccmd"]

20
prometheus/README.md Normal file
View File

@@ -0,0 +1,20 @@
# 构建 prometheus 镜像
## 定制
- 安装 prometheus alertmanager grafana loki unzip
- 固定一些常用配置
## 外挂目录和文件
- /etc/prometheus: 配置文件目录
- /var/log/prometheus: 日志目录
- /var/lib/prometheus: 数据目录
## 引入环境变量
- PROMETHEUS_OPTS: prometheus 启动参数
- ALERTMANAGER_OPTS: alertmanager 启动参数
- GRAFANA_OPTS: grafana 启动参数
- LOKI_OPTS: loki 启动参数
## 案例
- [Demo/SingleNode/](Demo/SingleNode/): 部署 prometheus

75
prometheus/prometheus.sh Executable file
View File

@@ -0,0 +1,75 @@
#!/bin/bash
#=========================================
# Author : colben
#=========================================
set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT
[ 'x86_64' == "$(uname -m)" ] && ARCH='' || ARCH="-$(uname -m)"
ROOT_DIR="$(cd $(dirname $0) && pwd)"
IMAGE="harbor.colben.cn/general/$(basename ${0%.sh})$ARCH:latest"
if [ -t 0 ]; then
function Print { echo -e "\033[36;1m$(date +'[%F %T]')\033[32;1m $*\033[0m"; }
function Warn { echo -e "\033[36;1m$(date +'[%F %T]')\033[33;1m $*\033[0m"; }
function Error { echo -e "\033[36;1m$(date +'[%F %T]')\033[31;1m $*\033[0m"; exit 1; }
else
function Print { echo -e "$(date +'[%F %T INFO]') $*"; }
function Warn { echo -e "$(date +'[%F %T WARN]') $*"; }
function Error { echo -e "$(date +'[%F %T ERROR]') $*"; exit 1; }
fi
function Quit {
local exitCode=$?
[ 0 -ne $exitCode ] && Error Failed to build or push image!
[ -z "${END:-}" ] && echo && Error Interrupted manually!
Print Succeeded to build and push image.
}
function YesOrNo {
Warn $*
local sw=
while :; do
read -p '(Yes/No/Quit) ' -n1 sw
[[ "$sw" =~ ^Y|y$ ]] && echo && return 0
[[ "$sw" =~ ^N|n$ ]] && echo && return 1
[[ "$sw" =~ ^Q|q$ ]] && echo && exit 0
[ -n "$sw" ] && echo
done
}
function Update {
:
}
function Build {
local yn
local mach
if [ -z "$ARCH" ]; then
mach=amd64
elif [ '-aarch64' == "$ARCH" ]; then
mach=arm64
else
Error Not supported arch: $ARCH
fi
cd $ROOT_DIR
docker images --format='{{.Repository}}:{{.Tag}}' | grep "^$IMAGE$" \
&& Warn Removing image $IMAGE ... \
&& docker rmi $IMAGE
Warn Building image: $IMAGE ...
docker build --force-rm --build-arg ARCH="$ARCH" --build-arg MACH="$mach" -t $IMAGE .
YesOrNo Push image: $IMAGE? && docker push $IMAGE
}
function Main {
Update
Build
END=1
}
# Start here
Main