Docker (21)--Docker k8s--Kubernetes storage--kubernetes monitoring--HPA instance

1. HPA instance

Official website

1.1 Single restriction

## 1。 拉取镜像
[root@server1 harbor]# docker pull mirrorgooglecontainers/hpa-example   ##下载测试镜像
[root@server1 harbor]# docker tag mirrorgooglecontainers/hpa-example reg.westos.org/library/hpa-example
[root@server1 harbor]# docker push reg.westos.org/library/hpa-example



## 2. 操作
[root@server2 ~]# mkdir hpa
[root@server2 ~]# cd hpa/
[root@server2 hpa]# vim hap.yaml
[root@server2 hpa]# cat hap.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
spec:
  selector:
    matchLabels:
      run: php-apache
  replicas: 1
  template:
    metadata:
      labels:
        run: php-apache
    spec:
      containers:
      - name: php-apache
        image: hpa-example
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: php-apache
  labels:
    run: php-apache
spec:
  ports:
  - port: 80
  selector:
    run: php-apache

[root@server2 hpa]# kubectl apply -f hpa.yaml 
deployment.apps/php-apache created
service/php-apache created
[root@server2 hpa]# kubectl  get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   11d
php-apache   ClusterIP   10.111.164.87   <none>        80/TCP    7s
[root@server2 hpa]# kubectl  get pod
NAME                          READY   STATUS    RESTARTS   AGE
php-apache-6cc67f7957-hdj9k   1/1     Running   0          15s

1.1.1. Operation

Insert picture description here

1.1.2 Create Horizontal Pod Autoscaler

#php-apache 服务器已经运行,我们将通过 kubectl autoscale 命令创建 Horizontal Pod Autoscaler。 以下命令将创建一个 Horizontal Pod Autoscaler 用于控制我们上一步骤中创建的 Deployment,使 Pod 的副本数量维持在 1 到 10 之间。 大致来说,HPA 将(通过 Deployment)增加或者减少 Pod 副本的数量以保持所有 Pod 的平均 CPU 利用率在 50% 左右(由于每个 Pod 请求 200 毫核的 CPU,这意味着平均 CPU 用量为 100 毫核)。自动扩缩完成副本数量的改变可能需要几分钟的时间。Hpa会根据Pod的CPU使用率动态调节Pod的数量

[root@server2 hpa]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10     ##设置
horizontalpodautoscaler.autoscaling/php-apache autoscaled
[root@server2 hpa]# kubectl get hpa
NAME         REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   <unknown>/50%   1         10        0          8s
[root@server2 hpa]# kubectl top pod
NAME                          CPU(cores)   MEMORY(bytes)   
php-apache-6cc67f7957-hdj9k   1m           5Mi           

Insert picture description here
Insert picture description here

1.1.3 Increase load

[root@server2 hpa]# kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"   ##增加负载命令

[root@server2 ~]# kubectl  get pod
[root@server2 ~]# kubectl  top pod     ##查看pod占用量
[root@server2 ~]# kubectl describe svc php-apache   ##详细查看服务情况
[root@server2 ~]# kubectl get hpa     ##查看占用量

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

1.1.4 Stop load

我们将通过停止负载来结束我们的示例。
在我们创建 busybox 容器的终端中,输入<Ctrl> + C 来终止负载的产生。	
然后我们可以再次检查负载状态(等待几分钟时间):

1.2 Automatic scaling based on multiple indicators

[root@server2 ~]# vim hpa-v2.yaml
[root@server2 ~]# cat hpa-v2.yaml 
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  maxReplicas: 10
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        averageUtilization: 60
        type: Utilization
  - type: Resource
    resource:
      name: memory
      target:
        averageValue: 50Mi
        type: AverageValue

[root@server2 hpa]# kubectl get pod
[root@server2 hpa]# kubectl get hpa
[root@server2 hpa]# kubectl top pod

Insert picture description here

1.3 Algorithm to see the official website

Guess you like

Origin blog.csdn.net/qwerty1372431588/article/details/114309044
Recommended