我能够使用以下方法实现 HPAmetrics-server
因为 heapster 已折旧。我已按照以下步骤操作:
- 克隆指标服务器 github 存储库:
git clone https://github.com/kubernetes-incubator/metrics-server.git
进入目录cd deploy/1.8+
并运行以下 yaml 文件:
[root@ip-10-0-1-91 1.8+]# kubectl apply -f aggregated-metrics-reader.yaml
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f auth-reader.yaml
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f auth-delegator.yaml
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f metrics-apiservice.yaml
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f resource-reader.yaml
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f metrics-server-deployment.yaml
serviceaccount/metrics-server created
deployment.extensions/metrics-server created
[root@ip-10-0-1-91 1.8+]# kubectl apply -f metrics-server-service.yaml
service/metrics-server created
现在创建一个要测试自动缩放的 pod(取自 kubernetes 官方文档):
[root@ip-10-0-1-91 auto]# kubectl run --generator=run-pod/v1 php-apache --
image=k8s.gcr.io/hpa-example --requests=cpu=200m --expose --port=80
service/php-apache created
deployment.apps/php-apache created
现在创建一个自动缩放部署:
[root@ip-10-0-1-91 auto]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
horizontalpodautoscaler.autoscaling/php-apache autoscaled
现在检查 HPA,您的指标是否出现:
[root@ip-10-0-1-91 manifests]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 0%/50% 1 10 1 2m
现在使用以下命令从另一个窗口生成负载:
kubectl run -i --tty load-generator --image=busybox /bin/sh
它将打开一个 sh 终端,您可以使用以下命令从该 sh 终端运行负载:
while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done
您的 Pod 需要一分钟左右的时间才能承受足够的负载,然后您会看到繁荣:
[root@ip-10-0-1-91 manifests]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 120%/50% 1 10 4 7m
和 pod 缩放:
希望这有助于您的 HPA 发挥作用。
EDIT:
更换metrics-server-deployment.yaml
文件输入deploy/1.8+
使用以下 yaml 文件:
apiVersion: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: metrics-server
namespace: kube-system
labels:
k8s-app: metrics-server
spec:
selector:
matchLabels:
k8s-app: metrics-server
template:
metadata:
name: metrics-server
labels:
k8s-app: metrics-server
spec:
serviceAccountName: metrics-server
volumes:
# mount in tmp so we can safely use from-scratch images and/or read-only containers
- name: tmp-dir
emptyDir: {}
containers:
- command:
- /metrics-server
- --metric-resolution=30s
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP
name: metrics-server
image: k8s.gcr.io/metrics-server-amd64:v0.3.1
imagePullPolicy: Always
volumeMounts:
- name: tmp-dir
mountPath: /tmp
另外,启用--authentication-token-webhook
在 kubelet.conf 中,然后您将能够获取 HPA。
EDIT2:您需要在要为其创建 HPA 的部署文件(在您的情况下为 tomcat)中设置以下属性,然后只有您的 HPA 可以从您的部署中获取指标。
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"