弹性伸缩

当CPU负载压力过了一定的量时,pod会自动扩容,当负载压力下降时,会自动减少pod

需要依赖heapster监控服务

1. 导入heapster监控包

image.png
for n in `ls *.tar.gz`;do docker load -i $n ;done

docker tag docker.io/kubernetes/heapster_grafana:v2.6.0 10.0.0.11:5000/heapster_grafana:v2.6.0
docker tag  docker.io/kubernetes/heapster_influxdb:v0.5 10.0.0.11:5000/heapster_influxdb:v0.5
docker tag docker.io/kubernetes/heapster:canary 10.0.0.11:5000/heapster:canary

上传监控服务的配置文件

image.png
数据的来源  : node节点kubelete的cadvisor服务
influxdb    数据库服务
grafana     出图,可视化工具
heapster    采集数据

创建资源

vim heapster-controller.yaml 把总是跟新镜像删掉或者改成imagePullPolicy: IfNotPresent

kubectl create -f .  
image.png

2.创建hpa

###一定要限制CPU
vim vim k8s_deploy.yaml 

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: lixin
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: 10.0.0.11:5000/nginx:1.13
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 100m
          requests:
            cpu: 100m

kubectl create namespace lixin
kubectl create -f .

image.png

创建弹性伸缩规则

kubectl autoscale  -n lixin deployment nginx-deployment  --max=8 --min=1 --cpu-percent=8

autoscale    自动伸缩规则
-n lixin         指定命名空间
deployment   指定资源类型
 nginx-deployment   指定资源类型名字
--max=8       最多能起多少个pod
--min=1         最少
 --cpu-percent=8    cpu使用百分比 (在生产上最好在60左右)


### 创建之后会立即生效 因为没有负载压力之前创建的pod是三个现在变成了一个
kubectl get all -n lixin -o wide
image.png

进行压力测试

yum install httpd-tools -y
ab -n 3000000 -c 100  172.16.20.16/index.html 
-n 一次发起多少请求
-c 每次多少并发
image.png
  • 看以上图CPU开始负载过高已经自动扩容
  • 当压力释放时 pod会慢慢减少


    image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容