Skip to content

Commit 7321365

Browse files
authored
Merge pull request #2 from xiaozongyang/deploy/add_k8s_deploy
1. support docker multi-arch docker images 2. push images to docker hub 3. add Chinese README
2 parents d0ce5f3 + 4d01ff0 commit 7321365

File tree

5 files changed

+190
-12
lines changed

5 files changed

+190
-12
lines changed

Dockerfile

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
FROM golang:1.24.0 as builder
1+
FROM --platform=$BUILDPLATFORM golang:1.24.0 as builder
22

3-
WORKDIR /workspace
3+
WORKDIR /app
44
ENV GOPROXY=https://goproxy.cn,direct
55

6+
COPY go.mod go.sum ./
7+
RUN go mod download
8+
69
COPY . .
710

8-
#### Build
9-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build .
1011

11-
CMD ["/workspace/kube-node-metrics"]
12+
ARG TARGETARCH
13+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -o kube-node-metrics .
14+
15+
FROM scratch
16+
WORKDIR /
17+
COPY --from=builder /app/kube-node-metrics .
18+
19+
EXPOSE 19191
20+
21+
CMD ["/kube-node-metrics"]

Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.PHONY:
2+
build:
3+
go fmt ./...
4+
go mod tidy
5+
go vet ./...
6+
go build .
7+
8+
.PHONY:
9+
build-docker:
10+
docker buildx build --platform linux/amd64,linux/arm64 \
11+
-t xiaozongyang/kube-node-metrics:latest \
12+
--push .
13+
14+
.PHONY:
15+
clean:
16+
rm kube-node-metrics

README-zh.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# README [Go To English Version](README.md)
2+
3+
本项目遵守 MIT 协议。
4+
5+
本项目灵感来源于 [kube-state-metrics](https://github.com/kubernetes/kube-state-metrics)[kubectl](https://github.com/kubernetes/kubectl)
6+
7+
本项目实现了一个暴露 kubernetes 节点资源指标的服务,目前暴露的指标有:
8+
```
9+
kube_node_metrics_cpu_request
10+
kube_node_metrics_cpu_limit
11+
kube_node_metrics_mem_request_bytes
12+
kube_node_metrics_mem_limit_bytes
13+
```
14+
15+
本项目暴露出的资源申请(request)和限制()g(limit)的指标值应该和 `kubectl describe node <node>` 命令的结果相同。
16+
17+
## 快速开始
18+
### 在 Minikube 上试用
19+
请确保你已经安装了 [minikube](https://github.com/kubernetes/minikube)
20+
21+
在你终端中执行下列命令:
22+
```
23+
minikube start
24+
kubectl apply -f deploy.yaml
25+
```
26+
27+
### 在 Kubernetes 上部署
28+
```bash
29+
kubectl apply -f deploy.yaml
30+
```
31+
32+
## 构建
33+
### 手动构建二进制
34+
```bash
35+
go build .
36+
```
37+
38+
### 构建 docker 镜像
39+
```bash
40+
docker build . -t <image>:<tag>
41+
```
42+
43+
## 运维
44+
本项目也暴露了一个 `kube_node_metrics_last_full_sync_ok_time_seconds` ata 指标,用于表示最后一次全量同步成功的时间。 根据这个指标可以配置报警,例如通过 `time() - kube_node_metrics_last_full_sync_ok_time_seconds > 300` 来通知已经 5 分钟没有同步成功的情况。
45+
46+
## TODO
47+
1. 支持暴露节点 k8s 节点标签,范围通过前缀指定

README.md

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Kube Node Metrics
1+
# README [查看中文版](README-zh.md)
2+
3+
This project is licensed under the MIT License.
24

35
This project is inspired by [kube-state-metrics](https://github.com/kubernetes/kube-state-metrics) and [kubectl](https://github.com/kubernetes/kubectl).
46

@@ -13,14 +15,35 @@ kube_node_metrics_mem_limit_bytes
1315
These samples values should be equal to the result of `kubectl describe node <node>`.
1416

1517
## Getting started
16-
TODO: add docker image
1718

18-
### build manually
19+
### Try in Minikube
20+
Please ensure you have [minikube](https://github.com/kubernetes/minikube) installed.
21+
22+
Run with following commands in your terminal:
23+
```
24+
minikube start
25+
kubectl apply -f deploy.yaml
26+
```
27+
28+
### Run in Kubernetes
29+
```bash
30+
kubectl apply -f deploy.yaml
31+
```
32+
33+
## Build
34+
### build binary manually
1935
```bash
2036
go build .
2137
```
2238

23-
# TODO
24-
1. release with docker image
25-
1. support custom k8s api server address
26-
1. support mutiple arch docker iamges
39+
### build docker image
40+
```bash
41+
docker build . -t <image>:<tag>
42+
```
43+
44+
## Operating
45+
This exporter also exposed a metric `kube_node_metrics_last_full_sync_ok_time_seconds` which indicates the last full sync timestamp in seconds. You can create alert rule to monitor the exporter health with the expression `time() - kube_node_metrics_last_full_sync_ok_time_seconds > 300` and you will receive alert if the exporter is not sync sucessfully for 300 seconds.
46+
47+
48+
## TODO
49+
1. support exposed node labels match given pattern

deploy.yaml

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: kube-node-metrics
5+
namespace: default
6+
7+
---
8+
apiVersion: rbac.authorization.k8s.io/v1
9+
kind: ClusterRole
10+
metadata:
11+
name: kube-node-metrics
12+
rules:
13+
- apiGroups: [""]
14+
resources: ["nodes"]
15+
verbs: ["list", "watch"]
16+
- apiGroups: [""]
17+
resources: ["pods"]
18+
verbs: ["list", "watch"]
19+
---
20+
apiVersion: rbac.authorization.k8s.io/v1
21+
kind: ClusterRoleBinding
22+
metadata:
23+
name: kube-node-metrics
24+
roleRef:
25+
apiGroup: rbac.authorization.k8s.io
26+
kind: ClusterRole
27+
name: kube-node-metrics
28+
subjects:
29+
- kind: ServiceAccount
30+
name: kube-node-metrics
31+
namespace: default
32+
33+
---
34+
apiVersion: apps/v1
35+
kind: Deployment
36+
metadata:
37+
name: kube-node-metrics
38+
namespace: default
39+
spec:
40+
replicas: 1
41+
selector:
42+
matchLabels:
43+
app: kube-node-metrics
44+
template:
45+
metadata:
46+
labels:
47+
app: kube-node-metrics
48+
spec:
49+
serviceAccountName: kube-node-metrics
50+
containers:
51+
- name: kubep-node-metrics
52+
image: docker.io/xiaozongyang/kube-node-metrics:latest
53+
env:
54+
- name: FULL_SYNC_INTERVAL
55+
value: "2m"
56+
ports:
57+
- containerPort: 19191
58+
name: metrics
59+
resources:
60+
limits:
61+
memory: "2Gi"
62+
cpu: "2000m"
63+
requests:
64+
memory: "64Mi"
65+
cpu: "50m"
66+
tolerations:
67+
- operator: Exists
68+
---
69+
apiVersion: v1
70+
kind: Service
71+
metadata:
72+
name: kube-node-metrics
73+
namespace: default
74+
labels:
75+
app: kube-node-metrics
76+
spec:
77+
selector:
78+
app: kube-node-metrics
79+
ports:
80+
- port: 19191
81+
targetPort: 19191
82+
name: metrics

0 commit comments

Comments
 (0)