1、各种缩写的应用
[root@k8s-master test]# kubectl get rs
NAME DESIRED CURRENT READY AGE
test001-64c7957b5c 2 2 2 8m59s
test001-698b98bb8f 0 0 0 12m
[root@k8s-master test]# kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
test001-64c7957b5c 2 2 2 9m19s
test001-698b98bb8f 0 0 0 12m
[root@k8s-master test]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
test001 2/2 2 2 12m
[root@k8s-master test]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
test001 2/2 2 2 12m
[root@k8s-master test]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
test001 2/2 2 2 12m
[root@k8s-master test]# kubectl get po
NAME READY STATUS RESTARTS AGE
test001-64c7957b5c-2w5jt 1/1 Running 0 7m2s
test001-64c7957b5c-gr5vp 1/1 Running 1 (69s ago) 11m
[root@k8s-master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test001-64c7957b5c-2w5jt 1/1 Running 0 7m4s
test001-64c7957b5c-gr5vp 1/1 Running 1 (71s ago) 11m
[root@k8s-master test]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test001-64c7957b5c-2w5jt 1/1 Running 0 7m8s
test001-64c7957b5c-gr5vp 1/1 Running 1 (75s ago) 11m
2、StatefulSet 有状态应用
StatefulSet管理有差别容器
1)StatefulSet 概念
StatefulSet 是 k8s 中的一个控制器资源对象,用于管理 有状态应用的部署和运行。
2)StatefulSet 特点
稳定的网络标识:StatefulSet 管理的 Pod 实例都有 一个唯一的网络标识符(通常为 DNS 名称),用于在 有状态应用中进行唯一标识和访问。
有序部署和扩展:StatefulSet 在部署和扩展 Pod 实 例时,会按照序号的顺序逐个创建和启动,确保有状 态应用的有序初始化和配置。
稳定的持久化存储:StatefulSet 具备与 Pod 实例关 联的持久化存储卷 (Persistent Volume)的能力,确保 每个实例可以访问和使用相应的持久化数据。
有状态应用的有序终止:在进行有状态应用的缩容或 删除时,StatefulSet 会按照序号的逆序进行有序的终 止操作,以确保数据的完整性和有状态应用的正常关 闭。
3)Headless Service
Headless Services 是一种特殊的 Service,其 spec:clusterIP 表示为 None,这样在实际运行时就不会 被分配 ClusterIP,使用 Endpoint 在 Pod 之间互相通 信。Headless Service 也被称为无头服务。
4)Headless Service和普通Service的区别:
5)StatefulSet 和 Deployment 控制器的区别:
6)Headless 一般格式
注:Headless Service需提前创建
7)使用yaml文件创建删除 StatefulSet应用
(1)下载并引入镜像
[root@k8s-master pods]# docker pull redis:latest
[root@k8s-master pods]# docker pull redis:7.0.14
[root@k8s-master pods]# docker save -o redis.7.0.14.tar redis:7.0.14
[root@k8s-master pods]# docker save -o redis.latest.tar redis:latest
[root@k8s-master pods]# scp redis.7.0.14.tar k8s-node01:/root
redis.7.0.14.tar 100% 128MB 67.0MB/s 00:01
[root@k8s-master pods]# scp redis.7.0.14.tar k8s-node02:/root
redis.7.0.14.tar 100% 128MB 73.2MB/s 00:01
[root@k8s-master pods]# scp redis.latest.tar k8s-node02:/root
redis.latest.tar 100% 115MB 73.1MB/s 00:01
[root@k8s-master pods]# scp redis.latest.tar k8s-node01:/root
redis.latest.tar 100% 115MB 64.5MB/s 00:01
[root@k8s-node01 ~]# ctr -n k8s.io images import redis.latest.tar --platform=linux/amd64
[root@k8s-node01 ~]# ctr -n k8s.io images import redis.7.0.14.tar --platform=linux/amd64
[root@k8s-node02 ~]# ctr -n k8s.io images import redis.7.0.14.tar --platform=linux/amd64
[root@k8s-node02 ~]# ctr -n k8s.io images import redis.latest.tar --platform=linux/amd64
[root@k8s-node01 ~]# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/library/redis 7.0.14 d9c89935bd08c 134MB
docker.io/library/redis latest 590b81f2fea1a 120MB
(2)服务的创建与删除
# 查看service的文件结构
# explain读取说明
[root@k8s-master pods]# kubectl explain service
[root@k8s-master pods]# kubectl explain service.apiVersion
[root@k8s-master pods]# kubectl explain service.kind
[root@k8s-master pods]# kubectl explain service.metadata
[root@k8s-master pods]# kubectl explain service.spec
# 创建了一个无头服务
[root@k8s-master pods]# vim test0027.yaml
apiVersion: v1
# 定义的是statefulSet的headless service(无头服务)
kind: Service
metadata:
# 定义redis的headless service
name: redis
labels:
app: redis-db
spec:
ports:
- name: redisport
port: 27017
clusterIP: None
selector:
app: redis-db
[root@k8s-master pods]# kubectl create -f test0027.yaml
service/redis created
[root@k8s-master pods]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d16h
redis ClusterIP None <none> 27017/TCP 52s
# 删除服务
[root@k8s-master pods]# kubectl delete -f test0027.yaml
service "redis" deleted
[root@k8s-master pods]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d16h
(3)StatefulSet的创建
# 查看statefulset的文件结构
[root@k8s-master pods]# kubectl explain statefulset
[root@k8s-master pods]# kubectl explain statefulset.apiVersion
[root@k8s-master pods]# kubectl explain statefulset.kind
[root@k8s-master pods]# kubectl explain statefulset.metadata
[root@k8s-master pods]# kubectl explain statefulset.spec
# 在服务后面加下面内容
[root@k8s-master pods]# vim test0027.yaml
---
apiVersion: apps/v1
# 资源类型
kind: StatefulSet
metadata:
name: redis-db
namespace: default
labels:
# 必须匹配 spec.template.metadata.labels
app: redis
spec:
selector:
matchLabels:
app: redis
replicas: 3
serviceName: "redis"
# 用于指定在进⾏滚动更新或⾸次创建 Pod 时,Pod 实例需要等待准备就绪的最⼩时间(以秒为单位)默认0
minReadySeconds: 10
template:
metadata:
labels:
# 必须匹配 spec.selector.matchLabels
app: redis
spec:
# pod接到终⽌信号后,多等待10秒
terminationGracePeriodSeconds: 10
containers:
- name: redis
image: docker.io/library/redis:latest
imagePullPolicy: Never
ports:
- name: redisport
containerPort: 27017
[root@k8s-master pods]# kubectl create -f test0027.yaml
service/redis created
statefulset.apps/redis-db created
# statefulset控制的pod,数量为定义好的3,并且与deployment不同的是,statefulset是按照序号顺序标识并控制pod启停的
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 0 7m17s
redis-db-0 1/1 Running 0 53s
redis-db-1 1/1 Running 0 33s
redis-db-2 1/1 Running 0 13s
(4)查看 StatefulSet
# 查看 kube-system 命名空间内的服务
[root@k8s-master pods]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 7d17h
metrics-server ClusterIP 10.96.195.245 <none> 443/TCP 7d
# 查看默认命名空间内的服务
[root@k8s-master pods]# kubectl get svc -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d17h
redis ClusterIP None <none> 27017/TCP 16m
# 查看默认命名空间下的statefulSet控制器
[root@k8s-master pods]# kubectl get sts
NAME READY AGE
redis-db 3/3 16m
[root@k8s-master pods]# kubectl get statefulsets.apps
NAME READY AGE
redis-db 3/3 17m
(5)删除StatefulSet应用创建出的pod(两种方式:级联与非级联)
# 普通方式删除不了StatefulSet应用创建出的pod
[root@k8s-master pods]# kubectl delete pod redis-db-0
pod "redis-db-0" deleted
# 重新创建一个pod顶替删除的pod
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 0 25m
redis-db-0 1/1 Running 0 7s
redis-db-1 1/1 Running 0 18m
redis-db-2 1/1 Running 0 18m
# 直接删除statefulset应用
[root@k8s-master pods]# kubectl delete statefulsets.apps redis-db
statefulset.apps "redis-db" deleted
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 0 26m
# 再次使用yaml文件创建statefulset应用,会发生报错
# 由于service已存在
[root@k8s-master pods]# kubectl create -f test0027.yaml
statefulset.apps/redis-db created
Error from server (AlreadyExists): error when creating "test0027.yaml": services "redis" already exists
# 先删除service(一般不这样)
[root@k8s-master pods]# kubectl delete svc redis
service "redis" deleted
[root@k8s-master pods]# kubectl create -f test0027.yaml
service/redis created
Error from server (AlreadyExists): error when creating "test0027.yaml": statefulsets.apps "redis-db" already exists
# 直接使用yaml文件删除所有相关文件
[root@k8s-master pods]# kubectl delete -f test0027.yaml
service "redis" deleted
statefulset.apps "redis-db" deleted
# 创建StatefulSet应用
[root@k8s-master pods]# kubectl create -f test0027.yaml
service/redis created
statefulset.apps/redis-db created
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 0 29m
redis-db-0 1/1 Running 0 56s
redis-db-1 1/1 Running 0 36s
redis-db-2 1/1 Running 0 16s
# 非级联删除pod
# 先将其service与statefulset.apps删除,但此时pod还存在并运行
[root@k8s-master pods]# kubectl delete -f test0027.yaml --cascade=false
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
service "redis" deleted
statefulset.apps "redis-db" deleted
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 0 30m
redis-db-0 1/1 Running 0 2m51s
redis-db-1 1/1 Running 0 2m31s
redis-db-2 1/1 Running 0 2m11s
[root@k8s-master pods]# kubectl get statefulsets.apps
No resources found in default namespace.
[root@k8s-master pods]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d17h
# 然后可以一个一个删除pod
[root@k8s-master pods]# for i in 0 1 2;do kubectl delete pod redis-db-$i;done
pod "redis-db-0" deleted
pod "redis-db-1" deleted
pod "redis-db-2" deleted
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 0 33m
(6)StatefulSet 创建 Pod 流程
1、对于具有 N 个副本的 StatefulSet,将按顺序从 0 到 N-1 开始创建 Pod。
2、当删除 Pod 时,将按照 N-1 到 0 的逆序终止并删 除。
3、在缩容 Pod 之前,必须保证当前的 Pod 是 Running 或 Ready 状态。
4、在终止 Pod 之前,它所有的继任者必须是完全关闭状 态。
5、如果前一个 Pod 状态不处于 Running 或 Ready,那 么下一任 Pod 不会被创建,并且即使在特殊情况下第 二个 Pod 创建成功,而第三个 Pod 也不会被创建。
8)修改更新StatefulSet(升级策略)
(1)创建pod
# 创建pod
[root@k8s-master pods]# kubectl create -f test0027.yaml
service/redis created
statefulset.apps/redis-db created
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 3 (19m ago) 3h19m
redis-db-0 1/1 Running 0 68s
redis-db-1 1/1 Running 0 48s
redis-db-2 1/1 Running 0 28s
(2)镜像修改
# 滚动更新,从序号最大的pod开始更新
# 镜像修改
[root@k8s-master pods]# kubectl set image sts redis-db redis=docker.io/library/redis:7.0.14
statefulset.apps/redis-db image updated
[root@k8s-master pods]# kubectl describe pod redis-db-1 | grep Image:
Image: docker.io/library/redis:7.0.14
[root@k8s-master pods]# for i in 0 1 2;do kubectl describe pod redis-db-$i | grep Image:;done
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:7.0.14
(3)扩容缩容方法1
# 扩容
[root@k8s-master pods]# kubectl scale statefulset redis-db --replicas 5
statefulset.apps/redis-db scaled
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 3 (30m ago) 3h30m
redis-db-0 1/1 Running 0 3m2s
redis-db-1 1/1 Running 0 3m23s
redis-db-2 1/1 Running 0 3m39s
redis-db-3 1/1 Running 0 78s
redis-db-4 1/1 Running 0 58s
# 缩容(从大到小删除)
[root@k8s-master pods]# kubectl scale statefulset redis-db --replicas 2
statefulset.apps/redis-db scaled
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 3 (32m ago) 3h32m
redis-db-0 1/1 Running 0 4m57s
redis-db-1 1/1 Running 0 5m18s
(4)扩容缩容方法2(观察扩容缩容过程)
[root@k8s-master pods]# kubectl get po -w # -w表示在当前终端查看过程
# 新开一终端执行命令
[root@k8s-master pods]# kubectl patch statefulsets.apps redis-db -p '{"spec":{"replicas":10}}'
statefulset.apps/redis-db patched
# 观察上一终端的创建过程
# ctrl+c退出观察
[root@k8s-master pods]# kubectl get po -w
[root@k8s-master pods]# kubectl patch statefulsets.apps redis-db -p '{"spec":{"replicas":3}}' statefulset.apps/redis-db patched
(5)分段更新
[root@k8s-master pods]# kubectl delete -f test0027.yaml
service "redis" deleted
statefulset.apps "redis-db" deleted
[root@k8s-master pods]# vim test0027.yaml
# 增加修改此部分
updateStrategy: # 更新策略模块
partition: 3 # 分段更新参数,序号大于或等于3的副本
type: RollingUpdate # 更新策略类型
[root@k8s-master pods]# kubectl delete -f test0027.yaml
service "redis" deleted
statefulset.apps "redis-db" deleted
[root@k8s-master pods]# kubectl create -f test0027.yaml
service/redis created
statefulset.apps/redis-db created
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 4 (13m ago) 4h13m
redis-db-0 1/1 Running 0 7m52s
redis-db-1 1/1 Running 0 7m37s
redis-db-2 1/1 Running 0 7m17s
redis-db-3 1/1 Running 0 6m57s
redis-db-4 1/1 Running 0 6m37s
[root@k8s-master pods]# for i in 0 1 2 3 4;do kubectl describe pod redis-db-1 | grep Image:;done
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:latest
[root@k8s-master pods]# kubectl set image sts redis-db redis=docker.io/library/redis:7.0.14
statefulset.apps/redis-db image updated
[root@k8s-master pods]# for i in 0 1 2 3 4;do kubectl describe pod redis-db-$i | grep Image:;done
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:7.0.14
3、守护进程集 DaemonSet
1)DaemonSet 概念
DaemonSet 是 Kubernetes 中的一个重要概念,它是一 种特殊的控制器,用于在集群中的每个节点上运行一个 Pod 副本。
DaemonSet 可以确保在集群的每个节点上都有相关的 Pod 运行,这对于一些需要每个节点都运行的应用非常 有用,例如日志收集、资源监控等。
当有新节点加入集群时,DaemonSet 会自动为新节点创 建一个 Pod,而当某个节点从集群中移除时, DaemonSet 会自动回收该节点上的 Pod。
此外, DaemonSet 还可以通过节点选择器和标签来指定仅在部 分具有特定特征的节点上运行指定的 Pod。
2)定义创建 DaemonSet
[root@k8s-master pods]# vim test0028.yaml
apiVersion: apps/v1
# 资源类型:DaemonSet
kind: DaemonSet
# 元信息
metadate:
# 该DaemonSet资源名
name: nginx
spec:
# 标签选择器
selector:
# 匹配标签
matchLabels:
# 与上面的标签相同
app: nginx
# 定义pod
template:
metadata:
labels:
# 定义的标签,与上面标签相同
app: nginx
spec:
# 定义pod的配置
containers:
# 容器名
- name: nginx
# 使用的镜像
image: docker.io/library/nginx:1.25.0
imagePullPolicy: Never
ports:
- name: nginxport
containerPort: 80
[root@k8s-master pods]# kubectl delete -f test0027.yaml
service "redis" deleted
statefulset.apps "redis-db" deleted
[root@k8s-master pods]# kubectl create -f test0028.yaml
daemonset.apps/nginx created
# 通过使用DaemonSet,使每个node节点都建立了一个pod,创建了两个pod节点
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 6 (4m14s ago) 5h15m
nginx-hh542 1/1 Running 0 7s
nginx-pbx4w 1/1 Running 0 7s
3)通过标签指定节点部署
[root@k8s-master pods]# vim test0028.yaml
# 增加下面内容
# nodeSelector: 添加node标签选择器
# abc: "1" 选择自定义的标签或node自带标签
# 查看node节点携带的标签
[root@k8s-master pods]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready control-plane 7d22h v1.28.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01 Ready <none> 7d7h v1.28.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
k8s-node02 Ready <none> 7d22h v1.28.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux
# 为node1节点设置指定的标签
[root@k8s-master pods]# kubectl label nodes k8s-node01 abc=1
node/k8s-node01 labeled
# 查看node节点标签
[root@k8s-master pods]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready control-plane 7d22h v1.28.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01 Ready <none> 7d7h v1.28.2 abc=1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
k8s-node02 Ready <none> 7d22h v1.28.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux
# 删除pod节点指定yaml文件重新创建pod节点
[root@k8s-master pods]# kubectl delete -f test0028.yaml
daemonset.apps "nginx" deleted
[root@k8s-master pods]# kubectl create -f test0028.yaml
daemonset.apps/nginx created
# 查看pod节点,发现只创建了一个pod节点
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 6 (9m11s ago) 5h20m
nginx-fnxnz 1/1 Running 0 7s
# 查看pod节点的ip地址与所在node节点,发现确实只创建到了node01上
[root@k8s-master pods]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cluster-test-66bb44bd88-cqkx5 1/1 Running 6 (9m54s ago) 5h20m 172.16.85.242 k8s-node01 <none> <none>
nginx-fnxnz 1/1 Running 0 50s 172.16.85.245 k8s-node01 <none> <none>
4)更新和回滚 DaemonSet
如果添加了新节点或修改了节点标签 (Label), DaemonSet 将立刻向新匹配上的节点添加 Pod 同时删 除不能匹配的节点上的 Pod。
DaemonSet 滚动更新可参考:DaemonSet | Kubernetes
DaemonSet 更新策略和 StatefulSet 类似,也有 OnDelete 和 RollingUpdate 两种方式。(若需要更新请 查看 Deployment 更新方式,将资源类型和名称替换即 可)
4、自动扩缩容 HPA
1)HPA 概念
Horizontal Pod Autoscaler(HPA)是Kubernetes中的 一个功能,可以根据观察到的CPU 利用率或其他应用程 序提供的指标自动伸缩副本数量。
HPA 监视指定的资源(通常是 Deployment )。
当负载增加时,HPA 可自动增加 Pod 副本数量以满足 需求。
当负载减小时,HPA 可自动缩减 Pod 副本数量以节省 资源。
HPA 接口类型:
HPAv1 为稳定版自动水平伸缩,只支持CPU指标
V2 为 beta 版本,分为 v2beta1 (支持CPU、内存和自 定义指标)
v2beta2 (支持CPU、内存、自定义指标 Custom 和额 外指标 ExternalMetrics)
[root@k8s-master pods]# kubectl get apiservices | grep autosca
v1.autoscaling Local True 8d
v2.autoscaling Local True 8d
[root@k8s-master pods]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master 140m 7% 1121Mi 65%
k8s-node01 52m 2% 905Mi 52%
k8s-node02 56m 2% 911Mi 53%
2)HPA 实战
注: 必须安装 metrics-server 或其他自定义 metrics-server
必须配置 requests 参数
不能扩容无法缩放的对象,比如 DaemonSet
(1)创建一个deployment设置hpa的属性
[root@k8s-master pods]# kubectl create deployment test0029 --image=docker.io/library/nginx:1.25.0 --replicas=1 -o=yaml --dry-run=client > test0029.yaml
[root@k8s-master pods]# vim test0029.yaml
# 查看并修改yaml文件
# requests: 配置requests参数
# cpu: 10m 指定cpu指标为10m
[root@k8s-master pods]# kubectl create -f test0029.yaml
deployment.apps/test0029 created
[root@k8s-master pods]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-cqkx5 1/1 Running 6 (29m ago) 5h40m
nginx-fnxnz 1/1 Running 0 19m
test0029-844d5495cf-rl5kk 1/1 Running 0 11s
[root@k8s-master pods]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cluster-test-66bb44bd88-cqkx5 1/1 Running 6 (30m ago) 5h41m 172.16.85.242 k8s-node01 <none> <none>
test0029-844d5495cf-rl5kk 1/1 Running 0 118s 172.16.58.198 k8s-node02 <none> <none>
[root@k8s-master pods]# curl 172.16.58.198
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
(2)映射端口
[root@k8s-master pods]# kubectl expose deployment test0029 --port=80
service/test0029 exposed
[root@k8s-master pods]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d23h
test0029 ClusterIP 10.96.56.90 <none> 80/TCP 31s
[root@k8s-master pods]# curl 10.96.56.90
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
(3)设置自动扩容参数
[root@k8s-master pods]# kubectl autoscale deployment test0029 --cpu-percent=10 --max=10 --min=1
horizontalpodautoscaler.autoscaling/test0029 autoscaled
# 新开一终端,输入下面指令,死循环访问服务ip地址
[root@k8s-master ~]# while true;do wget -q -O- http://10.96.56.90 > /dev/null;done
# 查看pod数量,发现可以自动创建pod直到达到最大标准数10
[root@k8s-master pods]# kubectl get pod | wc -l
12
# 另一终端停止访问
[root@k8s-master ~]# while true;do wget -q -O- http://10.96.56.90 > /dev/null;done
^C
# 等一段时间后,发现可以自动删除pod直到达到最小标准数1
[root@k8s-master pods]# kubectl get pod | wc -l
3
标签:kubectl,kubernetes,pods,redis,调度,master,k8s,root
From: https://blog.csdn.net/m0_73907608/article/details/142586682