pod 自动扩容

25 pod 自动扩展和缩减
HPA  Horizontal Pod Autoscaling (pod水平自动伸缩)

基本原理:
  HPA监控分析 RC、Deployment 控制的pod的负载变化 来调整副本的数量
  监控:CPU负载(Heapster)、自定义监控的负载

以容器的方式安装:heapster influxdb  grafana 三个插件:

cd heapster/

vi heapster.yaml
vi influxdb.yaml
vi grafana.yaml

kubectl create -f .
kubectl get pods -n kube-system -o wide    
查看pod建立在哪个节点上 然后到那个节点去拉取相应版本的包,并更名tag 为 k8s.gcr.io/xxx:v1xxx

下载
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-influxdb-amd64:v1.5.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-grafana-amd64:v5.0.4

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-amd64:v1.5.4

打tag
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-amd64:v1.5.4 k8s.gcr.io/heapster-amd64:v1.5.4

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-influxdb-amd64:v1.5.2 k8s.gcr.io/heapster-influxdb-amd64:v1.5.2

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/heapster-grafana-amd64:v5.0.4 k8s.gcr.io/heapster-grafana-amd64:v5.0.4


kubectl create -f .
或者:
kubectl create -f heapster.yaml
kubectl create -f influxdb.yaml
kubectl create -f grafana.yaml


查看报错日志:
kubectl logs -f heapster-7ff8d6bf9f-mq2vn -n kube-system

无监控pod负载权限,从原admin-account.yaml中拿取一段,如下:
修改处:
name: heapster-admin
name: heapster

vi heapster.yaml    加入ClousterRole 权限绑定

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: heapster
  namespace: kube-system

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: heapster-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: heapster
  namespace: kube-system


kubectl delete -f heapster.yaml
kubectl create -f heapster.yaml

kubectl logs -f heapster-7ff8d6bf9f-lkj22 -n kube-system

kubectl get pods -n kube-system -o wide

dashboard 管理界面查看图形界面监控数据:

hpa-demo.yaml

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: hpa-demo
  labels:
    app: hpa
spec:
  replicas: 1
  revisionHistoryLimit: 15   #最小数量历史版本为 15个
  minReadySeconds: 5         #5秒钟后更新
  strategy:              
    type: RollingUpdate      #滚动更新
    rollingUpdate:
      maxSurge: 1            #更新中 比原pod数最多多1个出来
      maxUnavailable: 1      #更新中 最多允许1个pod不能提供服务
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          requests:
            cpu: 100m
        ports:
        - containerPort: 80

kubectl create -f hpa-demo.yaml
kubectl get deployment
kubectl get pods

vi /etc/kubernetes/manifests/kube-controller-manager.yaml
- --horizontal-pod-autoscaler-use-rest-clients=false

创建自动扩容的资源对象:
kubectl autoscale deployment hpa-demo --min=1 --max=10 --cpu-percent=5

kubectl delete -f hpa-demo.yaml

kubectl get hpa
kubectl describe hpa hpa-demo

测试自动扩容:
kubectl run -i --tty test-hpa --image=busybox /bin/sh


panic: runtime error: invalid memory address or nil pointer dereference


kubectl 命令进入容器

kubectl get pods   #查看所有正在运行的pod
NAME                            READY     STATUS    RESTARTS   AGE
nginx-56b8c64cb4-t97vb          1/1       Running   3          1d
<2>
kubectl exec -it nginx-56b8c64cb4-t97vb /bin/bash      #假如当前pod只有一个容器,运行以下命令即可
root@nginx-56b8c64cb4-t97vb:/# ps -ef


kubectl exec -it test-hpa-54bfcf5b7b-fhnvc /bin/sh  

写个死循环做压力测试:
while true; do wget -q -O- http://10.244.1.89; done

kubectl get hpa
kubectl describe hpa hpa-demo

kubectl delete deployment test-hpa




得到yaml文件 格式内容:
kubectl get hpa hpa-demo -o yaml

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  annotations:
    autoscaling.alpha.kubernetes.io/conditions: '[{"type":"AbleToScale","status":"True","lastTransitionTime":"2019-11-07T11:07:02Z","reason":"SucceededGetScale","message":"the
      HPA controller was able to get the target''s current scale"},{"type":"ScalingActive","status":"False","lastTransitionTime":"2019-11-07T11:07:02Z","reason":"FailedGetResourceMetric","message":"the
      HPA was unable to compute the replica count: missing request for cpu on container
      nginx in pod default/nginx-deploy-75675f5897-gmttv"}]'
  creationTimestamp: 2019-11-07T10:48:43Z
  name: hpa-demo
  namespace: default
  resourceVersion: "1365696"
  selfLink: /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/hpa-demo
  uid: 2ab4536f-014c-11ea-8413-000c29c20a27
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: hpa-demo
  targetCPUUtilizationPercentage: 5
status:
  currentReplicas: 1
  desiredReplicas: 0



简化后:
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-demo
  namespace: default
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: hpa-demo
  targetCPUUtilizationPercentage: 5


kubectl delete -f hpa hpa-demo
kubectl delete -f hpa-demo.yaml
kubectl delete deployment test-hpa


vi hpa-demo.yaml

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-demo
  namespace: default
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: hpa-demo
  targetCPUUtilizationPercentage: 5


kubectl delete -f hpa-demo.yaml

kubectl create -f hpa-demo.yaml

kubectl get pods
kubectl get hpa

猜你喜欢

转载自www.cnblogs.com/walkersss/p/11815320.html
pod