欧洛米修斯监控 k8s

分享 123456789987654321 ⋅ 于 2022-08-28 15:25:53 ⋅ 179 阅读

promethus+Grafna监控

1.创建守护进程

[root@master1 ~]# mkdir moniter
[root@master1 ~]# cd moniter

kubectl create -f node-exportor.yaml
[root@master1 moniter]# kubectl apply -f node-exporter.yaml 
daemonset.apps/node-exporter created
service/node-exporter created
[root@master1 moniter]# kubectl delete -f node-exporter.yaml 如果之前有需要删除
# node-exporter.yaml 

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: kube-system
  labels:
    k8s-app: node-exporter
spec:
  selector:
    matchLabels:
      k8s-app: node-exporter
  template:
    metadata:
      labels:
        k8s-app: node-exporter
    spec:
      containers:
      - image: prom/node-exporter
        name: node-exporter
        ports:
        - containerPort: 9100
          protocol: TCP
          name: http
---
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: node-exporter
  name: node-exporter
  namespace: kube-system
spec:
  ports:
  - name: http
    port: 9100
    nodePort: 31672
    protocol: TCP
  type: NodePort
  selector:
    k8s-app: node-exporter

2.RBAC

[root@master1 moniter]# kubectl apply -f rbac-setup.yaml 
clusterrole.rbac.authorization.k8s.io/prometheus created
serviceaccount/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups: [""]
  resources:
  - nodes
  - nodes/proxy
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups:
  - extensions
  resources:
  - ingresses
  verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: kube-system

3.configmap

[root@master1 moniter]# kubectl apply -f configmap.yaml 
configmap/prometheus-config created
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: kube-system
data:
  prometheus.yml: |
    global:
      scrape_interval:     15s
      evaluation_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
      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:
      - role: node
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      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

    - job_name: 'kubernetes-cadvisor'
      kubernetes_sd_configs:
      - role: node
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      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

    - job_name: 'kubernetes-service-endpoints'
      kubernetes_sd_configs:
      - role: endpoints
      relabel_configs:
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
        action: replace
        target_label: __scheme__
        regex: (https?)
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
        action: replace
        target_label: __address__
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
      - action: labelmap
        regex: __meta_kubernetes_service_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        action: replace
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_service_name]
        action: replace
        target_label: kubernetes_name

    - job_name: 'kubernetes-services'
      kubernetes_sd_configs:
      - role: service
      metrics_path: /probe
      params:
        module: [http_2xx]
      relabel_configs:
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_probe]
        action: keep
        regex: true
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: blackbox-exporter.example.com:9115
      - source_labels: [__param_target]
        target_label: instance
      - action: labelmap
        regex: __meta_kubernetes_service_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_service_name]
        target_label: kubernetes_name

    - job_name: 'kubernetes-ingresses'
      kubernetes_sd_configs:
      - role: ingress
      relabel_configs:
      - source_labels: [__meta_kubernetes_ingress_annotation_prometheus_io_probe]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_ingress_scheme,__address__,__meta_kubernetes_ingress_path]
        regex: (.+);(.+);(.+)
        replacement: ${1}://${2}${3}
        target_label: __param_target
      - target_label: __address__
        replacement: blackbox-exporter.example.com:9115
      - source_labels: [__param_target]
        target_label: instance
      - action: labelmap
        regex: __meta_kubernetes_ingress_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_ingress_name]
        target_label: kubernetes_name

    - job_name: 'kubernetes-pods'
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
        action: replace
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
        target_label: __address__
      - action: labelmap
        regex: __meta_kubernetes_pod_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        action: replace
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_pod_name]
        action: replace
        target_label: kubernetes_pod_name

4.prometheus.deploy.yml

[root@master1 moniter]# kubectl apply -f prometheus.deploy.yml 
deployment.apps/prometheus created
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: prometheus-deployment
  name: prometheus
  namespace: kube-system
spec:
  selector:
    matchLabels:
      k8s-app: node-exporter
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - image: prom/prometheus:v2.0.0
        name: prometheus
        command:
        - "/bin/prometheus"
        args:
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus"
        - "--storage.tsdb.retention=24h"
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - mountPath: "/prometheus"
          name: data
        - mountPath: "/etc/prometheus"
          name: config-volume
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 500m
            memory: 2500Mi
      serviceAccountName: prometheus    
      volumes:
      - name: data
        emptyDir: {}
      - name: config-volume
        configMap:
          name: prometheus-config   

5.svc

[root@master1 moniter]# kubectl apply -f prometheus.svc.yml 
service/prometheus created

6.查看

[root@master1 moniter]# kubectl get pods -n kube-system
NAME                              READY   STATUS              RESTARTS   AGE
coredns-58cc8c89f4-mfkvs          1/1     Running             0          53m
coredns-58cc8c89f4-xvvkd          1/1     Running             0          53m
etcd-master1                      1/1     Running             0          52m
etcd-master2                      1/1     Running             0          42m
kube-apiserver-master1            1/1     Running             0          52m
kube-apiserver-master2            1/1     Running             0          42m
kube-controller-manager-master1   1/1     Running             2          52m
kube-controller-manager-master2   1/1     Running             0          42m
kube-proxy-jjrql                  1/1     Running             0          52m
kube-proxy-kfdrg                  1/1     Running             0          53m
kube-proxy-ppjls                  1/1     Running             0          42m
kube-scheduler-master1            1/1     Running             1          52m
kube-scheduler-master2            1/1     Running             0          42m
node-exporter-fbjwn               0/1     ImagePullBackOff    0          9m52s
prometheus-759d85775b-chwrb       0/1     ContainerCreating   0          2m11s

7.grafna部署

7.1grafana-deploy.yaml

[root@master1 moniter]# kubectl apply -f grafana-deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana-core
  namespace: kube-system
  labels:
    app: grafana
    component: core
spec:
  selector:
    matchLabels:
      app: prometheus
  replicas: 1
  template:
    metadata:
      labels:
        app: grafana
        component: core
    spec:
      containers:
      - image: grafana/grafana:4.2.0
        name: grafana-core
        imagePullPolicy: IfNotPresent
        # env:
        resources:
          # keep request = limit to keep this container in guaranteed class
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi
        env:
          # The following env variables set up basic auth twith the default admin user and admin password.
          - name: GF_AUTH_BASIC_ENABLED
            value: "true"
          - name: GF_AUTH_ANONYMOUS_ENABLED
            value: "false"
          # - name: GF_AUTH_ANONYMOUS_ORG_ROLE
          #   value: Admin
          # does not really work, because of template variables in exported dashboards:
          # - name: GF_DASHBOARDS_JSON_ENABLED
          #   value: "true"
        readinessProbe:
          httpGet:
            path: /login
            port: 3000
          # initialDelaySeconds: 30
          # timeoutSeconds: 1
        volumeMounts:
        - name: grafana-persistent-storage
          mountPath: /var
      volumes:
      - name: grafana-persistent-storage
        emptyDir: {}

7.2grafana-svc.yaml

[root@master1 moniter]# kubectl create -f grafana-svc.yaml 
service/grafana created
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: kube-system
  labels:
    app: grafana
    component: core
spec:
  type: NodePort
  ports:
    - port: 3000
  selector:
    app: grafana
    component: core

7.3ing

[root@master1 moniter]# kubectl apply -f grafana-ing.yaml 
ingress.extensions/grafana created
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: kube-system
spec:
  type: NodePort
  ports:
  - port: 9090
    targetPort: 9090
    nodePort: 30003
  selector:
    app: prometheus

7.4部署

[root@master1 moniter]# kubectl get svc -n kube-system  
#访问30559 端口
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
grafana         NodePort    10.1.135.36    <none>        3000:30559/TCP           24m
kube-dns        ClusterIP   10.1.0.10      <none>        53/UDP,53/TCP,9153/TCP   82m
node-exporter   NodePort    10.1.5.180     <none>        9100:31672/TCP           38m
prometheus      NodePort    10.1.154.100   <none>        9090:30003/TCP           30m

默认账号密码admin

[root@master1 moniter]# kubectl get pod -n kube-system -owide
NAME                              READY   STATUS    RESTARTS   AGE    IP               NODE            NOMINATED NODE   READINESS GATES
coredns-58cc8c89f4-mfkvs          1/1     Running   2          175m   172.17.0.2       master1         <none>           <none>
coredns-58cc8c89f4-xvvkd          1/1     Running   1          175m   172.17.0.3       master1         <none>           <none>
etcd-master1                      1/1     Running   1          174m   192.168.92.155   master1         <none>           <none>
etcd-master2                      1/1     Running   2          164m   192.168.92.156   master2         <none>           <none>
grafana-core-7789756d87-7czj8     1/1     Running   0          35m    172.17.0.4       node01.k8s.io   <none>           <none>
kube-apiserver-master1            1/1     Running   2          174m   192.168.92.155   master1         <none>           <none>
kube-apiserver-master2            1/1     Running   1          164m   192.168.92.156   master2         <none>           <none>
kube-controller-manager-master1   1/1     Running   4          174m   192.168.92.155   master1         <none>           <none>
kube-controller-manager-master2   1/1     Running   2          164m   192.168.92.156   master2         <none>           <none>
kube-proxy-jjrql                  1/1     Running   1          173m   192.168.92.157   node01.k8s.io   <none>           <none>
kube-proxy-kfdrg                  1/1     Running   2          175m   192.168.92.155   master1         <none>           <none>
kube-proxy-ppjls                  1/1     Running   2          164m   192.168.92.156   master2         <none>           <none>
kube-scheduler-master1            1/1     Running   2          174m   192.168.92.155   master1         <none>           <none>
kube-scheduler-master2            1/1     Running   2          164m   192.168.92.156   master2         <none>           <none>
node-exporter-fbjwn               1/1     Running   0          131m   172.17.0.3       node01.k8s.io   <none>           <none>
prometheus-759d85775b-rwtvh       1/1     Running   0          37m    172.17.0.2       node01.k8s.io   <none>           <none>

设置模板

ntpdate ntp1.aliyun.com

版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海牛部落-123456789987654321,http://hainiubl.com/topics/75901
点赞
成为第一个点赞的人吧 :bowtie:
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter