Linux实战笔记-----HPA实例

1、单一限制

server1准备好hpa-exmple,可从官网拉取

docker pull mirrorgooglecontainers/hpa-example
docker tag mirrorgooglecontainers/hpa-example reg.westos.org/library/hpa-example
docker push reg.westos.org/library/hpa-example

server2

mkdir hpa
cd hpa/
vim 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

kubectl apply -f hpa.yaml
kubectl get svc

在这里插入图片描述
查看php-apache
在这里插入图片描述

创建 Horizontal Pod Autoscaler

kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
kubectl top pod

Linux实战笔记-----HPA实例_第1张图片
增加负载

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"

2、基于多项指标的自动扩缩

vim 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

应用并查看
在这里插入图片描述

你可能感兴趣的:(kubernetes,linux)