欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > k8s 配置安装社区版 Prometheus

k8s 配置安装社区版 Prometheus

2025/5/24 23:19:25 来源:https://blog.csdn.net/Guzarish/article/details/148127389  浏览:    关键词:k8s 配置安装社区版 Prometheus

第一种:在线安装

helm 在线部署 Prometheus,通过 value.yaml 进行简单配置

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo updatekubectl create namespace monitoringhelm pull prometheus-community/prometheustar -xzf prometheus-27.13.0.tgz && cd prometheus/# 修改value.yaml配置文件,配置persistentVolume持久化相关helm install prometheus prometheus-community/prometheus -f values.yaml --namespace monitoring

第二种:离线安装

配置共享存储

Prometheus 需要配置持久化存储,防止数据丢失

服务端

服务端安装 NFS 服务

sudo apt install nfs-kernel-server

创建共享目录,在服务器端创建 /nfs 目录。

mkdir /nfs
chmod -R 777 /nfs # 设置文件权限

nfs目录下只给了默认权限,不设置权限,会报错

GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied

编写配置文件

vim /etc/exports
#[任意主机所有权限]
/nfs   *(rw,sync,insecure,no_subtree_check,no_root_squash)

重启 NFS 服务

sudo service nfs-kernel-server restart

常用命令工具

#在安装 NFS 服务器时,已包含常用的命令行工具,无需额外安装
#显示已经 mount 到本机 NFS 目录的客户端机器
sudo showmount -e localhost
#将配置文件中的目录全部重新 export 一次,无需重启服务
sudo exportfs -rv
#查看 NFS 的运行状态
sudo nfsstat
#查看 rpc 执行信息,可以用于检测 rpc 运行情况
sudo rpcinfo

客户端

需要连接服务端的节点,例如 node 节点

安装客户端工具

sudo apt install nfs-common

查看 NFS 服务器上的共享目录

#显示指定的 NFS 服务器(假设 IP 地址为 192.168.58.29)上 export 出来的目录
sudo showmount -e 192.168.58.29

创建本地挂载目录

sudo mkdir -p /nfs

挂载共享目录

#假设 NFS 服务器 IP为 192.168.58.29,可以如下设置挂载  
sudo mount -t nfs 192.168.58.29:/nfs /nfs

查看客户端挂载信息

df -h

开始安装 Prometheus

下载 Prometheus 压缩包

在 releases 中,找到自己想安装的版本:https://github.com/prometheus-community/helm-charts/releases/

wget https://github.com/prometheus-community/helm-charts/releases/download/prometheus-27.13.0/prometheus-27.13.0.tgz

配置 Prometheus 命名空间

kubectl get namespace monitoring || kubectl create namespace monitoring

配置持久化卷

创建 prometheus-pv.yaml 文件

apiVersion: v1
kind: PersistentVolume
metadata:name: prometheus-pv
spec:capacity:storage: 50GiaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: RetainstorageClassName: manualhostPath:path: /nfs/prometheus

创建 prometheus-pvc.yaml 文件

apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: prometheus-pvcnamespace: monitoring
spec:accessModes:- ReadWriteOnceresources:requests:storage: 50GistorageClassName: manual

执行安装 pvpvc

kubectl apply -f prometheus-pv.yaml
kubectl apply -f prometheus-pvc.yaml

验证安装

kubectl get pv -n monitoring
kubectl get pvc -n monitoring

配置 Prometheus 自定义配置

创建 prometheus-custom-values.yaml 文件

server:persistentVolume:enabled: truesize: 50GistorageClass: manualexistingClaim: prometheus-pvcsecurityContext:runAsUser: 65534  # Matches the 'nobody' user, commonly used by PrometheusrunAsGroup: 65534fsGroup: 65534    # Ensures the mounted volume is writable by this groupalertmanager:persistentVolume:enabled: truesize: 2GistorageClass: "-"accessModes:- ReadWriteOnceannotations: {}

helm 离线安装

helm install prometheus ./prometheus-27.13.0.tgz --namespace monitoring -f prometheus-custom-values.yaml

安装完成,输出日志

NAME: prometheus
LAST DEPLOYED: Tue May 13 20:37:07 2025
NAMESPACE: monitoring
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The Prometheus server can be accessed via port 80 on the following DNS name from within your cluster:
prometheus-server.monitoring.svc.cluster.localGet the Prometheus server URL by running these commands in the same shell:export POD_NAME=$(kubectl get pods --namespace monitoring -l "app.kubernetes.io/name=prometheus,app.kubernetes.io/instance=prometheus" -o jsonpath="{.items[0].metadata.name}")kubectl --namespace monitoring port-forward $POD_NAME 9090The Prometheus alertmanager can be accessed via port 9093 on the following DNS name from within your cluster:
prometheus-alertmanager.monitoring.svc.cluster.localGet the Alertmanager URL by running these commands in the same shell:export POD_NAME=$(kubectl get pods --namespace monitoring -l "app.kubernetes.io/name=alertmanager,app.kubernetes.io/instance=prometheus" -o jsonpath="{.items[0].metadata.name}")kubectl --namespace monitoring port-forward $POD_NAME 9093
#################################################################################
######   WARNING: Pod Security Policy has been disabled by default since    #####
######            it deprecated after k8s 1.25+. use                        #####
######            (index .Values "prometheus-node-exporter" "rbac"          #####
###### .          "pspEnabled") with (index .Values                         #####
######            "prometheus-node-exporter" "rbac" "pspAnnotations")       #####
######            in case you still need it.                                #####
#################################################################################The Prometheus PushGateway can be accessed via port 9091 on the following DNS name from within your cluster:
prometheus-prometheus-pushgateway.monitoring.svc.cluster.localGet the PushGateway URL by running these commands in the same shell:export POD_NAME=$(kubectl get pods --namespace monitoring -l "app=prometheus-pushgateway,component=pushgateway" -o jsonpath="{.items[0].metadata.name}")kubectl --namespace monitoring port-forward $POD_NAME 9091For more information on running Prometheus, visit:
https://prometheus.io/

卸载 Prometheus

helm uninstall prometheus --namespace monitoring

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词