k8s 之HPA应用

1、创建测试deployment

apiVersion: v1
kind: Service
metadata:
  name: my-app
  namespace: default
spec:
  type: NodePort
  selector: 
    app: my-app
    release: canary
  ports:
  - name: http
    nodePort: 30327
    port: 80
  
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deploy
  namespace: default
spec:
  replicas: 3
  selector: 
    matchLabels:
      app: my-app
      release: canary
  template:
    metadata:
      labels:
        app: my-app
        release: canary
    spec:
      containers:
      - name: my-app
        image: ikubernetes/myapp:v2
        ports: 
        - name: http
          containerPort: 80
        resources:
requests:
cpu: 128m
memory: 128Mi
limits:
cpu: 128m
memory: 128Mi

 

kubectl create -f app.yaml

 

[root@10 ingress]#  kubectl get pod | grep my-app
my-app-deploy-76f86b954b-5r5n7                                    1/1     Running            0          3m25s
my-app-deploy-76f86b954b-8n2dp                                    1/1     Running            0          3m25s
my-app-deploy-76f86b954b-d95d8                                    1/1     Running            0          3m25s
[root@10 ingress]# kubectl get svc | grep my-app 
my-app                                                NodePort       192.168.192.192   <none>        80:30327/TCP                                                   3m29s
[root@10 ingress]# 

 

2、创建HPA

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: my-app-hpa # 名称
  namespace: default #k8s命名空间
spec:
  maxReplicas: 10  # 最大副本数
  minReplicas: 3   # 最小副本数
  scaleTargetRef:   
    apiVersion: apps/v1beta1
    kind: Deployment  
    name: my-app-deploy   # 监控名为my-app-deploy的Deployment
  targetCPUUtilizationPercentage: 20  # cpu 阈值
kubectl create -f hap.yaml

[root@10 zhangpan]# kubectl get hpa
NAME         REFERENCE                  TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
my-app-hpa   Deployment/my-app-deploy   <unknown>/80%   2         10        3          77s
[root@10 zhangpan]# 

 

也可以直接用命令创建hpa

kubectl autoscale deployment myapp --min=2 --max=10 --cpu-percent=20

 

如果出现unknown,可以查看原始deployment的resource有没有设置cpu的限制,或者没有找到deployment,也有可能是由于heapster组件没有安装,也有可能是数据没有检测到,默认heapster采集时间为1分,需要稍等一会看一下状态。

可以用kubectl describe hpa my-app-hpa 查看具体原因

正常后显示如下:

[root@10 zhangpan]# kubectl get hpa
NAME         REFERENCE                  TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
my-app-hpa   Deployment/my-app-deploy   0%/20%    2         10        2          6m29s
[root@10 zhangpan]# 

 

上一篇:愿风指引你的道路


下一篇:kubernetes 自定义指标HPA