1设置自动伸缩
使用自动伸缩需要先安装metrics-server
mkdir -p kubernetes/HPA/
cd kubernetes/HPA/
wget https://github.com/kubernetes-incubator/metrics-server/archive/v0.3.1.zip
unzip v0.3.1.zip
cd metrics-server-0.3.1
ll deploy/1.8+/
-rw-r–r-- 1 root root 308 Sep 17 07:05 auth-delegator.yaml
-rw-r–r-- 1 root root 329 Sep 17 07:05 auth-reader.yaml
-rw-r–r-- 1 root root 298 Sep 17 07:05 metrics-apiservice.yaml
-rw-r–r-- 1 root root 1098 Feb 13 02:02 metrics-server-deployment.yaml
-rw-r–r-- 1 root root 249 Sep 17 07:05 metrics-server-service.yaml
-rw-r–r-- 1 root root 612 Sep 17 07:05 resource-reader.yaml
修改deployment.yaml文件,修正集群问题
问题1:metrics-server默认使用节点hostname通过kubelet 10250端口获取数据,但是coredns里面没有该数据无法解析(10.96.0.10:53),可以在metrics server启动命令添加参数 --kubelet-preferred-address-types=InternalIP 直接使用节点IP地址获取数据
问题2:kubelet 的10250端口使用的是https协议,连接需要验证tls证书。可以在metrics server启动命令添加参数–kubelet-insecure-tls不验证客户端证书
问题3:yaml文件中的image地址k8s.gcr.io/metrics-server-amd64:v0.3.0 需要梯子,需要改成中国可以访问的image地址,可以使用aliyun的。这里使用hub.docker.com里的google镜像地址 image: mirrorgooglecontainers/metrics-server-amd64:v0.3.1
针对以上3个问题修正后的部署文件内容如下:
cat deploy/1.8+/metrics-server-deployment.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:
- name: metrics-server
#image: k8s.gcr.io/metrics-server-amd64:v0.3.0
image: mirrorgooglecontainers/metrics-server-amd64:v0.3.1
imagePullPolicy: IfNotPresent
command:
- /metrics-server
- --metric-resolution=30s
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
volumeMounts:
- name: tmp-dir
mountPath: /tmp
部署启动metrics-server
kubectl apply -f deploy/1.8+/
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.extensions/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
kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
metrics-server-784fd5cdc5-2dgmx 1/1 Running 0 13s
验证
查看集群节点资源使用情况(CPU,MEM)
kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
ejucsmaster-shqs-1 585m 2% 9695Mi 7%
ejucsmaster-shqs-2 300m 1% 8339Mi 6%
ejucsmaster-shqs-3 340m 1% 8469Mi 6%
ejucsnode-shqs-1 266m 1% 8580Mi 6%
ejucsnode-shqs-2 127m 0% 8733Mi 6%
ejucsnode-shqs-3 119m 0% 8558Mi 6%
安装完成以后每个pod需要配置CPU限制!!!
如果不配置无法获取到数据
设置myreplicaset 最少1个,最多10个,cpu资源最多使用40%
kubectl autoscale replicaset myreplicaset --min=1 --max=10 --cpu-percent=40
使用yaml方式部署
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: test-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: test-app
minReplicas: 1 #最小pod数
maxReplicas: 10 #最大pod数
targetCPUUtilizationPercentage: 70 #CPU占用百分比