创建命名空间
apiVersion: v1kind: Namespacemetadata: name: monitor
准备prometheus配置文件,因此使用configmap的形式保存
apiVersion: v1kind: ConfigMapmetadata: name: prometheus-config namespace: monitordata: prometheus.yml: | global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090']
prometheus的资源文件(deployment)
出现Prometheus数据存储权限问题,因为Prometheus内部使用nobody启动进程,挂载数据目录后权限为root,因此使用initContainer进行目录权限修复:
apiVersion: apps/v1kind: Deploymentmetadata: name: prometheus namespace: monitor labels: app: prometheusspec: selector: matchLabels: app: prometheus template: metadata: labels: app: prometheus spec: serviceAccountName: prometheus nodeSelector: app: prometheus initContainers: - name: "change-permission-of-directory" image: busybox command: ["/bin/sh"] args: ["-c", "chown -R 65534:65534 /prometheus"] securityContext: privileged: true volumeMounts: - mountPath: "/etc/prometheus" name: config-volume - mountPath: "/prometheus" name: data containers: - image: prom/prometheus:v2.19.2 name: prometheus args: - "--config.file=/etc/prometheus/prometheus.yml" - "--storage.tsdb.path=/prometheus" # 指定tsdb数据路径 - "--web.enable-lifecycle" # 支持热更新,直接执行localhost:9090/-/reload立即生效 - "--web.console.libraries=/usr/share/prometheus/console_libraries" - "--web.console.templates=/usr/share/prometheus/consoles" ports: - containerPort: 9090 name: http volumeMounts: - mountPath: "/etc/prometheus" name: config-volume - mountPath: "/prometheus" name: data resources: requests: cpu: 100m memory: 512Mi limits: cpu: 100m memory: 512Mi volumes: - name: data hostPath: path: /data/prometheus/ - configMap: name: prometheus-config name: config-volume
创建rbac,prometheus会调用k8s api做服务发现进行抓取指标
apiVersion: v1kind: ServiceAccountmetadata: name: prometheus namespace: monitor---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: prometheusrules:- apiGroups: - "" resources: - nodes - services - endpoints - pods - nodes/proxy verbs: - get - list - watch- apiGroups: - "extensions" resources: - ingresses verbs: - get - list - watch- apiGroups: - "" resources: - configmaps - nodes/metrics verbs: - get- nonResourceURLs: - /metrics verbs: - get---apiVersion: rbac.authorization.k8s.io/v1beta1kind: ClusterRoleBindingmetadata: name: prometheusroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: prometheussubjects:- kind: ServiceAccount name: prometheus namespace: monitor
提供Service,为Ingress使用
apiVersion: v1kind: Servicemetadata: name: prometheus namespace: monitor labels: app: prometheusspec: selector: app: prometheus type: ClusterIP ports: - name: web port: 9090 targetPort: http$ cat prometheus-ingress.yamlapiVersion: extensions/v1beta1kind: Ingressmetadata: name: prometheus namespace: monitorspec: rules: - host: prometheus.luffy.com http: paths: - path: / backend: serviceName: prometheus servicePort: 9090
部署上述资源
# 命名空间$ kubectl create prometheus-namespace.yaml# 给node打上label$ kubectl label node k8s-slave1 app=prometheus#部署configmap$ kubectl create -f prometheus-configmap.yaml# rbac$ kubectl create -f prometheus-rbac.yaml# deployment$ kubectl create -f prometheus-deployment.yaml# service$ kubectl create -f prometheus-svc.yaml# ingress$ kubectl create -f prometheus-ingress.yaml# 访问测试$ kubectl -n monitor get ingress