GO语言写Prometheus自定义node-exporter的Docker容器测试

1. 安装docker-compose

执行以下命令,安装docker-compose到CentOS7.9环境中:

shell 复制代码
# 下载二进制文件
sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.7/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 给文件赋予执行的权限
sudo chmod +x /usr/local/bin/docker-compose
# 创建软链接到执行目录
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

安装完成后输入命令验证docker-compose安装成功:

shell 复制代码
docker-compose --version

2. 使用go语言编写node-exporter

创建文件夹04_prometheus_test,执行以下命令初始化go环境:

shell 复制代码
cd 04_prometheus_test
go mod init 04_prometheus_test

由于需要用到prometheus的go语言sdk,所以需要下载包:

shell 复制代码
go get github.com/prometheus/client_golang/prometheus  
go get github.com/prometheus/client_golang/prometheus/promhttp

执行完成后写代码:

go 复制代码
package main  
  
import (  
	"net/http"  
	"log"  
  
	"github.com/prometheus/client_golang/prometheus"  
	"github.com/prometheus/client_golang/prometheus/promhttp"  
)  
  
// 自定义一个计数器指标  
var helloWorldCounter = prometheus.NewCounter(prometheus.CounterOpts{  
	Name: "hello_world_counter_total",  
	Help: "Total number of hello world invocations.",  
})  
  
// 定义一个简单的HTTP处理器,每次调用都会增加计数器  
func helloWorldHandler(w http.ResponseWriter, r *http.Request) {  
	helloWorldCounter.Inc()  
	w.Write([]byte("Hello, World!"))  
}  
  
func main() {  
	// 注册自定义指标  
	prometheus.MustRegister(helloWorldCounter)  
  
	// 设置HTTP服务器监听端口  
	http.Handle("/metrics", promhttp.Handler())  
	http.HandleFunc("/hello", helloWorldHandler)  
  
	// 启动HTTP服务器  
	log.Fatal(http.ListenAndServe(":8080", nil))  
}

这个程序暴露<ip>:8080/metrics给prometheus获取数据,<ip>:8080/hello可以增加计数。

执行编译生成可执行文件:

shell 复制代码
go build .

3. docker镜像制作

写Dockerfile,用于创建Docker镜像:

Dockerfile 复制代码
FROM centos
LABEL maintainer="kaijessen@gmail.com"
COPY . /app
WORKDIR /app
RUN chmod a+x /app/*
EXPOSE 8080
ENTRYPOINT ["/app/04_prometheus_test"]

执行命令生成镜像到本地库:

shell 复制代码
docker build -t prom_custom_ne:latest .

完成后执行docker images可以看到生成新的镜像prom_custom_ne。

4. docker-compose制作

写一个docker-compose.yml文件,用来将镜像启动起来。

yml 复制代码
version: '3.5'  
  
services:  
  prometheus:  
    image: docker.io/prom/prometheus  
    container_name: prometheus  
    networks:  
      - prom_test_net  
    ports:  
      - "9090:9090"  
    volumes:  
      - prom_test_vol:/etc/prometheus/
  
  custom_ne:  
    image: prom_custom_ne  
    container_name: custom_ne  
    networks:  
      - prom_test_net  
    ports:
      - "8080:8080"
  
networks:  
  prom_test_net:  
    driver: bridge  
  
volumes:  
  prom_test_vol:  
    driver: local  
    driver_opts:  
      type: none  
      device: /opt/prometheus/
      o: bind

这个文件启动两个服务,prometheus和custom_ne;把宿主机的/opt/prometheus/目录挂在了prometheus容器的/etc/prometheus目录下;将prometheus:9090的端口和custome_ne:8080绑定到宿主机对应端口以便访问;创建一个bridge供两个容器互相通信。其中值得注意的是/opt/prometheus目录下配置一个prometheus.yml文件,用于配置监控目标,如下:

yml 复制代码
scrape_configs:  
  - job_name: 'custom_exporter'  
    static_configs:  
      - targets: ['custom_ne:8080']

输入以下命令启动docker-compose。

shell 复制代码
docker-compose up -d

5. 结果展示

访问宿主机的9090端口,查询自定义指标hello_world_counter_total,结果展示如下:

期间我通过宿主机8080/hello目录访问过几次,所以可以看到自定义数值在增加。

相关推荐
雪域迷影2 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
莹雨潇潇3 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
怪我冷i4 小时前
使用vscode调试wails项目(golang桌面GUI)
vscode·golang
tangdou3690986557 小时前
Docker系列-5种方案超详细讲解docker数据存储持久化(volume,bind mounts,NFS等)
docker·容器
小吴同学(wlx)7 小时前
Golang 进阶3—— 协程&管道
golang
技术卷7 小时前
GO网络编程(三):海量用户通信系统1:登录功能初步
golang·网络编程
虽千万人 吾往矣10 小时前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
漫无目的行走的月亮10 小时前
在Docker中运行微服务注册中心Eureka
docker
大道归简13 小时前
Docker 命令从入门到入门:从 Windows 到容器的完美类比
windows·docker·容器
zeruns80214 小时前
如何搭建自己的域名邮箱服务器?Poste.io邮箱服务器搭建教程,Linux+Docker搭建邮件服务器的教程
linux·运维·服务器·docker·网站