Prometheus、node-exporter、Grafana 容器化部署方案认知

举报
山河已无恙 发表于 2025/03/20 10:18:50 2025/03/20
【摘要】 写在前面博文内容涉及 Prometheus、node-exporter、Grafana docker 和 k8s 两种容器话部署方案理解不足小伙伴帮忙指正 :),生活加油 99%的焦虑都来自于虚度时间和没有好好做事,所以唯一的解决办法就是行动起来,认真做完事情,战胜焦虑,战胜那些心里空荡荡的时刻,而不是选择逃避。不要站在原地想象困难,行动永远是改变现状的最佳方式持续分享技术干货,感兴趣小伙...

写在前面


  • 博文内容涉及 Prometheus、node-exporter、Grafana docker 和 k8s 两种容器话部署方案
  • 理解不足小伙伴帮忙指正 :),生活加油

99%的焦虑都来自于虚度时间和没有好好做事,所以唯一的解决办法就是行动起来,认真做完事情,战胜焦虑,战胜那些心里空荡荡的时刻,而不是选择逃避。不要站在原地想象困难,行动永远是改变现状的最佳方式

持续分享技术干货,感兴趣小伙伴可以关注下 ^_^


docker 部署


创建一个专用网络,这个网络一般是业务对应网络

[root@ecs-hce ]# docker network create oa-net
8858411315a892cd61bbe8b31411595dd8b013792fd05ab68806f3ca5572c3a7

部署 Prometheus

下载 Prometheus 镜像, 查看 Prometheus 镜像

[root@ecs-hce ~]# cd ~ && docker pull prom/prometheus
Using default tag: latest
latest: Pulling from prom/prometheus
9fa9226be034: Pull complete
1617e25568b2: Pull complete
0af4f5a1aba4: Pull complete
524450aac7ac: Pull complete
3427b0266353: Pull complete
87f5816afa35: Pull complete
fe81a20504a7: Pull complete
bfac845c5ca1: Pull complete
6c1aa870016a: Pull complete
6126ba6f9b4f: Pull complete
68d61c64cf66: Pull complete
5e5897603b11: Pull complete
Digest: sha256:beb5e30ffba08d9ae8a7961b9a2145fc8af6296ff2a4f463df7cd722fcbfc789
Status: Downloaded newer image for prom/prometheus:latest
[root@ecs-hce ~]# docker images|grep prometheus
prom/prometheus      latest              2a72b385beaf        3 weeks ago         250MB

启动 Prometheus 容器, 这里需要注意端口和网络

[root@ecs-hce ~]# docker run -id --name=prom --network=oa-net -p  9090:9090 prom/prometheus
b1f8fb907b6963e84df3e9ca1cb7e190d9089e582ebff20956c33321fcbd219c

验证 Prometheus,在浏览器输入 EIP:9090 访问 Prometheus

点击 Endpoint 下的链接,此时会发现页面无法访问,需要修改配置文件

[root@ecs-hce ~]# mkdir prometheus
[root@ecs-hce ~]# docker cp prom:/etc/prometheus/prometheus.yml ./prometheus
[root@ecs-hce ~]# vim prometheus/prometheus.yml

```yaml
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"]

修改 targets 对应的值为 eip:9090,并且保存退出

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: ["120.46.64.191:9090"]

停止 prom 容器,删除 prom 容器,重新启动 Prometheus 容器

[root@ecs-hce ~]# docker stop prom
prom
[root@ecs-hce ~]# docker rm prom
prom

需要注意 -v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml,这里直接映射的文件

[root@ecs-hce ~]# docker run -id --name=prom --network=oa-net -p  9090:9090 -v /root/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml  prom/prometheus
08905b1011feb7e98431f53e53eadf8f5bb1ce6d44eb0894fb13b9598efa4c1f
[root@ecs-hce ~]#

通过 EIP:9090 访问 Prometheus,并进入 Status 选项下 Targets 页面

部署 node-exporter

下载 node-exporter 镜像

[root@ecs-hce ~]# docker pull prom/node-exporter
Using default tag: latest
latest: Pulling from prom/node-exporter
2abcce694348: Pull complete
455fd88e5221: Pull complete
324153f2810a: Pull complete
Digest: sha256:4cb2b9019f1757be8482419002cb7afe028fdba35d47958829e4cfeaf6246d80
Status: Downloaded newer image for prom/node-exporter:latest

启动 node-exporter,注意网络和端口配置

[root@ecs-hce ~]# docker run -id --name=node-exporter --network=oa-net -p 9100:9100 prom/node-exporter
933a43e1cebbdc2192928c69b0e0f60faf84156601caed0548f08cafe979923d

修改 Prometheus 配置文件,重新启动 prometheus

[root@ecs-hce ~]# vim prometheus/prometheus.yml
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "node-exporter"
    static_configs:
      - targets: ["120.46.64.191:9100"]
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["120.46.64.191:9090"]
[root@ecs-hce ~]# docker restart prom
prom
[root@ecs-hce ~]#

通过 EIP:9090 访问 Prometheus,在 Status—Targets 页面下,可以看到新配置的 node-exporter 数据源。

部署 Grafana

下载 Grafana 镜像

[root@ecs-hce ~]# docker pull grafana/grafana
Using default tag: latest
latest: Pulling from grafana/grafana
96526aa774ef: Pull complete
c7ba78600384: Pull complete
c27e091f8ead: Pull complete
1d06c4d0ba22: Pull complete
183afb97c8c4: Pull complete
409b84eddcee: Pull complete
10a0006e04fc: Downloading [==========>                                        ]  11.79MB/53.8MB
24decdaaac3e: Download complete
10a0006e04fc: Pull complete
24decdaaac3e: Pull complete
e178ee908fb8: Pull complete
d42dc690a758: Pull complete
Digest: sha256:7567a7c70a3c1d75aeeedc968d1304174a16651e55a60d1fb132a05e1e63a054
Status: Downloaded newer image for grafana/grafana:latest
[root@ecs-hce ~]#
[root@ecs-hce ~]#
[root@ecs-hce ~]# docker images grafana/grafana
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
grafana/grafana     latest              2fec87cd4362        2 weeks ago         406MB

启动 Grafana 容器,注意这里的端口和网络,grafana 并不需要和上面的组件放到同一个网络里面

[root@ecs-hce ~]# docker run -d --name grafana -p 3000:3000 grafana/grafana
ab78373b51595e6de467d2e42f119309f9a301ff31e7ed89525334f7e0ed5e47
[root@ecs-hce ~]#

通过 EIP:3000 访问 Grafana,初始账号为 admin,密码为 admin

登录后,DATA SOURCE,增加数据源

到这里容器化部署Prometheus、node-exporter、Grafana就完成了,之后可以根据情况配置面板

k8s 部署

需要安装监控(Metrics Server),这里不多讲

curl -Ls https://api.github.com/repos/kubernetes-sigs/metrics-server/tarball/v0.3.6 -o metrics-server-v0.3.6.tar.gz

kube-prometheus-stack helm 方式安装

k8s 环境安装有封装好的 helm chart kube-prometheus-stack-30.0.1

获取地址:

镜像拉不了的问题,直接替换不好找,这里把 charts 包下载下来,然后通过 helm template 转化为具体的 yaml 文件。替换对应的镜像。

但是这样还一个问题,一些 CRD 不会预先安装,尤其是多 master 的情况,这里你可以多试几次,说不定就可以了,github上有人提了,貌似没有很好的解决方案,我的解决办法是先用 helm 安装,然后卸载,卸载的时候不会卸载 crd,然后在运行 生成的 yaml 文件。

┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm]
└─$cd kube-prometheus-stack
┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm/kube-prometheus-stack]
└─$ls
Chart.lock  charts  Chart.yaml  CONTRIBUTING.md  crds  README.md  templates  values.yaml
┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm/kube-prometheus-stack]
└─$helm install kube-prometheus-stack .

┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm/kube-prometheus-stack]
└─$helm template . > ../kube-prometheus-stack.yaml

执行应用

┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm]
└─$kubectl apply  -f kube-prometheus-stack.yaml

执行完需要修改svc 为 NodePort 当然如果有 Ingress 控制器,或者 LB ,可以配置其他类型。

┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm]
└─$kubectl get svc
NAME                                      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
alertmanager-operated                     ClusterIP   None             <none>        9093/TCP,9094/TCP,9094/UDP   27m
kubernetes                                ClusterIP   10.96.0.1        <none>        443/TCP                      40h
prometheus-operated                       ClusterIP   None             <none>        9090/TCP                     27m
release-name-grafana                      NodePort    10.111.188.209   <none>        80:30203/TCP                 30m
release-name-kube-promethe-alertmanager   ClusterIP   10.97.17.175     <none>        9093/TCP                     30m
release-name-kube-promethe-operator       ClusterIP   10.107.60.174    <none>        443/TCP                      30m
release-name-kube-promethe-prometheus     ClusterIP   10.108.163.61    <none>        9090/TCP                     30m
release-name-kube-state-metrics           ClusterIP   10.102.37.208    <none>        8080/TCP                     30m
release-name-prometheus-node-exporter     ClusterIP   10.100.5.155     <none>        9100/TCP                     30m

登录用户名和密码获取

┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm]
└─$kubectl get secrets release-name-grafana -o jsonpath='{.data.admin-user}' | base64 -d
admin┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm]
└─$kubectl get secrets release-name-grafana -o jsonpath='{.data.admin-password}' | base64 -d
prom-operator┌──[root@vms100.liruilongs.github.io]-[~/ansible/helm]
└─$

得到用户名密码:admin/prom-operator

博文部分内容参考

© 文中涉及参考链接内容版权归原作者所有,如有侵权请告知 :)



© 2018-至今 , 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。