k8s部署Prometheus+Grafana

要在 Kubernetes 集群中部署 Prometheus 和 Grafana,确保已经搭建了一个 Kubernetes 集群。
部署 Prometheus:

  • 创建一个名为 prometheus-configmap.yaml 的 ConfigMap 文件,其中包含 Prometheus 配置文件。示例文件内容如下:
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-server-conf
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
    scrape_configs:
      - job_name: 'kubernetes-apiservers'
        kubernetes_sd_configs:
          - role: endpoints
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      - job_name: 'kubernetes-nodes'
        kubernetes_sd_configs:
          - role: node
  • 使用 kubectl apply -f prometheus-configmap.yaml 命令创建 ConfigMap。
  • 创建一个名为 prometheus-deployment.yaml 的 Deployment 文件,用于部署 Prometheus 实例。示例文件内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus
      volumes:
        - name: prometheus-config-volume
          configMap:
            name: prometheus-server-conf
  • 使用 kubectl apply -f prometheus-deployment.yaml 命令创建 Deployment。

部署 Grafana:

  • 创建一个名为 grafana-deployment.yaml 的 Deployment 文件,用于部署 Grafana 实例。示例文件内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
        - name: grafana
          image: grafana/grafana
          ports:
            - containerPort: 3000
  • 使用 kubectl apply -f grafana-deployment.yaml 命令创建 Deployment。

暴露服务:

  • 分别创建 Prometheus 和 Grafana 的 Service,使其可以被外部访问。可以创建 NodePort 或者使用 Ingress,具体根据需要选择。
    示例命令:
kubectl expose deployment prometheus --type=NodePort --port=9090
kubectl expose deployment grafana --type=NodePort --port=3000

配置数据源和仪表板:

  • 登录 Grafana 界面,配置 Prometheus 数据源,并导入所需的仪表板。
  • 配置 Grafana 数据源:
    • 打开 Grafana 界面(默认端口为 3000)并登录。
    • 转到 “Configuration” > “Data Sources” > “Add data source”。
    • 选择 “Prometheus” 数据源类型,并配置 Prometheus 服务器的地址。
    • 单击 “Save & Test” 按钮,确保连接成功。
  • 导入 Grafana 仪表板:
    • 转到 Grafana 界面中的 “Dashboards” > “Manage” > “Import”。
    • 输入要导入的仪表板 ID 或选择要导入的仪表板 JSON 文件。
    • 根据需要调整仪表板设置,并单击 “Import” 按钮。

完成以上步骤后,您就成功地在 Kubernetes 集群中部署了 Prometheus 和 Grafana,并且可以开始监控和可视化您的应用程序和集群的指标数据。

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值