在EKS Kubernetes集群上部署Prometheus
Prometheus是一个开源监控系统,具有维度数据模型,灵活的查询语言,高效的时间序列数据库和现代警报方法。 Prometheus可以作为独立服务安装在Linux机器上,也可以部署在Kubernetes集群中。在本教程中,我们将引导我们完成在AWS Cloud中部署的EKS集群上Prometheus的安装。
Prometheus的主要特点是:多维数据模型,其中时间序列数据由度量标准名称和键/值对标识。单个服务器节点是自主时间序列集合,通过HTTP上的拉模型进行收集通过中间网关支持推送时间序列通过服务发现或者静态配置发现目标通过图形和仪表板支持的多种模式
在EKS Kubernetes集群上部署Prometheus
在使用安装了kubectl并能够与API服务器进行交互的本教程之前,我们应该具有运行中的EKS群集。
列出可用的群集。
$eksctl get cluster NAME REGION prod-eks-cluster eu-west-1
第1步:配置kubectl
要随时从通过eksctl部署的EKS群集中获取群集凭据,请运行:
$eksctl utils write-kubeconfig --cluster=<name> [--kubeconfig=<path>][--set-kubeconfig-context=<bool>]
See below example:
# Create default kubeconfig directory - Can be custom --- Linux -- $mkdir /home/$USER/.kube --- macOS -- $mkdir /Users/$USER/.kube # Obtain cluster credentials --- Linux -- $s write-kubeconfig --cluster=prod-eks-cluster --kubeconfig=/home/$USER/.kube/config --- macOS -- $eksctl utils write-kubeconfig --cluster=prod-eks-cluster --kubeconfig=/Users/$USER/.kube/config
预期的命令执行输出:
[ℹ] eksctl version 0.25.0 [ℹ] using region eu-west-1 [✔] saved kubeconfig as "/Users/jkmutai/.kube/config"
确认kubectl命令是否按预期工作。
$kubectl get nodes NAME STATUS ROLES AGE VERSION ip-192-168-138-244.eu-west-1.compute.internal Ready <none> 7d21h v1.17.9-eks-4c6976 ip-192-168-176-247.eu-west-1.compute.internal Ready <none> 7d21h v1.17.9-eks-4c6976
安装Kubernetes Metrics Server
Kubernetes API服务器公开了许多对监视和分析有用的指标。这些度量是通过引用/metrics HTTP API的度量端点在内部公开的。
按照以下教程安装Metrics Server:
在Amazon EKS集群上安装Kubernetes Metrics Server
我们可以通过运行以下命令来查看群集原始指标:
$kubectl get --raw /metrics
安装头盔
Kubernetes的Helm软件包管理器可在Kubernetes集群上安装和管理应用程序。很高兴使用帮助将Prometheus部署到EKS上。
这些是我们将用来安装头盔的命令。
--- Linux -- curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 > get_helm.sh chmod 700 get_helm.sh ./get_helm.sh --- macOS -- $brew install helm --- Windows -- $choco install kubernetes-helm
我们可以使用以下命令查询已安装的版本:
$helm version version.BuildInfo{Version:"v3.2.4", GitCommit:"0ad800ef43d3b826f31a5ad8dfbb4fe05d143688", GitTreeState:"dirty", GoVersion:"go1.14.3"}
在EKS Kubernetes集群上部署Prometheus
可以使用Operator或者头盔将Prometheus安装在Kubernetes集群上。使用我们刚刚安装的Helm V3,可以在EKS集群中部署Prometheus。
首先创建一个监视名称空间。
$kubectl create namespace monitoring namespace/monitoring created
Prometheus需要一种保留指标数据以供历史参考的方法。最好使用gp2存储类已配备的EBS。
$kubectl get sc kubectl get sc NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE efs-sc efs.csi.aws.com Delete Immediate false 2d23h gp2 (default) kubernetes.io/aws-ebs Delete WaitForFirstConsumer false 7d21h
添加图表存储库:
$helm repo add stable https://kubernetes-charts.storage.googleapis.com "stable" has been added to your repositories
使用Helm部署Prometheus。
helm install prometheus stable/prometheus \ --namespace monitoring \ --set alertmanager.persistentVolume.storageClass="gp2",server.persistentVolume.storageClass="gp2"
命令执行输出:
NAME: prometheus LAST DEPLOYED: Wed Aug 19 22:01:16 2017 NAMESPACE: monitoring STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: The Prometheus server can be accessed via port 80 on the following DNS name from within your cluster: prometheus-server.monitoring.svc.cluster.local
确认已创建PV和PVC。
$kubectl get pv -n monitoring NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-7a1d891c-5b3f-4d28-91b5-5f1d1590daf9 8Gi RWO Delete Bound monitoring/prometheus-server gp2 11m pvc-7d45951f-a8c5-4fb1-b149-747dccaceecc 2Gi RWO Delete Bound monitoring/prometheus-alertmanager gp2 11m $kubectl get pvc -n monitoring NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE prometheus-alertmanager Bound pvc-7d45951f-a8c5-4fb1-b149-747dccaceecc 2Gi RWO gp2 11m prometheus-server Bound pvc-7a1d891c-5b3f-4d28-91b5-5f1d1590daf9 8Gi RWO gp2 11m
在EKS Kubernetes集群上访问Prometheus
安装后,查询监视名称空间中的所有资源:
$kubectl get all -n monitoring NAME READY STATUS RESTARTS AGE pod/prometheus-alertmanager-d47577c4b-kz9td 2/2 Running 0 98s pod/prometheus-kube-state-metrics-6df5d44568-fzfwn 1/1 Running 0 98s pod/prometheus-node-exporter-p6qk7 1/1 Running 0 99s pod/prometheus-node-exporter-stsjk 1/1 Running 0 99s pod/prometheus-pushgateway-57c97d878d-hmnwr 1/1 Running 0 98s pod/prometheus-server-559c49b4ff-42k46 2/2 Running 0 98s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/prometheus-alertmanager ClusterIP 10.100.136.125 <none> 80/TCP 99s service/prometheus-kube-state-metrics ClusterIP 10.100.169.64 <none> 8080/TCP 99s service/prometheus-node-exporter ClusterIP None <none> 9100/TCP 99s service/prometheus-pushgateway ClusterIP 10.100.71.173 <none> 9091/TCP 99s service/prometheus-server ClusterIP 10.100.138.66 <none> 80/TCP 99s NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/prometheus-node-exporter 2 2 2 2 2 <none> 99s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/prometheus-alertmanager 1/1 1 1 99s deployment.apps/prometheus-kube-state-metrics 1/1 1 1 99s deployment.apps/prometheus-pushgateway 1/1 1 1 99s deployment.apps/prometheus-server 1/1 1 1 99s NAME DESIRED CURRENT READY AGE replicaset.apps/prometheus-alertmanager-d47577c4b 1 1 1 99s replicaset.apps/prometheus-kube-state-metrics-6df5d44568 1 1 1 99s replicaset.apps/prometheus-pushgateway-57c97d878d 1 1 1 99s replicaset.apps/prometheus-server-559c49b4ff 1 1 1 99s
通过在同一shell中运行以下命令来获取Prometheus服务器URL:
export POD_NAME=$(kubectl get pods --namespace monitoring -l "app=prometheus,component=server" -o jsonpath="{.items[0].metadata.name}")
使用Kubernetes端口转发功能访问Prometheus Server。
$kubectl --namespace monitoring port-forward $POD_NAME 9090 Forwarding from 127.0.0.1:9090 -> 9090 Forwarding from [::1]:9090 -> 9090
从本地计算机浏览器访问127.0.0.1:9090。
类似的命令用于访问Alertmanager UI
export POD_NAME=$(kubectl get pods --namespace monitoring -l "app=prometheus,component=alertmanager" -o jsonpath="{.items[0].metadata.name}") kubectl --namespace monitoring port-forward $POD_NAME 9093
如果要使用节点端口访问Prometheus Server和Alertmanager,请编辑服务并设置NodePort。