Kubernetes 中多个节点上容器运行的监控实践

在 Kubernetes 集群中,多个节点上可能同时运行多个容器。为了保障集群的健康和稳定运行,需要对这些容器进行监控。本文将介绍在 Kubernetes 中实现多节点容器监控的过程。

步骤一:安装容器监控工具

我们可以使用 Prometheus + Grafana 的组合来监控 Kubernetes 中多个节点上的容器运行状况。

在 Kubernetes 集群中安装 Prometheus 和 Grafana 的步骤如下:

  1. 先在 Kubernetes 集群中创建一个 ConfigMap:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: prometheus-kube-state-metrics
      namespace: monitoring
      labels:
        app: prometheus
    data:
      prometheus.rules: |
        groups:
        - name: k8s
          rules:
          - record: k8s:configmaps:created_total:sum
            expr: sum(kube_configmap_created)
          - record: k8s:configmaps:deleted_total:sum
            expr: sum(kube_configmap_deleted)
          - record: k8s:configmaps:watch_count:sum
            expr: sum(changes(kube_configmap_status))
          - record: k8s:configmaps:failed_to_create_total:sum
            expr: sum(kube_configmap_failed_to_create)
  2. 然后创建一个 ServiceAccount:

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: prometheus
      namespace: monitoring
  3. 接着创建一个 RoleBinding:

    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: RoleBinding
    metadata:
      name: prometheus
      namespace: monitoring
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: kubernetes-dashboard
    subjects:
    - kind: ServiceAccount
      name: prometheus
      namespace: monitoring
  4. 然后,我们需要在 Kubernetes 集群中部署 Prometheus 和 Grafana:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prometheus
      namespace: monitoring
    spec:
      selector:
        matchLabels:
          app: prometheus
      replicas: 1
      template:
        metadata:
          labels:
            app: prometheus
        spec:
          containers:
          - name: prometheus
            image: prom/prometheus:latest
            command:
            - /bin/prometheus
            args:
            - --config.file=/prometheus/prometheus.yml
            ports:
            - containerPort: 9090
            volumeMounts:
            - name: prometheus-config
              mountPath: /prometheus
          volumes:
          - name: prometheus-config
            configMap:
              name: prometheus-kube-state-metrics
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: prometheus
        namespace: monitoring
      spec:
        ports:
        - name: web
          port: 9090
          targetPort: 9090
        selector:
          app: prometheus
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: grafana
      namespace: monitoring
    spec:
      selector:
        matchLabels:
          app: grafana
      replicas: 1
      template:
        metadata:
          labels:
            app: grafana
            # Add these labels for Grafana Operator v0.4.0 and later
            grafana.instance: grafana
            grafana.metric: jvm
        spec:
          serviceAccount: grafana
          containers:
          - name: grafana
            image: grafana/grafana:6.2.2
            env:
            - name: GF_SECURITY_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: grafana
                  key: admin-password
            ports:
            - containerPort: 3000
            volumeMounts:
            - name: grafana-provisioning
              mountPath: /etc/grafana/provisioning
              readOnly: true
            - name: grafana-dashboards
              mountPath: /var/lib/grafana/dashboards
          volumes:
          - name: grafana-provisioning
            configMap:
              name: grafana-dash
              items:
              - key: datasources.yaml
                path: datasources.yaml
              - key: dashboard.json
                path: dashboards/dashboard.json
          - name: grafana-dashboards
            configMap:
              name: grafana-dash
              items:
              - key: dashboard.json
                path: dashboard.json
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: grafana
        namespace: monitoring
      spec:
        ports:
        - name: web
          port: 80
          targetPort: 3000
        selector:
          app: grafana
  5. 最后,在 Kubernetes 集群中启动 Prometheus 和 Grafana:

    kubectl create -f prometheus.yaml
    kubectl create -f grafana.yaml

步骤二:编写 Prometheus 配置文件

在 Kubernetes 中,我们可以使用 Kubernetes API Server 来查询运行的 Pod 的状态和信息,并将这些信息提供给 Prometheus。为此,我们需要在 Prometheus 的配置文件中添加如下的配置:

- job_name: 'kubernetes-pods'
  metrics_path: '/apis/metrics.k8s.io/v1beta1/pods'
  scheme: https
  bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
  tls_config:
    ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
  kubernetes_sd_configs:
  - role: pod
    namespaces:
      names:
      - monitoring
  relabel_configs:
  - source_labels: [__meta_kubernetes_namespace]
    regex: monitoring
    action: keep
  - source_labels: [__meta_kubernetes_pod_label_app_kubernetes_io_name]
    regex: prometheus
    action: drop

步骤三:在 Grafana 中创建 Dashboard

我们还需要在 Grafana 中创建一个 Dashboard,用于展示 Kubernetes 中多个节点上的容器运行状态信息。

在 Grafana 中创建 Dashboard 的步骤如下:

  1. 在 Grafana 中点击 New Dashboard -> Graph,然后在左侧面板中选择 Edit:

  2. 然后在左侧面板中选择 Prometheus 数据源,在 Metrics 中输入以下表达式:

    sum(container_memory_working_set_bytes{namespace="$namespace"}) by (pod_name, namespace)

    其中,$namespace 是 Dashboard 变量,用于展示不同的资源利用率。

  3. 最后,将 Dashboard 保存并在 Grafana 中预览:

总结

以上就是在 Kubernetes 中实现多节点容器监控的全过程。通过使用 Prometheus + Grafana 的组合,我们可以实现对多个节点上的容器运行状态的实时监控。同时,我们还可以通过在 Grafana 中创建 Dashboard 来展示 Kubernetes 中多个节点上的容器运行信息,并据此优化资源利用率,提高 Kubernetes 集群的性能和稳定性。

示例代码:https://github.com/kubernetes/examples/tree/master/staging/simple-prometheus-grafana-stack

来源:JavaScript中文网 ,转载请注明来源 本文地址:https://www.javascriptcn.com/post/65ab3337add4f0e0ff4d17bc