在Amazon EKS集群上安装Kubernetes Metrics Server
在我们之前的教程中,我们讨论了如何使用Amazon EKS服务在AWS上安装Kubernetes Cluster。实际上,这是几分钟之内在AWS中运行Kubernetes集群的最快,最简单的方法。 eksctl的设置过程是相当自动化的,它使用幕后使用CloudFormation堆栈来引导由Amazon Linux工作机驱动的工作集群。
在本教程中,我将逐步引导我们完成在EKS集群中安装和配置Kubernetes指标服务器的步骤。 Metrics Server是Kubernetes内置自动伸缩管道的可扩展,高效的容器资源指标来源。它从Kubelet收集资源指标,并通过Metrics API在Kubernetes apiserver中公开它们,以供Horizontal Pod AutoscalerandVertical Pod Autoscaler使用。
Metrics Server提供:
适用于大多数群集的单个部署可扩展支持多达5,000个节点群集资源效率:Metrics Server使用0.5m的CPU核心和每个节点4 MB的内存
在Amazon EKS集群上安装Kubernetes Metrics Server
在Amazon EKS Cluster上开始安装Kubernetes Metrics Server之前,请确认我们有EKS集群正在工作。我们可以使用eksctl命令检查可用的EKS群集。
$eksctl get cluster NAME REGION prod-eks-cluster eu-west-1
如果本地有kubeconfig,请使用它来确认Kubernetes API服务器是否响应。
$kubectl --kubeconfig=/Users/jkmutai/.kube/eksctl/clusters/prod-eks-cluster get nodes NAME STATUS ROLES AGE VERSION ip-192-168-138-244.eu-west-1.compute.internal Ready <none> 13h v1.17.9-eks-4c6976 ip-192-168-176-247.eu-west-1.compute.internal Ready <none> 13h v1.17.9-eks-4c6976
指标服务器要求
Metrics Server对群集和网络配置有特定要求。这些要求是所有群集分发的默认设置。在使用Metrics Server之前,请确保集群分发支持这些要求:必须从kube-apiserver可以访问Metrics Server必须正确配置kube-apiserver以启用聚合层节点必须配置kubelet授权以匹配Metrics Server配置容器运行时必须实现容器度量RPC
如何在Amazon EKS集群上安装Kubernetes Metrics Server
将kubeconfig保存到环境变量。
export KUBECONFIG=/Users/jkmutai/.kube/eksctl/clusters/prod-eks-cluster
确认我们可以运行kubectl命令,而无需手动将路径传递到kubeconfig文件。
$kubectl get nodes NAME STATUS ROLES AGE VERSION ip-192-168-138-244.eu-west-1.compute.internal Ready <none> 13h v1.17.9-eks-4c6976 ip-192-168-176-247.eu-west-1.compute.internal Ready <none> 13h v1.17.9-eks-4c6976
应用Metrics Server版本中可用的Metrics Server列表,使其可通过url安装:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.4.1/components.yaml
这是所创建资源的输出。
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created serviceaccount/metrics-server created deployment.apps/metrics-server created service/metrics-server created clusterrole.rbac.authorization.k8s.io/system:metrics-server created clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
使用以下命令来验证metrics-server部署是否正在运行所需数量的容器:
$kubectl get deployment metrics-server -n kube-system NAME READY UP-TO-DATE AVAILABLE AGE metrics-server 1/1 1 1 7m23s $kubectl get pods -n kube-system | grep metrics metrics-server-7cb45bbfd5-kbrt7 1/1 Running 0 8m42s
确认指标服务器处于活动状态。
$kubectl get apiservice v1beta1.metrics.k8s.io -o yaml apiVersion: apiregistration.k8s.io/v1 kind: APIService metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"apiregistration.k8s.io/v1beta1","kind":"APIService","metadata":{"annotations":{},"name":"v1beta1.metrics.k8s.io"},"spec":{"group":"metrics.k8s.io","groupPriorityMinimum":100,"insecureSkipTLSVerify":true,"service":{"name":"metrics-server","namespace":"kube-system"},"version":"v1beta1","versionPriority":100}} creationTimestamp: "2017-08-12T11:27:13Z" name: v1beta1.metrics.k8s.io resourceVersion: "130943" selfLink: /apis/apiregistration.k8s.io/v1/apiservices/v1beta1.metrics.k8s.io uid: 83c44e41-6346-4dff-8ce2-aff665199209 spec: group: metrics.k8s.io groupPriorityMinimum: 100 insecureSkipTLSVerify: true service: name: metrics-server namespace: kube-system port: 443 version: v1beta1 versionPriority: 100 status: conditions: - lastTransitionTime: "2017-08-12T11:27:18Z" message: all checks passed reason: Passed status: "True" type: Available
还可以使用kubectl top命令访问Metrics API。这样可以更轻松地调试自动缩放管道。
$kubectl top --help Display Resource (CPU/Memory/Storage) usage. The top command allows you to see the resource consumption for nodes or pods. This command requires Metrics Server to be correctly configured and working on the server. Available Commands: node Display Resource (CPU/Memory/Storage) usage of nodes pod Display Resource (CPU/Memory/Storage) usage of pods Usage: kubectl top [flags] [options] Use "kubectl <command> --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).
要显示群集节点的资源使用情况CPU /内存/存储,请运行以下命令:
$kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% ip-192-168-138-244.eu-west-1.compute.internal 50m 2% 445Mi 13% ip-192-168-176-247.eu-west-1.compute.internal 58m 3% 451Mi 13%
类似的命令可以用于Pod。
$kubectl top pods -A NAMESPACE NAME CPU(cores) MEMORY(bytes) kube-system aws-node-glfrs 4m 51Mi kube-system aws-node-sgh8p 5m 51Mi kube-system coredns-6987776bbd-2mgxp 2m 6Mi kube-system coredns-6987776bbd-vdn8j 2m 6Mi kube-system kube-proxy-5glzs 1m 7Mi kube-system kube-proxy-hgqm5 1m 8Mi kube-system metrics-server-7cb45bbfd5-kbrt7 1m 11Mi
我们还可以访问use kubectl get raw以获取群集中所有节点的原始资源使用情况指标。
$kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq { "kind": "NodeMetricsList", "apiVersion": "metrics.k8s.io/v1beta1", "metadata": { "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes" }, "items": [ { "metadata": { "name": "ip-192-168-176-247.eu-west-1.compute.internal", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/ip-192-168-176-247.eu-west-1.compute.internal", "creationTimestamp": "2017-08-12T11:44:41Z" }, "timestamp": "2017-08-12T11:44:17Z", "window": "30s", "usage": { "cpu": "55646953n", "memory": "461980Ki" } }, { "metadata": { "name": "ip-192-168-138-244.eu-west-1.compute.internal", "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/ip-192-168-138-244.eu-west-1.compute.internal", "creationTimestamp": "2017-08-12T11:44:41Z" }, "timestamp": "2017-08-12T11:44:09Z", "window": "30s", "usage": { "cpu": "47815890n", "memory": "454944Ki" } } ] }