k8s 配置安装社区版 Prometheus

发布于:2025-05-24 ⋅ 阅读:(15) ⋅ 点赞:(0)

第一种:在线安装

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

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

kubectl create namespace monitoring

helm pull prometheus-community/prometheus

tar -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: 50Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: manual
  hostPath:
    path: /nfs/prometheus

创建 prometheus-pvc.yaml 文件

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-pvc
  namespace: monitoring
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  storageClassName: 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: true
    size: 50Gi
    storageClass: manual
    existingClaim: prometheus-pvc
    
  securityContext:
    runAsUser: 65534  # Matches the 'nobody' user, commonly used by Prometheus
    runAsGroup: 65534
    fsGroup: 65534    # Ensures the mounted volume is writable by this group

alertmanager:
  persistentVolume:
    enabled: true
    size: 2Gi
    storageClass: "-"
    accessModes:
      - ReadWriteOnce
    annotations: {}

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.local


Get 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 9090


The Prometheus alertmanager can be accessed via port 9093 on the following DNS name from within your cluster:
prometheus-alertmanager.monitoring.svc.cluster.local


Get 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.local


Get 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 9091

For more information on running Prometheus, visit:
https://prometheus.io/

卸载 Prometheus

helm uninstall prometheus --namespace monitoring

网站公告

今日签到

点亮在社区的每一天
去签到