24-k8s的附件组件-Metrics-server组件与hpa资源pod水平伸缩

一、概述

Metrics-Server组件目的:获取集群中pod、节点等负载信息;

hpa资源目的:通过metrics-server获取的pod负载信息,自动伸缩创建pod;

参考链接:

资源指标管道 | Kubernetes

https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/metrics-server

GitHub - kubernetes-sigs/metrics-server: Scalable and efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines.

二、安装部署Metrics-Server组件

就是给k8s集群安装top命令的意思;

1,下载Metrics-Server资源清单

· 第一种方式:github下载

root@k8s231 metricsserver# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/high-availability-1.21+.yaml

· 第二种方式:本地上传安装包

百度云盘;

链接:https://pan.baidu.com/s/1axn44_AsbHQxIMw9nuNVMw?pwd=jtqb

提取码:jtqb

2,编辑Metrics-Server的资源清单

root@k8s231 metricsserver# vim high-availability-1.21+.yaml

spec:

affinity:

podAntiAffinity:

requiredDuringSchedulingIgnoredDuringExecution:

  • labelSelector:

matchLabels:

k8s-app: metrics-server

namespaces:

  • kube-system

topologyKey: kubernetes.io/hostname

containers:

  • args:

#启动允许使用不安全的证书

  • --kubelet-insecure-tls

  • --cert-dir=/tmp

  • --secure-port=10250

  • --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname

  • --kubelet-use-node-status-port

  • --metric-resolution=15s

#image: registry.k8s.io/metrics-server/metrics-server:v0.7.0

image: registry.aliyuncs.com/google_containers/metrics-server:v0.6.3

3,创建Metrics-Server资源

root@k8s231 metricsserver# kubectl apply -f high-availability-1.21+.yaml

4,验证Metrics-Server是否成功安装

· 查看pod

root@k8s231 metricsserver# kubectl get pods -A

· 使用top命令测试是否管用

查节点的top值

root@k8s231 metricsserver# kubectl top node

查看pod的top值

root@k8s231 metricsserver# kubectl top pods -A

三、hpa资源实现pod水平伸缩(自动扩缩容)

1,当资源使用超一定的范围,会自动扩容,但是扩容数量不会超过最大pod数量;

2,扩容时无延迟,只要监控资源使用超过阔值,则会直接创建pod;

3,当资源使用率恢复到阔值以下时,需要等待一段时间才会释放,大概时5分钟;

1,编辑deployment资源

root@k8s231 hpa# cat deploy.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: dm-hpa

spec:

replicas: 1

selector:

matchLabels:

k8s: xinjizhiwa

template:

metadata:

labels:

k8s: xinjizhiwa

spec:

containers:

  • name: c1

image: centos:7

command:

  • tail

  • -f

  • /etc/hosts

resources:

requests:

cpu: "50m"

limits:

cpu: "150m"

2,编写hpa资源清单

root@k8s231 hpa# cat hpa.yaml

apiVersion: autoscaling/v1

kind: HorizontalPodAutoscaler

metadata:

name: hpa-tools

spec:

#指定pod最大的数量是多少(自动扩容的上限)

maxReplicas: 10

#指定pod最小的pod数量是多少(自动缩容的下限)

minReplicas: 2

#弹性伸缩引用的目标是谁?

scaleTargetRef:

#目标资源的api

apiVersion: "apps/v1"

#目标资源的类型kind

kind: Deployment

#目标资源的名称metadata-name是什么

name: dm-hpa

#使用cpu阈值(使用到达多少,开始扩容、缩容)

#95%

targetCPUUtilizationPercentage: 95

3,创建hpa和deploy资源

root@k8s231 hpa# kubectl apply -f .

4,查看hpa资源

root@k8s231 hpa# kubectl get hpa -o wide

到这里,就已经实现了自动扩缩容的pod副本了;

至此,咱们的metrics-server组件和hpa资源,就学习完毕了;

四、压测测试

1,进入pod,安装stress工具

· 进入pod容器

root@k8s231 hpa# kubectl exec dm-hpa-5bb4dd448d-ks2rt -it -- sh

· 安装aili源和epel源

sh-4.2# yum -y install wget

sh-4.2# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

sh-4.2# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

· 安装压测工具

sh-4.2# yum -y install stress

2,开始使用命令压测pod

sh-4.2# stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 20m

3,查看hpa资源的负载情况

root@k8s231 \~# kubectl get hpa -o wide

可以看到:

1,我们创建的deploy资源只有一个副本;

2,我们创建的hpa资源之后,设置最小值是2,最大值是10 ;

3,我们在查看pod,可以看见,pod变成了2个;

4,我们进入容器,开始压测,将负载压测到超过95%;

5,再次查看pod,发现变成了3个,自动创建了一个;

6,关闭压测,5分钟后,pod有回归到了2个;

7,至此,hpa的pod自动伸缩,测试完毕;

相关推荐
AOwhisky32 分钟前
Linux(CentOS)系统管理入门笔记(第一期)——从 Multics 到主流发行版
linux·运维·笔记·centos·云计算
从零开始的代码生活_33 分钟前
Linux epoll 多路转接详解
linux·运维·网络·后端·tcp/ip·计算机网络·php
渣渣盟41 分钟前
Linux软件管理与编辑器命令速查手册
linux·运维·编辑器
我叫张小白。1 小时前
一个微服务电商+社区项目(瓷韵app)的技术深度复盘
docker·微服务·云原生·架构
无足鸟ICT1 小时前
【RHCA+】扩展正则表达式
linux·正则表达式
咕噜咕噜的猪大侠1 小时前
《K8s 核心知识梳理:Deployment 滚动更新、JobCronJob、DaemonSet 和 Service》
云原生·容器·kubernetes
虚心的百褶裙1 小时前
远程调用服务架构设计及zookeeper技术详解(下篇)
分布式·zookeeper·云原生
一池秋_1 小时前
ubuntu(linux)完美复刻windows11字体,解决linux字体费眼晴
linux·运维·ubuntu
Geek-Chow1 小时前
微服务认证与授权:08 — OPA(PDP)
微服务·云原生·架构
随风一样自由1 小时前
【WSL+Linux】国内网络连接微软官方服务器(或GitHub)不稳定,导致下载内核或Linux发行版时超时断开如何解决?
linux·服务器·wsl