prometheus 监控容器
rometheus作为云原生应用程序的第一个开源监控工具CNCF就毕业监控工具而言,开发者是Prometheus寄予巨大希望。 在Kubernetes许多人认为在社区里Prometheus成为容器监控标准的制定者,是容器场景监控的第一方案。
什么是 cAdvisor
cAdvisor (Container Advisor) 是 Google 开源容器监控工具可用于监控容器资源的使用和性能。用于收集、聚合、处理和导出正在运行的容器的相关信息。具体来说,该组件将记录资源隔离参数、历史资源使用、完整历史资源使用的直方图和网络统计信息。
cAdvisor 本身就对 Docker 容器支持,并尽可能支持其他类型的容器,努力兼容和适应所有类型的容器。
从以上介绍我们可以知道,cAdvisor 用于监控容器发动机。由于其监控的实用性,Kubernetes 默经默认了 Kubelet 因此,我们不需要单独部署 cAdvisor 组件直接暴露节点中容器运行的信息 Kubelet 组件提供的指标采集地址。
Cadvisor 收集,通过 Prometheus 用作数据源 Grafana 进行展示
环境说明
主机 | ip | 安装软件 |
---|---|---|
master | 192.168.111.152 | docker-ce |
client | 192.168.111.153 | docker-ce |
master、client主机安装docker并配置镜像加速
配置docker-ce 源
[root@master ~]# cd /etc/yum.repos.d/ [root@master yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo [root@client ~]# cd /etc/yum.repos.d/ [root@client yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
安装 docker-ce 依靠包和工具
[root@master ~]# dnf -y install yum-utils device-mapper-persistent-data lvm2 [root@master ~]# yum -y install docker-ce --allowerasing [root@client ~]# dnf -y install yum-utils device-mapper-persistent-data lvm2 [root@client ~]# yum -y install docker-ce --allowerasing
安装后使用 docker version 命令查看docker的版本信息
[root@master ~]# docker version Client: Docker Engine - Community Version: 20.10.12 API version: 1.41 Go version: go1.16.12 Git commit: e91ed57 Built: Mon Dec 13 11:45:22 2021 OS/Arch: linux/amd64 Context: default Experimental: true [root@client ~]# docker version Client: Docker Engine - Community Version: 20.10.12 API version: 1.41 Go version: go1.16.12 Git commit: e91ed57 Built: Mon Dec 13 11:45:22 2021 OS/Arch: linux/amd64 Context: default Experimental: true
配置docker镜像 加速
[root@master ~]# mkdir -p /etc/docker [root@master ~]# vi /etc/docker/daemon.json { "registry-mirrors": ["https://gpdm6wat.mirror.aliyuncs.com"] } [root@master ~]# systemctl enable --now docker Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service. [root@client ~]# mkdir -p /etc/docker [root@client ~]# vi /etc/docker/daemon.json { "registry-mirrors": ["https://gpdm6wat.mirror.aliyuncs.com"] } [root@client ~]# systemctl enable --now docker Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
等待docker两台主机部署完成后,master
主机上拉取prom/Prometheus官方镜像
[root@master ~]# docker pull prom/prometheus Using default tag: latest latest: Pulling from prom/prometheus 3cb635b06aa2: Pull complete 34f699df6fe0: Pull complete 33d6c9635e0f: Pull complete f2af7323bed8: Pull complete c16675a6a294: Pull complete 827843f6afe6: Pull complete 3d272942eeaf: Pull complete 7e785cfa34da: Pull complete 05e324559e3b: Pull complete 170620261a59: Pull complete ec35f5996032: Pull complete 5509173eb708: Pull complete Digest: sha256:cb9817249c346d6cfadebe383ed3b3cd4c540f623db40c4ca00da2ada45259bb Status: Downloaded newer image for prom/prometheus:latest docker.io/prom/prometheus:latest [root@master ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE prom/prometheus latest a3d385fc29f9 13 days ago 201MB
在client
上获取prometheus.yml配置文件
# 将prometheus将安装包上传至主机,解压,将prometheus.yaml传输配置文件master主机的/opt目录中 [root@client ~]# ls anaconda-ks.cfg prometheus-2.32.1.linux-amd64.tar.gz [root@client ~]# tar xf prometheus-2.32.1.linux-amd64.tar.gz [root@client prometheus-2.32.1.linux-amd64]# scp /root/prometheus-2.32.1.linux-amd64/prometheus.yml 192.168.111.152:/opt/promentheus.yml The authenticity of host '192.168.111.152 (192.168.111.152)' can't be established. ECDSA key figerprint is SHA256:QpD0Aqv7AOmOAiN59YP4MsXykPbnNu7+a0MeU6yHACU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.111.152' (ECDSA) to the list of known hosts.
root@192.168.111.152's password:
prometheus.yml
master
主机上使用官方镜像运行prometheus 容器,并进行端口和目录文件映射
[root@master ~]# cat /opt/prometheus.yml
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
# Load rules once and periodically evaluate them accordin
g 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"]
# 映射端口和配置文件到主机上且设置随docker启动而启动容器
[root@master ~]# docker run -d --name prometheus --restart always -p 9090:9090 -v /opt/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
bf7c4b13ce354a521666d5e0f86b836355f99cb8faa5e78ae219157ff5ed9898
# 查看容器运行状态
[root@master ~]# docker ps | grep prometheus
bf7c4b13ce35 prom/prometheus "/bin/prometheus --c…" 2 minutes ago Restarting (2) 14 seconds ago prometheus