当前位置: 首页 > news >正文

k8s之控制器详解

1.deployment:适用于无状态服务

1.功能

        (1)创建高可用pod

      (2)滚动升级/回滚

      (3)平滑扩容和缩容

2.操作命令

(1)回滚

# 回滚到上一个版本
kubectl rollout undo deployment/my-app# 回滚到特定版本(先查看历史)
kubectl rollout history deployment/my-app
kubectl rollout undo deployment/my-app --to-revision=2

(2)平滑扩容和缩容

        手动扩缩容

# 扩容到 5 个副本
kubectl scale deployment/my-app --replicas=5# 缩容到 2 个副本
kubectl scale deployment/my-app --replicas=2

        自动扩缩容(HPA)

# 创建 HPA(CPU 使用率超过 50% 时扩容,最多 10 个 Pod)
kubectl autoscale deployment/my-app --min=2 --max=10 --cpu-percent=50

3.滚动升级/回滚图解(先创建出新的,然后用新的逐步替换出旧的)

4.滚动升级/回滚示例

apiVersion: apps/v1
kind: Deployment
metadata:name: my-httpd          # Deployment 名称labels:app: httpd            # 标签(用于 Service 选择器匹配)
spec:replicas: 3             # Pod 副本数(高可用)revisionHistoryLimit: 5 # 保留的历史版本数(用于回滚)strategy:type: RollingUpdate   # 滚动升级策略rollingUpdate:maxSurge: 1         # 升级时最多临时超出的 Pod 数量maxUnavailable: 0   # 升级时允许不可用的 Pod 数量(0 表示全量可用)selector:matchLabels:app: httpd          # 匹配 Pod 的标签(必须与 template 一致)template:metadata:labels:app: httpd        # Pod 标签(Service 通过此选择器关联)spec:affinity:          # 反亲和性:将 Pod 分散到不同节点podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchLabels:app: httpdtopologyKey: kubernetes.io/hostnamecontainers:- name: httpdimage: httpd:alpine   # 容器镜像ports:- containerPort: 80   # 容器监听的端口(targetPort)resources:requests:cpu: "100m"       # 最小资源请求(HPA 自动扩缩容依据)memory: "128Mi"limits:cpu: "200m"       # 资源上限memory: "256Mi"livenessProbe:        # 健康检查httpGet:path: /port: 80initialDelaySeconds: 5periodSeconds: 10

5.hpa扩缩容示例


五.HPA弹性控制器:监控控制器,实现集群的自动扩缩容设置控制器:---kind: DeploymentapiVersion: apps/v1metadata:name: mydeployspec:replicas: 1selector:matchLabels:app: deploy-httpdtemplate:metadata:labels:app: deploy-httpdspec:containers:- name: webimage: myos:httpdresources:           # 为该资源设置配额requests:          # HPA 控制器会根据配额使用情况伸缩集群cpu: 300m        # CPU 配额---kind: Service   #负载均衡apiVersion: v1metadata:name: websvcspec:type: ClusterIPclusterIP: 10.245.1.80selector:app: deploy-httpdports:- protocol: TCPport: 80targetPort: 80HPA 控制器:---kind: HorizontalPodAutoscalerapiVersion: autoscaling/v2metadata:name: myhpaspec:behavior:                # 窗口稳定期,这个期间的资源变化scaleDown:stabilizationWindowSeconds: 60scaleTargetRef:          # 指定控制器kind: DeploymentapiVersion: apps/v1name: mydeployminReplicas: 1           # 副本数量      maxReplicas: 3metrics:                 # 扩缩容设置     - type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: 50 #百分比,节点超过设置的cpu资源的百分之50,则扩容节点,平均资源小于值,缩容

2.statefulset:有状态的服务,(redis,mysql等)

        通过headless无头服务,暴露每个pod的唯一dns地址

        访问方式 :redis-0.redis-service.default.svc.cluster.local(0个-服务名.----)

(1) Headless Service 配置# service-headless.yaml
apiVersion: v1
kind: Service
metadata:name: redis-service  # 名称需与 StatefulSet 的 serviceName 一致
spec:clusterIP: None     # Headless Service 的关键配置selector:app: redis        # 匹配 StatefulSet 的 Pod 标签ports:- protocol: TCPport: 6379        # Service 端口,集群内部的访问端口,nodeport集群外部的访问端口targetPort: 6379   # Pod 端口,用于service,后端服务的端口(2) StatefulSet 配置
# statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: redis
spec:serviceName: "redis-service"  # 必须与 Headless Service 名称一致replicas: 3selector:matchLabels:app: redistemplate:metadata:labels:app: redisspec:containers:- name: redisimage: redis:alpineports:- containerPort: 6379 容器里面的端口volumeClaimTemplates:  # 每个 Pod 独立存储- metadata:name: dataspec:storageClassName: "ssd"resources:requests:storage: 10Gi

3.daemonset:守护进程,保证每个node上都运行一个容器(Prometheus日志,elk监控等)

# DaemonSet 示例:Fluentd 日志收集
apiVersion: apps/v1
kind: DaemonSet
metadata:name: fluentd
spec:selector:matchLabels:name: fluentdtemplate:metadata:labels:name: fluentdspec:containers:- name: fluentdimage: fluent/fluentdvolumeMounts:- name: varlogmountPath: /var/logvolumes:- name: varloghostPath:path: /var/log

4.job和cronjob:定时任务的pod,数据备份,定时器清理等(执行完后pod销毁)

5.HPA弹性控制器:自动扩缩容

# nginx-deployment.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploy  # 此名称必须与HPA中的scaleTargetRef.name一致
spec:replicas: 3template:spec:containers:- name: nginximage: nginxresources:requests:cpu: "100m"# HPAapiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:name: nginx-hpa
spec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginx-deploy  # 必须与集群中Deployment名称一致minReplicas: 2maxReplicas: 10metrics:- type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: 50  # CPU使用率目标50%behavior:  # 扩缩容行为控制(Kubernetes 1.18+)scaleDown:  # 缩容配置stabilizationWindowSeconds: 300  # 缩容冷却时间5分钟(默认300秒)policies:- type: Percent  # 按百分比缩容value: 10      # 每次最多缩容10%的PodperiodSeconds: 60  # 每60秒评估一次- type: Pods     # 按固定数量缩容(与Percent二选一)value: 1       # 每次最多缩容1个PodscaleUp:  # 扩容配置stabilizationWindowSeconds: 60  # 扩容冷却时间1分钟(默认0秒)policies:- type: Percentvalue: 100     # 允许瞬间扩容100%的Pod(紧急情况下)periodSeconds: 15- type: Podsvalue: 4       # 每次最多扩容4个Pod

http://www.lryc.cn/news/601078.html

相关文章:

  • 基于springboot的图书借阅系统
  • mysql-数据表-DDL语句
  • Python爬虫实战:诗词名句网《三国演义》全集
  • Redis C++客户端——通用命令
  • 相机标定相关原理
  • FitCoach AI:基于React+CloudBase的智能健身教练应用开发全解析
  • Ubuntu系统 系统盘和数据盘扩容具体操作
  • S7-200 SMART 数字量 I/O 组态指南:从参数设置到实战案例
  • 6G通感算
  • AI使能的SVD算子:基于深度学习的矩阵分解方法
  • 【计算机组成原理】第一章:计算机系统概述
  • python---元组解包(Tuple Unpacking)
  • Linux内核设计与实现 - 课程大纲
  • 通过redis_exporter监控redis cluster
  • 学习嵌入式的第三十二天-数据结构-(2025.7.24)IO多路复用
  • 数组内存学习
  • 英语听力口语词汇-8.美食类
  • VisionPro系列讲解 - 03 Simulator 模拟器使用
  • 20250726-4-Kubernetes 网络-Service DNS名称解析_笔记
  • MGER实验
  • selenium自动化鼠标和键盘操作
  • 幸福网咖订座点餐小程序的设计与实现
  • Compose笔记(三十八)--CompositionLocal
  • VS Code + LaTeX 绘制电气图完全指南(含 PlantUML 样式参考)
  • 酒店智能门锁SDK新V门锁系统接口函数[2025版]Delphi 7.0——东方仙盟硬件接口库
  • 方正小标宋简3.0,可编辑
  • Python - 100天从新手到大师 - Day6
  • 【科研绘图系列】R语言绘制误差连线散点图
  • freeRTOS 静态创建任务
  • FastAPI入门:安装、Pydantic、并发和并行