kubernetes 部署prometheus笔记 (四)

本文记录了在Kubernetes上部署Grafana的两种方法:以Pod方式和传统方式。重点介绍了以Pod方式部署时安装Kubernetes插件以展示监控信息Dashboard的步骤,但遇到无法成功连接集群的问题。另外,提到了直接在物理服务器上部署Grafana,需要kubectl和访问集群的配置。
摘要由CSDN通过智能技术生成

1. 部署grafana,有两种方式部署,一种是以pod方式部署,准备yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: monitoring-grafana
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: grafana
  template:
    metadata:
      labels:
        k8s-app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana
       # command: ["grafana-cli plugins install grafana-kubernetes-app & service grafana-server restart"]
       # ports:
       # - containerPort: 3000
       #   protocol: TCP
        volumeMounts:
        - mountPath: /etc/ssl/certs
          name: ca-certificates
          readOnly: true
        - mountPath: /var
          name: grafana-storage
        env:
        #- name: INFLUXDB_HOST
        #  value: monitoring-influxdb
        #- name: GF_SERVER_HTTP_PORT
        #  value: "3000"
          # The following env variables are required to make Grafana accessible via
  
A:在 Kubernetes部署 Prometheus,可以按照以下步骤进行操作: 1. 创建一个 Kubernetes Namespace,可以使用以下命令: ``` kubectl create ns monitoring ``` 2. 创建一个 Prometheus 配置文件 prometheus.yml,可以使用以下命令: ``` vi prometheus.yml ``` 将以下内容复制到文件中: ``` global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'kubernetes-apiservers' kubernetes_sd_configs: - api_server: null role: endpoints namespaces: names: ['default'] relabel_configs: - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name] action: keep regex: default;kubernetes;https - job_name: 'kubernetes-nodes' kubernetes_sd_configs: - api_server: null role: node relabel_configs: - action: labelmap regex: __meta_kubernetes_node_label_(.+) - target_label: __address__ replacement: kubernetes.default.svc:443 - source_labels: [__meta_kubernetes_node_name] regex: (.+) target_label: __metrics_path__ replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor ``` 这个配置文件将监控使用 `https` 端口的 Kubernetes API Server、Kubernetes Node 的 CPU、内存和磁盘利用率等信息,以及已经部署Prometheus 本身。 3. 创建一个 ConfigMap,将以上配置文件导入 Kubernetes 集群,可以使用以下命令: ``` kubectl create configmap prometheus-cm --from-file=prometheus.yml -n monitoring ``` 4. 创建一个 Prometheus Deployment 并暴露一个 Service,可以使用以下 YAML 文件: ``` apiVersion: apps/v1 kind: Deployment metadata: name: prometheus namespace: monitoring spec: replicas: 1 selector: matchLabels: app: prometheus template: metadata: labels: app: prometheus spec: containers: - name: prometheus image: prom/prometheus:v2.22.2 args: - --config.file=/etc/prometheus/prometheus.yml - --storage.tsdb.path=/prometheus/data - --storage.tsdb.retention.time=7d volumeMounts: - name: prometheus-data mountPath: /prometheus - name: prometheus-config mountPath: /etc/prometheus volumes: - name: prometheus-data emptyDir: {} - name: prometheus-config configMap: name: prometheus-cm items: - key: prometheus.yml path: prometheus.yml --- apiVersion: v1 kind: Service metadata: name: prometheus namespace: monitoring spec: selector: app: prometheus ports: - name: prometheus-web port: 9090 protocol: TCP targetPort: 9090 ``` 以上 YAML 文件中,我们创建了一个名称为 prometheus 的 Deployment,副本数为 1,使用了 prom/prometheus:v2.22.2 镜像。然后创建了一个名称为 prometheus 的 Service,并将 targetPort 设置为 9090。 5. 使用以下命令,将以上 YAML 文件部署到 K8s 集群中: ``` kubectl apply -f prometheus.yaml ``` 等待几分钟后,可以执行以下命令验证 Prometheus 是否正常运行: ``` kubectl get pods -n monitoring ``` 以上命令输出的结果应该类似于以下内容: ``` NAME READY STATUS RESTARTS AGE prometheus-7b447fcfbc-wq2xn 1/1 Running 0 8h ``` 此外,还可以使用以下命令获取 Prometheus 的 Service 的 ClusterIP: ``` kubectl get svc prometheus -n monitoring ``` 以上命令输出的结果应该类似于以下内容: ``` NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE prometheus ClusterIP 10.100.82.214 <none> 9090/TCP 8h ``` 通过以上输出的 ClusterIP 和 9090/TCP,就可以在 K8s 集群内部访问 Prometheus 了,比如可以使用以下命令: ``` curl http://10.100.82.214:9090/ ``` 以上命令输出的内容应该是 HTML 格式的 Prometheus 首页。 至此,在 Kubernets 中部署 Prometheus 完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值