在Kubernetes(k8s)上部署Prometheus的步骤如下:
(图片来源网络,侵删)
1、创建命名空间
我们需要创建一个命名空间来存放Prometheus相关的资源,可以使用以下命令创建一个名为monitoring的命名空间:
kubectl create namespace monitoring2、下载Prometheus配置文件
从GitHub上下载Prometheus的默认配置文件:
wget https://raw.githubusercontent.com/prometheus/prometheus/master/prometheus.yml3、创建ConfigMap
将下载的配置文件放入一个名为configmap.yml的文件中,并使用以下命令将其转换为ConfigMap:
kubectl create configmap prometheusconfig fromfile=configmap.yml n monitoring4、创建Prometheus服务账号和角色绑定
创建一个名为prometheus的服务账号,并将其与monitoring命名空间绑定:
kubectl create serviceaccount prometheus n monitoring kubectl create rolebinding prometheusrolebinding n monitoring clusterrole=edit serviceaccount=monitoring:prometheus5、创建Prometheus部署文件
创建一个名为prometheusdeployment.yml的文件,内容如下:
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.30.3 args: "config.file=/etc/prometheus/prometheus.yml" "storage.tsdb.path=/prometheus" ports: containerPort: 9090 volumeMounts: name: prometheusconfigvolume mountPath: /etc/prometheus/prometheus.yml subPath: prometheus.yml name: prometheusstoragevolume mountPath: /prometheus volumes: name: prometheusconfigvolume configMap: name: prometheusconfig name: prometheusstoragevolume emptyDir: {} apiVersion: v1 kind: ServiceAccount metadata: name: prometheus namespace: monitoring apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: prometheusclusterrolebinding roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: edit subjects: kind: ServiceAccount name: prometheus namespace: monitoring6、应用Prometheus部署文件
使用以下命令应用刚刚创建的部署文件:
kubectl apply f prometheusdeployment.yml n monitoring7、获取Prometheus服务的访问地址和Token信息
运行以下命令以获取Prometheus服务的访问地址和Token信息:
export POD_NAME=$(kubectl get pods n monitoring l app=prometheus o jsonpath="{.items[0].metadata.name}") # 获取Pod名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用POD_NAME环境变量替换pod名称部分,kubectl n monitoring describe secret $(kubectl n monitoring get secret | grep prometheus | awk {print $1}) # 获取Secret名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SECRET_NAME环境变量替换secret名称部分,kubectl n monitoring describe serviceaccount prometheus # 获取ServiceAccount名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SERVICEACCOUNT_NAME环境变量替换serviceaccount名称部分,kubectl n monitoring describe service prometheus # 获取Service名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SERVICE_NAME环境变量替换service名称部分,kubectl n monitoring describe endpoints prometheus # 获取Endpoints名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用ENDPOINTS_NAME环境变量替换endpoints名称部分,echo "http://${POD_NAME}.monitoring.svc.cluster.local:9090" # Prometheus访问地址,echo "${SECRET_NAME}" # Prometheus访问Token,echo "prom" # Prometheus访问用户名,echo "prom" # Prometheus访问密码,echo "${SERVICEACCOUNT_NAME}" # Prometheus访问用户组,echo "${SERVICE_NAME}" # Prometheus访问服务名,echo "${ENDPOINTS_NAME}" # Prometheus访问端点名,export POD_NAME=$(kubectl get pods n monitoring l app=prometheus o jsonpath="{.items[0].metadata.name}") # 获取Pod名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用POD_NAME环境变量替换pod名称部分,kubectl n monitoring describe secret $(kubectl n monitoring get secret | grep prometheus | awk {print $1}) # 获取Secret名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SECRET_NAME环境变量替换secret名称部分,kubectl n monitoring describe serviceaccount prometheus # 获取ServiceAccount名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SERVICEACCOUNT_NAME环境变量替换serviceaccount名称部分,kubectl n monitoring describe service prometheus # 获取Service名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SERVICE_NAME环境变量替换service名称部分,kubectl n monitoring describe endpoints prometheus # 获取Endpoints名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用ENDPOINTS_NAME环境变量替换endpoints名称部分,echo "http://${POD_NAME}.monitoring.svc.cluster.local:9090" # Prometheus访问地址,echo "${SECRET_NAME}" # Prometheus访问Token,echo "prom" # Prometheus访问用户名,echo "prom" # Prometheus访问密码,echo "${SERVICEACCOUNT_NAME}" # Prometheus访问用户组,echo "${SERVICE_NAME}" # Prometheus访问服务名,echo "${ENDPOINTS_NAME}" # Prometheus访问端点名,export POD_NAME=$(kubectl get pods n monitoring l app=prometheus o jsonpath="{.items[0].metadata.name}") # 获取Pod名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用POD_NAME环境变量替换pod名称部分,kubectl n monitoring describe secret $(kubectl n monitoring get secret | grep prometheus | awk {print $1}) # 获取Secret名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SECRET_NAME环境变量替换secret名称部分,kubectl n monitoring describe serviceaccount prometheus # 获取ServiceAccount名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SERVICEACCOUNT_NAME环境变量替换serviceaccount名称部分,kubectl n monitoring describe service prometheus # 获取Service名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用SERVICE_NAME环境变量替换service名称部分,kubectl n monitoring describe endpoints prometheus # 获取Endpoints名称,稍后用于创建ServiceMonitor和Alertmanager等资源时使用ENDPOINTS_NAME环境变量替换endpoints名称部分,echo "http://${POD_NAME}.monitoring.svc.cluster.local:9090" # Prometheus访问地址,echo "${SECRET_NAME}" # Prometheus访问Token,echo "prom" # Prometheus访问用户名,echo "prom" # Prometheus访问密码,echo "${SERVICEACCOUNT_NAME}" # Prometheus访问用户组,echo "${SERVICE_NAME}" # Prometheus访问服务名,echo "${ENDPOINTS_NAME}" # Prometheus访问端点名