天天看点

k8s的HPA

依赖metrics-server& 安装metrics

  1. git clone https://github.com/kubernetes-incubator/metrics-server.git
  2. cd metrics-server/deploy/1.8+/
  3. 修改deployment的参数
增加
- --metric-resolution=30s
- --kubelet-preferred-address-types=InternalIP,           
  1. kubectl create -f .

部署完成问题排查

  1. 查看apiserver
kubectl get apiservices

v1beta1.metrics.k8s.io                 kube-system/metrics-server   False (FailedDiscoveryCheck)   31m
           
  1. 查看 apiservices/v1beta1.metrics.k8s.io
kubectl describe apiservices/v1beta1.metrics.k8s.io -n kube-system
Name:         v1beta1.metrics.k8s.io
Namespace:
Labels:       <none>
Annotations:  <none>
API Version:  apiregistration.k8s.io/v1
Kind:         APIService
Metadata:
  Creation Timestamp:  2019-12-19T05:43:01Z
  Resource Version:    1321294
  Self Link:           /apis/apiregistration.k8s.io/v1/apiservices/v1beta1.metrics.k8s.io
  UID:                 77883025-7c16-4c5f-8939-e3036d81118f
Spec:
  Group:                     metrics.k8s.io
  Group Priority Minimum:    100
  Insecure Skip TLS Verify:  true
  Service:
    Name:            metrics-server
    Namespace:       kube-system
    Port:            443
  Version:           v1beta1
  Version Priority:  100
Status:
  Conditions:
    Last Transition Time:  2019-12-19T05:43:01Z
    Message:               failing or missing response from https://10.201.2.18:443/apis/metrics.k8s.io/v1beta1: Get https://10.201.2.18:443/apis/metrics.k8s.io/v1beta1: dial tcp 10.201.2.18:443: connect: connection refused
    Reason:                FailedDiscoveryCheck
    Status:                False
    Type:                  Available
Events:                    <none>


其中10.201.2.18 是mestric的svc地址,所以原因为master节点连不通mestric的svc地址,需要在master节点部署kube-proxy           

实验自动伸缩

  1. 命令
kubectl autoscale deployment nginx --cpu-percent=50 --min=1 --max=10
           
  1. 查看hpa
kubectl get hpa
kubectl top node
kubectl top pod           
  1. yaml文件
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50           

继续阅读