1.下载并解压Metrics-Server
https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz
tar -zxvf v0.3.6.tar.gz
2.修改Metrics-Server配置文件
cd metrics-server-0.3.6/deploy/1.8+/
vim metrics-server-deployment.yaml
vim metrics-server-deployment.yaml文件(修改后的)
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
apiVersion: apps/v1
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: mirrorgooglecontainers/metrics-server-amd64:v0.3.6
imagePullPolicy: IfNotPresent
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalDNS,InternalIP,ExternalDNS,ExternalIP,Hostname
volumeMounts:
- name: tmp-dir
mountPath: /tmp
resources:
limits:
cpu: 300m
memory: 200Mi
requests:
cpu: 200m
memory: 100Mi
3.安装Metrics-Server
kubectl apply -f metrics-server-0.3.6/deploy/1.8+/
4.查看node信息
[root@master 1.8+]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master 289m 7% 1315Mi 48%
node01 119m 2% 910Mi 33%
node02 100m 2% 628Mi 23%
5、测试HPA
1)、创建部署deployment,这里设置副本数是1
apiVersion: v1
kind: Service
metadata:
name: svc-hpa
namespace: default
spec:
selector:
app: nginx
type: NodePort ##注意这里是NodePort,下面压力测试要用到。
ports:
- name: http
port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: docker.io/nginx
ports:
- containerPort: 80
resources:
limits:
cpu: 50m
memory: 50Mi
requests:
cpu: 50m
memory: 50Mi
2)、设定自动扩容的条件
kubectl autoscale deployment nginx-deployment --max=3 --min=1 --cpu-percent=50
使用ab工具进行压测
先安装ab
yum -y install httpd-tools -y
3)、使用ab工具进行压测
ab -c 500 -n 2000 http://192.168.111.129:31379/
发现副本成两个了
[root@master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-859b474bd-2nnlv 1/1 Running 0 60m
nginx-deployment-859b474bd-pfm4l 1/1 Running 0 4m38s
过一段时间又变成了一个
[root@master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-859b474bd-2nnlv 1/1 Running 0 61m
nginx-deployment-859b474bd-pfm4l 0/1 Terminating 0 5m23s
[root@master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-859b474bd-2nnlv 1/1 Running 0 61m
看下hpa
[root@master test]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-deployment Deployment/nginx-deployment 10%/50% 1 3 1 50m