k8s hpa弹性伸缩和namespace(十)

it2024-03-09  77

k8s hpa弹性伸缩

1.Horizontal Pod Autoscaler

自动扩展作为一个长久的议题,一直为人们津津乐道,系统能够根据负载的变化对计算资源的分配进行自动的扩增或者收缩,无疑是一个非常吸引人的特征,它能够最大可能减少费用或者其他代价。

自动扩展主要分为两种,其一是水平扩展,针对实例数目的增减,其二是垂直扩展,即单个实例可以使用的资源

2.Horizontal Pod Autoscaler工作方式

hpa操作对象是rc或者deployment对应的pod资源,根据观察到的CPU实际使用量与用户的期望值进行比对,做出是否需要增减实例的决策,controller目前使用heapster来检测CPU使用率,检测周期为30s。

期望值比对比如deployment设置的pod数量为2,但是hpa最小资源数设置的是3,过30s就会自动将deployment的pod数量改为3。

3.Horizontal Pod Autoscaler决策

在hpa检测到CPU的实际使用量之后,会求出当前的CPU使用率,然后hpa会通过调整副本数量使得CPU使用率尽量向期望值靠近,另外考虑到自动伸缩的决策可能需要一段时间才能生效,甚至在短时间内会引入一些噪声,例如当pod所需要的CPU负荷过大,从而运行一个新的pod分流,在创建的过程中,系统的CPU使用量可能会有一个攀升的过程,所以再每次做出决策后的端时间内,将不再进行扩展决策,对于scaleup而言,这个时间段为3分钟,scaledown为5分钟。

4.hpa的使用

4.1.hpa语法格式

kubectl autoscale deployment nginx-deployment --max=10 --min=5 --cpu-percent=80 kubectl autoscale 资源类型 资源名 --max=最大pod数 --min=最小pod数 --cpu-percent=当cpu到达多少时创建策略

4.2.创建一个hpa

对deployment资源的nginx做一个弹性伸缩

1)创建弹性伸缩 [root@k8s-master delpoyment]# kubectl autoscale deployment nginx-deployment --max=10 --min=5 --cpu-percent=80 deployment "nginx-deployment" autoscaled 2)查看资源 [root@k8s-master delpoyment]# kubectl get hpa NAME REFERENCE TARGET CURRENT MINPODS MAXPODS AGE nginx-deployment Deployment/nginx-deployment 80% 0% 5 10 40m [root@k8s-master delpoyment]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deployment 5 5 5 5 41m

5.namespace的使用

k8s默认创建的资源都会存到default命名空间中,这样看起来很乱,假如我想起的资源,资源名一样这时无法创建,一个名称空间中不允许有相同的资源名称,这时就可以创建多个namespace,企业环境中也是一个项目一个名称空间

5.1.查看所有的名称空间

[root@k8s-master k8s]# kubectl get namespace NAME STATUS AGE default Active 17d kube-system Active 17d

5.2.创建、删除一个名称空间

创建 [root@k8s-master k8s]# kubectl create namespace name1 namespace "name1" created [root@k8s-master k8s]# kubectl get namespace NAME STATUS AGE default Active 17d kube-system Active 17d name1 Active 1s 删除 [root@k8s-master nginx]# kubectl delete namespace nginx02 namespace "nginx02" deleted

5.3.在名称空间中创建资源

1)创建一个namespace为nginx [root@k8s-master tomcat]# kubectl create namespace nginx namespace "nginx" created 2)创建一个pod资源指定namespace为nginx [root@k8s-master nginx]# vim k8s_pod.yml apiVersion: v1 kind: Pod metadata: name: nginx3 namespace: nginx #指定namespace labels: app: web spec: containers: - name: nginx image: 192.168.81.240/k8s/nginx:1.15 ports: - containerPort: 80 3)查看namespace [root@k8s-master nginx]# kubectl get pod --namespace=nginx NAME READY STATUS RESTARTS AGE nginx3 1/1 Running 0 38s

5.4.迁移资源到指定namespace中

将刚刚创建的tomcat资源迁移到新的namespace中

1)创建一个新的namespace为nginx02 [root@k8s-master nginx]# kubectl create namespace nginx02 namespace "nginx02" created 2)修改配置文件,将namespace指定为nginx02 [root@k8s-master nginx]# sed -ri '/namespace:/c \ \ namespace: nginx02' k8s_pod.yml 3)更新配置 [root@k8s-master nginx]# kubectl apply -f k8s_pod.yml pod "nginx3" created 5)查看新的名称空间发现资源已迁移过来 [root@k8s-master nginx]# kubectl get pod -n nginx02 NAME READY STATUS RESTARTS AGE nginx3 1/1 Running 0 21s
最新回复(0)