首页 > 系统相关 >基于cpu和内存进行pod扩容,创建hpa

基于cpu和内存进行pod扩容,创建hpa

时间:2023-04-07 17:55:07浏览次数:76  
标签:kubectl cpu nginx apache hpa php root pod

基于cpu和内存进行pod扩容,创建hpa

创建镜像

mkdir php
cd php
touch dockerfile
touch index.php

vim dockerfile
FROM php:5-apache
ADD index.php /var/www/html/index.php
RUN chmod a+rx index.php


vim index.php
<?php
$x = 0.0001;
for ($i = 0; $i <= 1000000;$i++) {
$x += sqrt($x);
}
echo "OK!";
?>

docker build -t k8s.gcr.io/hpa-example:v1 .

docker save -o hpa-example.tar.gz k8s.gcr.io/hpa-example:v1

 

创建deployment,service

 

vim php-apache.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: k8s.gcr.io/hpa-example:v1
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 php-apache.yaml

  

创建hpa,基于cpu指标


1)让副本数维持在 1-10 个之间(这里副本数指的是通过 deployment 部署的 pod 的副本数)
2)将所有 Pod 的平均 CPU 使用率维持在 50%(通过 kubectl run 运行的每个 pod 如果是 200
毫核,这意味着平均 CPU 利用率为 100 毫核)

创建hpa

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

kubectl autoscale deployment php-apache (php-apache 表示 deployment 的名字) --
cpu-percent=50(表示 cpu 使用率不超过 50%) --min=1(最少一个 pod)
--max=10(最多 10 个 pod)

[root@master php]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10

horizontalpodautoscaler.autoscaling/php-apache autoscaled
You have new mail in /var/spool/mail/root
[root@master php]#
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache <unknown>/50% 1 10 0 6s

 

压力测试

busybox
kubectl run v1 -it --image=busybox /bin/sh
while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done


kubectl run v2 -it --image=busybox /bin/sh
while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done


[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache <unknown>/50% 1 10 0 6s
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 0%/50% 1 10 1 2m8s
You have new mail in /var/spool/mail/root
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 0%/50% 1 10 1 2m13s
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 0%/50% 1 10 1 2m15s
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 179%/50% 1 10 4 2m53s
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 179%/50% 1 10 4 2m54s
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 82%/50% 1 10 5 3m8s
You have new mail in /var/spool/mail/root
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 82%/50% 1 10 5 3m11s

 

 

停止一个请求,还有一个请求存在,发现pod副本的数量没有降下来

kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
php-apache-775dff594-48dp8 1/1 Terminating 0 7m8s 192.168.166.147 node1 <none> <none>
php-apache-775dff594-6q4tv 1/1 Running 0 6m53s 192.168.166.149 node1 <none> <none>
php-apache-775dff594-htfs9 1/1 Terminating 0 6m38s 192.168.166.150 node1 <none> <none>
php-apache-775dff594-jct7v 1/1 Running 0 7m8s 192.168.135.17 node3 <none> <none>
php-apache-775dff594-jnm54 1/1 Running 0 13m 192.168.166.146 node1 <none> <none>
php-apache-775dff594-mth8j 1/1 Running 0 7m8s 192.168.135.16 node3 <none> <none>
php-apache-775dff594-qjp9k 1/1 Running 0 6m38s 192.168.135.18 node3 <none> <none>
php-apache-775dff594-sbkhq 1/1 Terminating 0 6m7s 192.168.166.151 node1 <none> <none>
php-apache-775dff594-wxv4k 1/1 Running 0 6m8s 192.168.135.19 node3 <none> <none>
v1 1/1 Running 0 8m19s 192.168.135.15 node3 <none> <none>

发现有的pod在逐渐的删除

  

 


基于内存指标进行pod的扩容

 

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-hpa
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
name: http
protocol: TCP
resources:
requests:
cpu: 0.01
memory: 25Mi
limits:
cpu: 0.05
memory: 60Mi
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
selector:
app: nginx
type: NodePort
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
nginx 的 pod 里需要有如下字段,否则 hpa 会采集不到内存指标
resources:
requests:
cpu: 0.01
memory: 25Mi
limits:
cpu: 0.05
memory: 60Mi


创建hpa
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-hpa
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 60

[root@master mem]# kubectl api-versions|grep auto
autoscaling/v1
autoscaling/v2
[root@master mem]# vim mem-hpa.yaml
[root@master mem]# kubectl apply -f mem-hpa.yaml
Error from server (BadRequest): error when creating "mem-hpa.yaml": HorizontalPodAutoscaler in version "v2" cannot be handled as a HorizontalPodAutoscaler: strict decoding error: unknown field "spec.metrics[0].resource.targetAverageUtilization"

The HorizontalPodAutoscaler "nginx-hpa" is invalid:
* spec.metrics[0].resource.target.type: Required value: must specify a metric target type
* spec.metrics[0].resource.target.type: Invalid value: "": must be either Utilization, Value, or AverageValue

 


HorizontalPodAutoscaler in version "v2" cannot be handled as a HorizontalPodAutoscaler

averageUtilization:60
指定 averageUtilization 和所有 pod 的目标平均内存利用率,表示为请求内存的百分比。目标 pod 必须配置内存请求

[root@master mem]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-hpa Deployment/nginx-hpa 27%/60% 1 10 1 58s

kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-hpa-7d44f846d6-fhgkp 1/1 Running 0 11m


登录到上面通过 pod 创建的 nginx,并生成一个文件,增加内存

kubectl exec -it nginx-hpa-7d44f846d6-fhgkp -- /bin/sh
dd if=/dev/zero of=/tmp/a

[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-hpa Deployment/nginx-hpa 91%/60% 1 10 1 3m2s
[root@master php]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx-hpa Deployment/nginx-hpa 181%/60% 1 10 2 3m4s

扩展:查看 v2 版本的 hpa 如何定义?
[root@master mem]# kubectl get hpa.v2beta2.autoscaling -o yaml > 1.yaml
error: the server doesn't have a resource type "hpa"
[root@master mem]# kubectl get hpa.v2.autoscaling -o yaml > 1.yaml

  


 

标签:kubectl,cpu,nginx,apache,hpa,php,root,pod
From: https://www.cnblogs.com/lzjloveit/p/17296978.html

相关文章

  • 实战-JAVA应用程序CPU占用率飙升,定位线程的堆栈信息
    分以下几个步奏:(1)使用命令top-p<pid>,显示你的java进程的cpu情况,pid是你的java进程号,比如14203。(使用jps可以获取到java的进程id或者top直接查看)(2)按H,获取每个线程的CPU情况。(shirt+H)(3)找到内存和cpu占用最高的线程tid,比如14204。(4)转为十六进制得到377C,此为线程id的十六进......
  • A NAIVE CON2D CPU IMPL
    /******************************************************************************WelcometoGDBOnline.GDBonlineisanonlinecompileranddebuggertoolforC,C++,Python,Java,PHP,Ruby,Perl,C#,OCaml,VB,Swift,Pascal,Fortran,Haskell,Objective......
  • 安装wsl的必备操作——开启CPU虚拟化——WslRegisterDistribution failed with error_
    参考:https://www.cnblogs.com/smdtxz/p/16837946.htmlhttps://www.cnblogs.com/wenonly/p/17206040.htmlhttps://blog.csdn.net/qq_41460654/article/details/118026986  ======================================================  因为实验室需要炼丹,而炼丹要用ubun......
  • GaussDB(DWS)集群中寻找节点CPU占用高的语句
    摘要:本文主要通过实例讲解如何通过gs_cpuwatcher.sh脚本寻找CPU占用高语句。本文分享自华为云社区《GaussDB(DWS)gs_cpuwatcher.sh脚本如何寻找CPU占用高语句》,作者:fighttingman。【工具名称】gs_cpuwatcher【功能描述】1.寻找集群内节点占用CPU高的语句【使用场景】CPUsys使用......
  • GaussDB(DWS)集群中寻找节点CPU占用高的语句
    摘要:本文主要通过实例讲解如何通过gs_cpuwatcher.sh脚本寻找CPU占用高语句。本文分享自华为云社区《GaussDB(DWS)gs_cpuwatcher.sh脚本如何寻找CPU占用高语句》,作者:fighttingman。【工具名称】gs_cpuwatcher【功能描述】1.寻找集群内节点占用CPU高的语句【使用场景】CP......
  • 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
    DeviceYearClass的主要功能是根据CPU核数、时钟频率以及内存大小对设备进行分级。下表是Facebook公司提供的分级标准,其中Year栏表示分级结果。以下类就是从Facebook的开源项目DeviceYearClass中拿出来的packagecom.yirui.youbao.util;importandroid.annotati......
  • 保证高效写入查询的情况下,如何实现 CPU 资源和磁盘 IO 的最低开销?
    从《写入性能:TDengine最高达到InfluxDB的10.3倍,TimeScaleDB的6.74倍》、《查询性能:TDengine最高达到了InfluxDB的37倍、TimescaleDB的28.6倍》两篇文章中,我们发现,TDengine不仅在写入和查询性能上超越了InfluxDB和TimescaleDB,在数据处理过程的资源消耗也比两......
  • 48、K8S-调度机制-污点、容忍度调度之节点的Traint、Pod的tolerations
    Kubernetes学习目录1、基础知识1.1、回顾我们之前所学的所有调度策略,都是基于节点调度和pod调度选择我们新的pod资源应该如何创建,而实际的生产角度上,往往会出现基于node节点的属性来选择是否让新的pod资源进行创建,但是对于节点来说,没有所谓的节点反亲和性,但是有一种类似的策略......
  • 马哥课程回顾-----pod
      kubectlapply-fpod1.yamlkubectlgetpod  kubectlexec-itpod1-ctestpod-dev--/bin/bash  //pod1是本实验的Pod名称,testpod-dev是本实验pod1中的容器名称,该docker容器部署了nginx  查看pod详情  查看pod日志: ......
  • Podman Compose 新手指南
    作者:袁首京原创文章,转载时请保留此声明,并给出原文连接。技术人员多数又呆板又花心不长久。我知道你可能已经厌倦了Docker,但是系统还没有复杂到需要高攀K8S的地步。那我建议您,有空的话可以约一下Podman。Podman使用起来是足够简单的,直接把它当做改了名字的Docker就可......