生产安装 Prometheus Operator
生产安装 Prometheus Operator
1.克隆代码 根据 git描述 支持k8s版本进行克隆 我的 1.22 克隆0.10
git clone -b release-0.10 https://gitee.com/x602/kube-prometheus.git
2.修改配置文件
修改存储时间默认24h 修改持久化存储
cd kube-prometheus/manifests/
cat prometheus-prometheus.yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.32.1
name: k8s
namespace: monitoring
spec:
#指定保存15天
retention: 15d
alerting:
alertmanagers:
- apiVersion: v2
name: alertmanager-main
namespace: monitoring
port: web
# 挂载 动态存储
storage:
volumeClaimTemplate:
spec:
# 动态存储名称 csi-rbd-sc 执行获取 kubectl get sc
storageClassName: csi-rbd-sc
resources:
requests:
# 大小根据保留时间和监控数据而定
storage: 1000Gi
enableFeatures: []
externalLabels: {}
image: quay.io/prometheus/prometheus:v2.32.1
nodeSelector:
kubernetes.io/os: linux
podMetadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.32.1
podMonitorNamespaceSelector: {}
podMonitorSelector: {}
probeNamespaceSelector: {}
probeSelector: {}
replicas: 2
resources:
requests:
memory: 400Mi
ruleNamespaceSelector: {}
ruleSelector: {}
securityContext:
fsGroup: 2000
runAsNonRoot: true
runAsUser: 1000
serviceAccountName: prometheus-k8s
serviceMonitorNamespaceSelector: {}
serviceMonitorSelector: {}
version: 2.32.1
修改 grc 镜像 找不到就在当前目录搜索
grep k8s.gcr.io -r .
vi kubeStateMetrics-deployment.yaml 修改镜像为 bitnami/kube-state-metrics:2.3.0
vi prometheusAdapter-deployment.yaml 修改镜像为 willdockerhub/prometheus-adapter:v0.9.1
3.应用 Prometheus Operator:
cd kube-prometheus
kubectl apply --server-side -f manifests/setup
until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done
kubectl apply -f manifests/
```
### 4.验证
查看 Operator 容器的状态:
```
kubectl get po -n monitoring
```
将 Grafana 的 Service 改成 NodePort 类型 并查看:
```
kubectl edit svc grafana -n monitoring
kubectl get svc grafana -n monitoring
```
添加 prometheus 的 NodePort类型 yaml,也可以直接像上面的 Grafana 修改
`labels` 根据实际填写
cat prometheus-k8s-np.yaml
```
apiVersion: v1
kind: Service
metadata:
name: prometheus-k8s-np
namespace: monitoring
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.32.1
spec:
type: NodePort
ports:
- port: 9090
targetPort: 9090
nodePort: 30090
protocol: TCP
selector:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.32.1
```
```
kubectl apply -f prometheus-k8s-np.yaml
```
## 拓展 直接修改 保留时间
```
#找到 定义 kind 为 prometheus 的资源直接修改
# kubectl get prometheus -A
NAMESPACE NAME VERSION REPLICAS AGE
monitoring k8s 2.32.1 2 47h
# kubectl edit prometheus -n monitoring k8s
```
参考文档:持久化存储 https://blog.csdn.net/qq_23435961/article/details/110957974