一个计算机技术爱好者与学习者

0%

好好学Sealos:Sealos制作 kube-prometheus-stack 集群镜像

1. 前言

本文中,我们会基于sealos制作一个kube-prometheus-stack集群镜像。
使用这个集群镜像,我们能够在sealos拉起的K8S集群中,一键部署kube-prometheus-stack。

参考文档:

2. 前置条件

安装sealos4.2.0,拉起K8S 1.24.8集群(同时安装helm),具体方法参考《sealos入门篇》

3. 制作kube-prometheus-stack集群镜像

1、添加prometheus-community chart repo

1
2
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts 
helm repo update

2、下载chart包

1
2
3
mkdir prometheus && cd prometheus
helm search repo prometheus-community/kube-prometheus-stack -l
helm pull prometheus-community/kube-prometheus-stack --version 45.25.0

这里下载 chart45.25.0 版本,对应 app v0.63.0 版本。
下载的文件名为:kube-prometheus-stack-45.25.0.tgz

3、修改chart

1
2
3
tar -xzvf kube-prometheus-stack-45.25.0.tgz
vim kube-prometheus-stack/values.yaml
tar -czvf kube-prometheus-stack-45.25.0-custom.tgz kube-prometheus-stack

修改chart的values.yaml,给一些默认值,然后重新打包chart,后续Dockerfile中的CMD中可以少填一些参数。

具体修改的内容,参考文档《K8S中安装配置Prometheus官方套装》

3、添加镜像列表

1
2
3
helm template prometheus kube-prometheus-stack | grep 'image:'
mkdir -p images/shim/
vim images/shim/images-list.txt

内容为:

1
2
3
4
5
6
7
8
9
quay.mirrors.ustc.edu.cn/prometheus/node-exporter:v1.5.0
quay.mirrors.ustc.edu.cn/kiwigrid/k8s-sidecar:1.22.0
docker.io/grafana/grafana:9.5.1
docker.io/dyrnq/kube-state-metrics:v2.8.2
quay.mirrors.ustc.edu.cn/prometheus-operator/prometheus-operator:v0.63.0
quay.mirrors.ustc.edu.cn/prometheus/alertmanager:v0.25.0
quay.mirrors.ustc.edu.cn/prometheus/prometheus:v2.42.0
docker.io/bats/bats:v1.4.1
docker.io/dyrnq/kube-webhook-certgen:v20221220-controller-v1.5.1-58-g787ea74b6

4、编写Dockerfile

1
2
3
4
5
FROM scratch
COPY ../prometheus .
CMD ["kubectl apply -f kube-prometheus-stack/crds/ --server-side \
&& helm install prometheus kube-prometheus-stack-45.25.0-custom.tgz \
-n prometheus --create-namespace"]

5、构建集群镜像

1
2
sealos build -f Dockerfile -t docker.io/voidking/kube-prometheus-stack:v0.63.0 .
sealos inspect docker.io/voidking/kube-prometheus-stack:v0.63.0

4. 使用kube-prometheus-stack集群镜像

4.1. 自定义安装kube-prometheus-stack

1
2
3
4
5
6
7
8
9
sealos run docker.io/voidking/kube-prometheus-stack:v0.63.0 --cmd="kubectl apply -f kube-prometheus-stack/crds/ --server-side \
&& helm install prometheus kube-prometheus-stack-45.25.0-custom.tgz \
-n prometheus --create-namespace \
--set grafana.defaultDashboardsTimezone=Asia/Shanghai \
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false \
&& kubectl expose deployment prometheus-grafana \
--port=80 --target-port=3000 --type=NodePort --name grafana-external -n prometheus"

kubectl get all -n prometheus

4.2. 上传集群镜像

上传到公共镜像仓库

1
2
3
4
sealos push docker.io/voidking/kube-prometheus-stack:v0.63.0
sealos tag docker.io/voidking/kube-prometheus-stack:v0.63.0 hub.sealos.cn/voidking/kube-prometheus-stack:v0.63.0
sealos login -k config.yml hub.sealos.cn
sealos push hub.sealos.cn/voidking/kube-prometheus-stack:v0.63.0

上传到私有镜像仓库

1
2
3
sealos tag docker.io/voidking/kube-prometheus-stack:v0.63.0 harbor.voidking.com/sealos/kube-prometheus-stack:v0.63.0
sealos login harbor.voidking.com
sealos push harbor.voidking.com/sealos/kube-prometheus-stack:v0.63.0