1、prometheus部署yml文件地址 github地址
2、下载yml文件
root@iZj6cd9joygowsf7am5hryZ:~# git clone https://github.com/redhatxl/k8s-prometheus-grafana.git
Cloning into 'k8s-prometheus-grafana'...
remote: Enumerating objects: 21, done.
remote: Total 21 (delta 0), reused 0 (delta 0), pack-reused 21
Unpacking objects: 100% (21/21), 5.18 KiB | 1.72 MiB/s, done.
root@iZj6cd9joygowsf7am5hryZ:~# cd k8s-prometheus-grafana/
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana# ls
grafana node-exporter.yaml prometheus README.md
3、安装node-exporter
node-exporter.yml文件内容
apiVersion: apps/v1
kind: DaemonSet
metadata:name: node-exporternamespace: kube-systemlabels:k8s-app: node-exporter
spec:selector:matchLabels:k8s-app: node-exportertemplate:metadata:labels:k8s-app: node-exporterspec:containers:- image: prom/node-exportername: node-exporterports:- containerPort: 9100protocol: TCPname: http
---
apiVersion: v1
kind: Service
metadata:labels:k8s-app: node-exportername: node-exporternamespace: kube-system
spec:ports:- name: httpport: 9100nodePort: 31672protocol: TCPtype: NodePortselector:k8s-app: node-exporter
创建node-exporter pod
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana# kubectl apply -f node-exporter.yaml
daemonset.apps/node-exporter created
service/node-exporter created
查看对应pod
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana# kubectl get pods -n kube-system |grep node-exporter
node-exporter-5vc5l 1/1 Running 0 118s
node-exporter-qdj9w 1/1 Running 0 118s
node-exporter-s69w7 1/1 Running 0 118s
node-exporter-xw98s 1/1 Running 0 118s
查看daemonset
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana# kubectl get daemonset -n kube-system |grep node-exporter
node-exporter 4 4 4 4 4 <none> 3m16s
查看对应service
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana# kubectl get service -n kube-system |grep node-exporter
node-exporter NodePort 10.68.113.47 <none> 9100:31672/TCP 4m4s
4、部署prometheus
a、对应yaml文件内容如下
rbac-setup.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: prometheus
rules:
- apiGroups: [""]resources:- nodes- nodes/proxy- services- endpoints- podsverbs: ["get", "list", "watch"]
- apiGroups:- extensionsresources:- ingressesverbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:name: prometheusnamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: prometheus
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: prometheus
subjects:
- kind: ServiceAccountname: prometheusnamespace: kube-system
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: prometheus-confignamespace: kube-system
data:prometheus.yml: |global:scrape_interval: 15sevaluation_interval: 15sscrape_configs:- job_name: 'kubernetes-apiservers'kubernetes_sd_configs:- role: endpointsscheme: httpstls_config:ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crtbearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/tokenrelabel_configs:- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]action: keepregex: default;kubernetes;https- job_name: 'kubernetes-nodes'kubernetes_sd_configs:- role: nodescheme: httpstls_config:ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crtbearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/tokenrelabel_configs:- action: labelmapregex: __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: nodescheme: httpstls_config:ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crtbearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/tokenrelabel_configs:- action: labelmapregex: __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: endpointsrelabel_configs:- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]action: keepregex: true- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]action: replacetarget_label: __scheme__regex: (https?)- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]action: replacetarget_label: __metrics_path__regex: (.+)- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]action: replacetarget_label: __address__regex: ([^:]+)(?::\d+)?;(\d+)replacement: $1:$2- action: labelmapregex: __meta_kubernetes_service_label_(.+)- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: kubernetes_namespace- source_labels: [__meta_kubernetes_service_name]action: replacetarget_label: kubernetes_name- job_name: 'kubernetes-services'kubernetes_sd_configs:- role: servicemetrics_path: /probeparams:module: [http_2xx]relabel_configs:- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_probe]action: keepregex: 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: labelmapregex: __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: ingressrelabel_configs:- source_labels: [__meta_kubernetes_ingress_annotation_prometheus_io_probe]action: keepregex: 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: labelmapregex: __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: podrelabel_configs:- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]action: keepregex: true- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]action: replacetarget_label: __metrics_path__regex: (.+)- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]action: replaceregex: ([^:]+)(?::\d+)?;(\d+)replacement: $1:$2target_label: __address__- action: labelmapregex: __meta_kubernetes_pod_label_(.+)- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: kubernetes_namespace- source_labels: [__meta_kubernetes_pod_name]action: replacetarget_label: kubernetes_pod_name
prometheus.deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:labels:name: prometheus-deploymentname: prometheusnamespace: kube-system
spec:replicas: 1selector:matchLabels:app: prometheustemplate:metadata:labels:app: prometheusspec:containers:- image: prom/prometheus:v2.0.0name: prometheuscommand:- "/bin/prometheus"args:- "--config.file=/etc/prometheus/prometheus.yml"- "--storage.tsdb.path=/prometheus"- "--storage.tsdb.retention=24h"ports:- containerPort: 9090protocol: TCPvolumeMounts:- mountPath: "/prometheus"name: data- mountPath: "/etc/prometheus"name: config-volumeresources:requests:cpu: 100mmemory: 100Milimits:cpu: 500mmemory: 2500MiserviceAccountName: prometheus volumes:- name: dataemptyDir: {}- name: config-volumeconfigMap:name: prometheus-config
prometheus.svc.yml
---
kind: Service
apiVersion: v1
metadata:labels:app: prometheusname: prometheusnamespace: kube-system
spec:type: NodePortports:- port: 9090targetPort: 9090nodePort: 30003selector:app: prometheus
b、部署如下
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana# cd prometheus/
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/prometheus# ls
configmap.yaml prometheus.deploy.yml prometheus.svc.yml rbac-setup.yaml
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/prometheus# kubectl apply -f rbac-setup.yaml
clusterrole.rbac.authorization.k8s.io/prometheus created
serviceaccount/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/prometheus# kubectl apply -f configmap.yaml
configmap/prometheus-config created
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/prometheus# kubectl apply -f prometheus.deploy.yml
deployment.apps/prometheus created
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/prometheus# kubectl apply -f prometheus.svc.yml
service/prometheus created
c、查看部署的各个项
pods
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl get pods -n kube-system |grep prometheus
prometheus-8597dfbfbb-2bjml 1/1 Running 0 14m
service
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl get service -n kube-system |grep prometheus
prometheus NodePort 10.68.213.220 <none> 9090:30003/TCP 14m
5、部署grafana
a、yaml文件如下
grafana-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: grafana-corenamespace: kube-systemlabels:app: grafanacomponent: core
spec:replicas: 1selector:matchLabels:app: grafanacomponent: coretemplate:metadata:labels:app: grafanacomponent: corespec:containers:- image: grafana/grafana:4.2.0name: grafana-coreimagePullPolicy: IfNotPresent# env:resources:# keep request = limit to keep this container in guaranteed classlimits:cpu: 100mmemory: 100Mirequests:cpu: 100mmemory: 100Mienv:# The following env variables set up basic auth twith the default admin user and admin password.- name: GF_AUTH_BASIC_ENABLEDvalue: "true"- name: GF_AUTH_ANONYMOUS_ENABLEDvalue: "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: /loginport: 3000# initialDelaySeconds: 30# timeoutSeconds: 1volumeMounts:- name: grafana-persistent-storagemountPath: /varvolumes:- name: grafana-persistent-storageemptyDir: {}
grafana-svc.yaml
apiVersion: v1
kind: Service
metadata:name: grafananamespace: kube-systemlabels:app: grafanacomponent: core
spec:type: NodePortports:- port: 3000selector:app: grafanacomponent: core
grafana-ing.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: grafananamespace: kube-system
spec:rules:- host: k8s.grafanahttp:paths:- path: /pathType: Prefix # This line was added to specify the path typebackend:service:name: grafanaport:number: 3000
b、具体部署如下
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl apply -f grafana-deploy.yaml
deployment.apps/grafana-core created
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl apply -f grafana-svc.yaml
service/grafana created
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl apply -f grafana-ing.yaml
ingress.networking.k8s.io/grafana created
c、查看部署的各个项
pods
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl get pods -n kube-system |grep grafana
grafana-core-676ccc87df-8qfjn 1/1 Running 0 8m
service
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl get service -n kube-system |grep grafana
grafana NodePort 10.68.227.18 <none> 3000:4688/TCP 7m11s
ingress
root@iZj6cd9joygowsf7am5hryZ:~/k8s-prometheus-grafana/grafana# kubectl get ingress -n kube-system |grep grafana
grafana <none> k8s.grafana 80 4m4s
6、访问node-exporter采集的数据,访问地址是http://localhost:31672/metrics(localhost根据自己的IP)
7、访问prometheus的地址,访问地址是:http://localhost:30003/graph (localhost根据自己的IP)
选择第一选择栏的status
8、访问grafana的地址,访问地址是:http://localhost:4688//login (localhost根据自己的IP),默认账号密码都是admin,登录后会要求更改账号密码
添加grafana源数据,选择prometheus
9、把k8s的Dashboard模块导入
导入315模块,或者下载好的json文件
具体模型图如下