主机环境:kali 6.1 (debian..)
一、安装应用
安装docker-compose
apt install docker-compose -y
安装docker
sudo apt update
sudo apt install -y docker.io
sudo systemctl enable docker --now
sudo usermod -aG docker $USER
printf '%s\n' "deb https://download.docker.com/linux/debian bullseye stable" |
sudo tee /etc/apt/sources.list.d/docker-ce.list
curl -fsSL https://download.docker.com/linux/debian/gpg |
sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-ce-archive-keyring.gpg
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
安装完成后配置服务器仓库
在 /etc/docker/daemon.json 下添加
{
"registry-mirrors" :["http://hub-mirror.c.163.com"]
}
docker -v 检查容器是否安装成功
在opt目录新建docker-compose.yml 文件
添加如下内容:
version: "3.7"
services:
node-exporter:
image: prom/node-exporter:latest
container_name: "node-exporter6"
ports:
- "9100:9100"
restart: always
network_mode: host
prometheus:
image: prom/prometheus:latest
container_name: "prometheus6"
restart: always
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- ./prometheus_data:/prometheus
network_mode: host
grafana:
image: grafana/grafana
container_name: "grafana6"
ports:
- "3000:3000"
restart: always
volumes:
- ./grafana_data:/var/lib/grafana
network_mode: host
运行docker-compose up -d 拉取镜像。
二、给本地映射目录授权
在以上yml文件中,设置了两个卷映射(绑定)
有两个volumes(卷) 如下
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- ./prometheus_data:/prometheus
volumes:
- ./grafana_data:/var/lib/grafana
这两个卷就是容器内部和主机进行数据共享的空间。格式 主机 : 容器
例如 ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml 代表 容器路径 /etc/prometheus/prometheus.yml文件和属主机路径
./prometheus/prometheus.yml 文件进行绑定。二者任意一个改变另一个也会一起改变。
其他绑定的含义就不再赘述。绑定的存在代表一定会进行数据交换,所以需要给本地路径授权让容器的数据在本地能做持久化存储。
chmod 777 grafana_data && chmod 777 prometheus_data && chmod 777 prometheus
授权之后
再次运行 docker-compose up -d 启动容器。
┌──(root㉿kali)-[~]
└─# docker-compose up -d
Creating grafana6 ... done
Creating prometheus6 ... done
Creating node-exporter6 ... done
状态为done代表容器启动成功。但别高兴太早,此时再检查容器日志运行情况。
先运行
docker ps 查看容器状态
┌──(root㉿kali)-[~]
└─# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf886faea79e prom/prometheus:latest "/bin/prometheus --c…" 3 minutes ago Up 3 minutes prometheus6
0dda19730332 prom/node-exporter:latest "/bin/node_exporter" 3 minutes ago Up 3 minutes node-exporter6
f63f83126ff5 grafana/grafana "/run.sh" 3 minutes ago Up 3 minutes grafana6
再运行
docker logs -f cf886faea79e 查看 prometheus6容器运行情况,其他两个容器同样需要查看。以下是prometheus6容器日志:
ts=2023-03-09T13:57:32.904Z caller=main.go:512 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2023-03-09T13:57:32.905Z caller=main.go:556 level=info msg="Starting Prometheus Server" mode=server version="(version=2.42.0, branch=HEAD, revision=225c61122d88b01d1f0eaaee0e05b6f3e0567ac0)"
ts=2023-03-09T13:57:32.905Z caller=main.go:561 level=info build_context="(go=go1.19.5, platform=linux/amd64, user=root@c67d48967507, date=20230201-07:53:32)"
ts=2023-03-09T13:57:32.905Z caller=main.go:562 level=info host_details="(Linux 6.1.0-kali5-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.12-1kali2 (2023-02-23) x86_64 kali (none))"
ts=2023-03-09T13:57:32.905Z caller=main.go:563 level=info fd_limits="(soft=1048576, hard=1048576)"
ts=2023-03-09T13:57:32.905Z caller=main.go:564 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2023-03-09T13:57:32.908Z caller=web.go:561 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2023-03-09T13:57:32.910Z caller=main.go:993 level=info msg="Starting TSDB ..."
ts=2023-03-09T13:57:32.921Z caller=tls_config.go:232 level=info component=web msg="Listening on" address=[::]:9090
ts=2023-03-09T13:57:32.921Z caller=tls_config.go:235 level=info component=web msg="TLS is disabled." http2=false address=[::]:9090
ts=2023-03-09T13:57:32.923Z caller=head.go:564 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2023-03-09T13:57:32.924Z caller=head.go:608 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=617.329µs
ts=2023-03-09T13:57:32.924Z caller=head.go:614 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2023-03-09T13:57:32.961Z caller=head.go:685 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=1
ts=2023-03-09T13:57:32.962Z caller=head.go:685 level=info component=tsdb msg="WAL segment loaded" segment=1 maxSegment=1
ts=2023-03-09T13:57:32.962Z caller=head.go:722 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=218.631µs wal_replay_duration=37.887971ms wbl_replay_duration=202ns total_replay_duration=39.031702ms
ts=2023-03-09T13:57:32.964Z caller=main.go:1014 level=info fs_type=EXT4_SUPER_MAGIC
ts=2023-03-09T13:57:32.964Z caller=main.go:1017 level=info msg="TSDB started"
ts=2023-03-09T13:57:32.964Z caller=main.go:1197 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2023-03-09T13:57:32.965Z caller=main.go:1234 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=838.48µs db_storage=1.592µs remote_storage=1.745µs web_handler=635ns query_engine=1.301µs scrape=310.656µs scrape_sd=94.42µs notify=1.411µs notify_sd=4.727µs rules=1.553µs tracing=28.148µs
ts=2023-03-09T13:57:32.965Z caller=main.go:978 level=info msg="Server is ready to receive web requests."
ts=2023-03-09T13:57:32.965Z caller=manager.go:974 level=info component="rule manager" msg="Starting rule manager..."
三个容器都要查看。以上只做一个示范。
三、最后一步,最重要一步。
添加监控节点。
在prometheus中一个监控节点就是一个node
在prometheus.yml中配置节点。
global:
scrape_interval: 15s # 默认抓取周期
external_labels:
monitor: 'codelab-monitor'
scrape_configs:
- job_name: 'node-exporter' #服务的名称
scrape_interval: 5s
metrics_path: /metrics #获取指标的url
static_configs:
- targets: ['localhost:9100'] # 这个为监听指定服务服务的ip和port,需要修改为自己的ip,貌似云服务必须用公网ip
四、注意,要想监控其他主机,只需要在其他主机上安装node-exporter即可。
标签:node,09T13,compose,level,grafana,prometheus,2023,go,docker From: https://www.cnblogs.com/guokaixuan/p/17201764.html