1. 首页 > Kubernetes教程 > 正文

Kubernetes教程FG053-Kubernetes Pod Priority and Preemption实战

本文档风哥主要介绍Kubernetes Pod Priority and Preemption实战,包括Pod Priority概述、Preemption概述、使用场景、优先级规划、抢占规划、最佳实践规划、Pod Priority实现、Preemption实现、管理实现、Pod Priority案例、Preemption案例、集成案例等内容,风哥教程参考Kubernetes官方文档和调度相关文档,适合想使用和理解Kubernetes调度管理的开发人员和运维人员。

Part01-基础概念与理论知识

1.1 Pod Priority概述

Pod Priority是Kubernetes中用于为Pod设置优先级的机制,它可以影响Pod的调度顺序和抢占行为。

Pod Priority的主要特性包括:

  • 优先级设置:为Pod设置优先级值,值越高,优先级越高
  • 调度顺序:高优先级的Pod会先被调度
  • 抢占行为:高优先级的Pod可以抢占低优先级的Pod的资源
  • PriorityClass:使用PriorityClass定义优先级类别

1.2 Preemption概述

Preemption是Kubernetes中用于高优先级Pod抢占低优先级Pod资源的机制,它可以确保高优先级的Pod能够获得所需的资源。

Preemption的主要特性包括:

  • 资源抢占:高优先级的Pod可以抢占低优先级的Pod的资源
  • 优雅终止:被抢占的Pod会被优雅终止
  • 调度决策:调度器会选择最合适的低优先级Pod进行抢占
  • 抢占限制:可以通过配置限制抢占行为

1.3 使用场景

Pod Priority and Preemption的使用场景包括:

  • 关键应用:确保关键应用的Pod能够优先获得资源
  • 资源紧张:在资源紧张的情况下,确保重要的Pod能够运行
  • 批量处理:为批量处理任务设置低优先级,确保在线服务的资源需求
  • 弹性伸缩:在弹性伸缩场景中,确保新的Pod能够获得资源
  • 服务质量:根据服务质量要求设置不同的优先级

Part02-生产环境规划与建议

2.1 优先级规划

Kubernetes Pod Priority的规划:

# 优先级规划
– 目标:
– 实现Pod的优先级管理
– 确保关键应用的资源需求
– 优化资源使用
– 提高服务质量
– 范围:
– 优先级类别定义
– 优先级值设置
– 应用优先级分配
– 测试和验证
– 工具选择:
– kubectl:用于管理PriorityClass和Pod
– Kubernetes Dashboard:用于可视化管理
– Prometheus:用于监控
– Grafana:用于可视化监控数据
– 流程设计:
– 优先级类别定义:定义不同的优先级类别
– 优先级值设置:为不同的优先级类别设置合适的值
– 应用优先级分配:为不同的应用分配合适的优先级
– 测试和验证:测试优先级的效果,确保其正常工作
– 监控和调整:监控Pod调度情况,根据需要调整优先级设置
– 资源分配:
– 人力资源:集群管理员、运维人员
– 时间资源:规划时间、部署时间、测试时间
– 基础设施:计算资源、存储资源、网络资源

2.2 抢占规划

Kubernetes Preemption的规划:

# 抢占规划
– 目标:
– 实现Pod的抢占管理
– 确保高优先级Pod能够获得资源
– 最小化抢占对系统的影响
– 提高资源利用率
– 范围:
– 抢占策略设计
– 抢占限制设置
– 抢占行为监控
– 测试和验证
– 工具选择:
– kubectl:用于管理Pod和查看抢占事件
– Kubernetes Dashboard:用于可视化管理
– Prometheus:用于监控
– Grafana:用于可视化监控数据
– 流程设计:
– 抢占策略设计:设计合适的抢占策略
– 抢占限制设置:设置合理的抢占限制
– 抢占行为监控:监控抢占行为,及时发现问题
– 测试和验证:测试抢占的效果,确保其正常工作
– 调整和优化:根据监控结果调整和优化抢占策略
– 资源分配:
– 人力资源:集群管理员、运维人员
– 时间资源:规划时间、部署时间、测试时间
– 基础设施:计算资源、存储资源、网络资源

2.3 最佳实践规划

Kubernetes Pod Priority and Preemption的最佳实践规划:

# 最佳实践规划
– 优先级最佳实践:
– 合理设置优先级值:根据应用的重要性设置合适的优先级值
– 使用PriorityClass:使用PriorityClass定义优先级类别,便于管理
– 避免过度使用高优先级:避免为太多应用设置高优先级,导致抢占过于频繁
– 定期审查优先级设置:定期审查和更新优先级设置,确保其符合应用需求
– 测试优先级效果:在生产环境中使用前,在测试环境中测试优先级效果
– 抢占最佳实践:
– 合理配置抢占策略:根据集群规模和应用需求配置合适的抢占策略
– 监控抢占行为:监控抢占行为,及时发现和处理问题
– 避免不必要的抢占:避免为非关键应用设置过高的优先级,导致不必要的抢占
– 考虑Pod生命周期:考虑Pod的生命周期,避免频繁的抢占和重新调度
– 测试抢占效果:在生产环境中使用前,在测试环境中测试抢占效果
– 部署最佳实践:
– 使用Deployment:使用Deployment管理Pod,确保Pod的高可用性
– 使用StatefulSet:对于有状态应用,使用StatefulSet管理Pod
– 配置资源请求和限制:配置Pod的资源请求和限制,确保资源使用合理
– 监控Pod状态:监控Pod的状态,及时发现和处理问题
– 运维最佳实践:
– 文档化优先级和抢占配置:文档化所有的优先级和抢占配置,便于维护和审计
– 培训和教育:对开发人员和运维人员进行培训,提高调度管理意识
– 定期审查配置:定期审查和更新优先级和抢占配置,确保其符合应用需求
– 持续改进:持续改进调度策略,提高调度效率和资源利用率

Part03-生产环境项目实施方案

3.1 Pod Priority实现

Pod Priority实现的具体步骤:

# Pod Priority实现
1. 创建PriorityClass:
# 创建PriorityClass
$ cat > priority-classes.yaml << 'EOF' apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority value: 1000000 globalDefault: false description: "High priority class for critical applications" --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: medium-priority value: 500000 globalDefault: false description: "Medium priority class for important applications" --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: low-priority value: 100000 globalDefault: false description: "Low priority class for non-critical applications" EOF $ kubectl apply -f priority-classes.yaml # 查看PriorityClass $ kubectl get priorityclasses # 输出 NAME VALUE GLOBAL-DEFAULT AGE high-priority 1000000 false 1m medium-priority 500000 false 1m low-priority 100000 false 1m 2. 创建带有优先级的Pod: # 创建高优先级Pod $ cat > high-priority-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: high-priority-pod spec: containers: - name: high-priority-container image: nginx:latest resources: requests: cpu: 1 memory: 1Gi priorityClassName: high-priority EOF $ kubectl apply -f high-priority-pod.yaml # 创建中优先级Pod $ cat > medium-priority-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: medium-priority-pod spec: containers: - name: medium-priority-container image: nginx:latest resources: requests:,风哥提示:。 cpu: 1 memory: 1Gi priorityClassName: medium-priority EOF $ kubectl apply -f medium-priority-pod.yaml # 创建低优先级Pod $ cat > low-priority-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: low-priority-pod spec: containers: - name: low-priority-container image: nginx:latest resources: requests: cpu: 1 memory: 1Gi priorityClassName: low-priority EOF $ kubectl apply -f low-priority-pod.yaml 3. 查看Pod优先级: # 查看Pod $ kubectl get pods -o wide # 输出 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES high-priority-pod 1/1 Running 0 1m 10.244.0.2 node1
medium-priority-pod 1/1 Running 0 1m 10.244.1.2 node2
low-priority-pod 1/1 Running 0 1m 10.244.2.2 node3
# 查看Pod优先级详情
$ kubectl get pod high-priority-pod -o jsonpath='{.spec.priorityClassName}’
# 输出
high-priority
4. 清理资源:
# 清理Pod
$ kubectl delete pod high-priority-pod medium-priority-pod low-priority-pod
# 清理PriorityClass
$ kubectl delete priorityclass high-priority medium-priority low-priority

3.2 Preemption实现

Preemption实现的具体步骤:

# Preemption实现
1. 创建PriorityClass:
# 创建PriorityClass
$ cat > priority-classes.yaml << 'EOF' apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority value: 1000000 globalDefault: false description: "High priority class for critical applications" --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: low-priority value: 100000 globalDefault: false description: "Low priority class for non-critical applications" EOF $ kubectl apply -f priority-classes.yaml 2. 创建低优先级Pod: # 创建低优先级Pod $ cat > low-priority-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: low-priority-pod spec: containers: - name: low-priority-container image: nginx:latest resources: requests: cpu: 2 memory: 2Gi priorityClassName: low-priority EOF $ kubectl apply -f low-priority-pod.yaml # 查看Pod状态 $ kubectl get pods -o wide # 输出 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES low-priority-pod 1/1 Running 0 1m 10.244.0.2 node1
3. 创建高优先级Pod,触发抢占:
# 创建高优先级Pod
$ cat > high-priority-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: high-priority-pod spec: containers: - name: high-priority-container image: nginx:latest resources: requests: cpu: 2 memory: 2Gi priorityClassName: high-priority EOF $ kubectl apply -f high-priority-pod.yaml 4. 查看抢占情况: # 查看Pod状态 $ kubectl get pods -o wide # 输出 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES high-priority-pod 1/1 Running 0 1m 10.244.0.3 node1
low-priority-pod 0/1 Terminating 0 2m 10.244.0.2 node1
# 查看Pod事件
$ kubectl describe pod high-priority-pod
# 输出
Events:
Type Reason Age From Message
—- —— —- —- ——-
Normal Scheduled 1m default-scheduler Successfully assigned default/high-priority-pod to node1
Normal Preempted 1m default-scheduler Preempted pod: default/low-priority-pod
Normal Pulling 1m kubelet, node1 Pulling image “nginx:latest”
Normal Pulled 1m kubelet, node1 Successfully pulled image “nginx:latest”
Normal Created 1m kubelet, node1 Created container high-priority-container
Normal Started 1m kubelet, node1 Started container high-priority-container
5. 清理资源:
# 清理Pod
$ kubectl delete pod high-priority-pod
# 清理PriorityClass
$ kubectl delete priorityclass high-priority low-priority

3.3 管理实现

,学习交流加群风哥微信: itpux-com。

Pod Priority and Preemption管理的具体步骤。,风哥提示:。

# 管理实现
1. 监控Pod优先级和抢占情况:
# 安装Prometheus和Grafana
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update
$ helm install prometheus prometheus-community/kube-prometheus-stack
# 查看监控面板
$ kubectl port-forward deployment/prometheus-grafana 3000:3000
# 打开浏览器访问 http://localhost:3000
2. 配置调度告警:
# 创建告警规则
$ cat > scheduling-alert.yaml << 'EOF' apiVersion: monitoring.coreos.com/v1 kind: PrometheusRule metadata: name: scheduling-alerts spec: groups: - name: pod-scheduling rules: - alert: PodPending expr: sum by (namespace, pod) (kube_pod_status_phase{phase="Pending"}) > 0
for: 5m
labels:
severity: critical
annotations:
summary: “Pod pending”
description: “Pod {{ $labels.pod }} in namespace {{ $labels.namespace }} has been pending for more than 5 minutes”
– alert: PodPreempted
expr: sum by (namespace, pod) (kube_pod_status_reason{reason=”Preempted”}) > 0
for: 1m
labels:
severity: warning
annotations:
summary: “Pod preempted”
description: “Pod {{ $labels.pod }} in namespace {{ $labels.namespace }} has been preempted”
EOF
$ kubectl apply -f scheduling-alert.yaml
3. 管理PriorityClass:
# 列出PriorityClass
$ kubectl get priorityclasses
# 查看PriorityClass详情
$ kubectl describe priorityclass high-priority
# 更新PriorityClass
$ kubectl patch priorityclass high-priority -p ‘{“value”: 1500000}’
# 删除PriorityClass
$ kubectl delete priorityclass high-priority
4. 管理Pod优先级:
# 查看Pod的优先级
$ kubectl get pod high-priority-pod -o jsonpath='{.spec.priorityClassName}’
# 更新Pod的优先级
$ kubectl patch pod high-priority-pod -p ‘{“spec”:{“priorityClassName”:”medium-priority”}}’
5. 配置调度策略:
# 查看调度器配置
$ kubectl get configmap kube-scheduler -n kube-system -o yaml
# 更新调度器配置
$ kubectl edit configmap kube-scheduler -n kube-system
6. 测试调度策略:
# 创建测试Pod
$ cat > test-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test-container image: nginx:latest resources: requests: cpu: 1 memory: 1Gi priorityClassName: high-priority EOF $ kubectl apply -f test-pod.yaml # 查看Pod调度情况 $ kubectl get pods -o wide 7. 清理资源: # 清理Pod $ kubectl delete pod test-pod # 清理告警规则 $ kubectl delete prometheusrule scheduling-alerts # 卸载Prometheus和Grafana $ helm uninstall prometheus

Part04-生产案例与实战讲解

4.1 Pod Priority案例

Pod Priority的实战案例。

# 案例:使用Pod Priority确保关键应用的资源需求
# 场景:在Kubernetes集群中,使用Pod Priority确保关键应用能够优先获得资源
# 问题:
– 集群中有多种类型的应用,包括关键应用和非关键应用
– 资源有限,需要确保关键应用能够获得足够的资源
– 非关键应用不应该影响关键应用的运行
# 解决方案:
1. 创建PriorityClass:
# 创建PriorityClass
$ cat > priority-classes.yaml << 'EOF' apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: critical-priority value: 1000000 globalDefault: false description: "Critical priority class for critical applications" --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: normal-priority value: 500000 globalDefault: false description: "Normal priority class for normal applications" --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: batch-priority value: 100000 globalDefault: false description: "Batch priority class for batch jobs" EOF $ kubectl apply -f priority-classes.yaml 2. 部署关键应用: # 创建关键应用Deployment $ cat > critical-app-deployment.yaml << 'EOF' apiVersion: apps/v1,学习交流加群风哥QQ113257174。 kind: Deployment metadata: name: critical-app spec: replicas: 2 selector: matchLabels: app: critical-app template: metadata: labels: app: critical-app spec: containers: - name: critical-app-container image: nginx:latest resources: requests: cpu: 1 memory: 1Gi priorityClassName: critical-priority EOF $ kubectl apply -f critical-app-deployment.yaml 3. 部署普通应用: # 创建普通应用Deployment $ cat > normal-app-deployment.yaml << 'EOF' apiVersion: apps/v1 kind: Deployment metadata: name: normal-app spec: replicas: 2 selector: matchLabels: app: normal-app template: metadata: labels: app: normal-app spec: containers: - name: normal-app-container image: nginx:latest resources: requests: cpu: 1 memory: 1Gi priorityClassName: normal-priority EOF $ kubectl apply -f normal-app-deployment.yaml 4. 部署批处理应用: # 创建批处理应用Job $ cat > batch-job.yaml << 'EOF' apiVersion: batch/v1 kind: Job metadata: name: batch-job spec: completions: 5 parallelism: 2 template: spec: containers: - name: batch-job-container image: busybox command: ["sh", "-c", "sleep 30"] resources: requests: cpu: 1 memory: 512Mi priorityClassName: batch-priority restartPolicy: OnFailure EOF $ kubectl apply -f batch-job.yaml 5. 查看Pod调度情况: # 查看Pod $ kubectl get pods -o wide # 输出 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES batch-job-5f48d 1/1 Running 0 1m 10.244.2.3 node3
batch-job-7c9d6 1/1 Running 0 1m 10.244.2.4 node3
critical-app-675949546d-2q5k2 1/1 Running 0 2m 10.244.0.2 node1
critical-app-675949546d-5b7c8 1/1 Running 0 2m 10.244.1.2 node2
normal-app-675949546d-7f8d9 1/1 Running 0 2m 10.244.0.3 node1
normal-app-675949546d-9p6q7 1/1 Running 0 2m 10.244.1.3 node2
6. 测试资源紧张情况:
# 创建更多批处理Job
$ kubectl scale job batch-job –replicas=10
# 查看Pod状态
$ kubectl get pods -o wide
# 输出
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
batch-job-5f48d 1/1 Running 0 2m 10.244.2.3 node3
batch-job-7c9d6 1/1 Running 0 2m 10.244.2.4 node3
batch-job-9a8b7 0/1 Pending 0 1m
batch-job-bc6d5 0/1 Pending 0 1m
batch-job-de4f3 0/1 Pending 0 1m
batch-job-fg2h1 0/1 Pending 0 1m
critical-app-675949546d-2q5k2 1/1 Running 0 3m 10.244.0.2 node1
critical-app-675949546d-5b7c8 1/1 Running 0 3m 10.244.1.2 node2
normal-app-675949546d-7f8d9 1/1 Running 0 3m 10.244.0.3 node1
normal-app-675949546d-9p6q7 1/1 Running 0 3m 10.244.1.3 node2
7. 清理资源:
# 清理Deployment和Job
$ kubectl delete deployment critical-app normal-app
$ kubectl delete job batch-job
# 清理PriorityClass
$ kubectl delete priorityclass critical-priority normal-priority batch-priority
# 输出结果:
# PriorityClass配置成功
# 关键应用优先获得资源
# 普通应用次之
# 批处理应用最后获得资源
# 资源紧张时,批处理应用等待

4.2 Preemption案例

Preemption的实战案例。。

# 案例:使用Preemption确保高优先级Pod能够获得资源
# 场景:在Kubernetes集群中,使用Preemption确保高优先级Pod能够抢占低优先级Pod的资源
# 问题:
– 集群资源有限
– 低优先级Pod占用了大量资源
– 高优先级Pod无法调度
# 解决方案:
1. 创建PriorityClass:
# 创建PriorityClass
$ cat > priority-classes.yaml << 'EOF' apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: high-priority value: 1000000,更多视频教程www.fgedu.net.cn。 globalDefault: false description: "High priority class for critical applications" --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: low-priority value: 100000 globalDefault: false description: "Low priority class for non-critical applications" EOF $ kubectl apply -f priority-classes.yaml 2. 创建低优先级Pod: # 创建低优先级Pod $ cat > low-priority-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: low-priority-pod-1 spec: containers: - name: low-priority-container image: nginx:latest resources: requests: cpu: 2 memory: 2Gi priorityClassName: low-priority EOF $ kubectl apply -f low-priority-pod.yaml # 创建更多低优先级Pod $ cat > low-priority-pod-2.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: low-priority-pod-2 spec: containers: - name: low-priority-container image: nginx:latest resources: requests: cpu: 2 memory: 2Gi priorityClassName: low-priority EOF $ kubectl apply -f low-priority-pod-2.yaml # 查看Pod状态 $ kubectl get pods -o wide # 输出 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES low-priority-pod-1 1/1 Running 0 1m 10.244.0.2 node1
low-priority-pod-2 1/1 Running 0 1m 10.244.1.2 node2
3. 创建高优先级Pod,触发抢占:
# 创建高优先级Pod
$ cat > high-priority-pod.yaml << 'EOF' apiVersion: v1 kind: Pod metadata: name: high-priority-pod spec: containers: - name: high-priority-container image: nginx:latest resources: requests: cpu: 2 memory: 2Gi priorityClassName: high-priority EOF $ kubectl apply -f high-priority-pod.yaml 4. 查看抢占情况: # 查看Pod状态 $ kubectl get pods -o wide # 输出 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES high-priority-pod 1/1 Running 0 1m 10.244.0.3 node1
low-priority-pod-1 0/1 Terminating 0 2m 10.244.0.2 node1
low-priority-pod-2 1/1 Running 0 2m 10.244.1.2 node2
# 查看Pod事件
$ kubectl describe pod high-priority-pod
# 输出
Events:
Type Reason Age From Message
—- —— —- —- ——-
Normal Scheduled 1m default-scheduler Successfully assigned default/high-priority-pod to node1
Normal Preempted 1m default-scheduler Preempted pod: default/low-priority-pod-1
Normal Pulling 1m kubelet, node1 Pulling image “nginx:latest”
Normal Pulled 1m kubelet, node1 Successfully pulled image “nginx:latest”
Normal Created 1m kubelet, node1 Created container high-priority-container
Normal Started 1m kubelet, node1 Started container high-priority-container
5. 清理资源:
# 清理Pod
$ kubectl delete pod high-priority-pod low-priority-pod-2
# 清理PriorityClass
$ kubectl delete priorityclass high-priority low-priority
# 输出结果:
# Preemption配置成功
# 高优先级Pod成功抢占低优先级Pod的资源
# 低优先级Pod被优雅终止
# 高优先级Pod成功调度

4.3 集成案例

Pod Priority and Preemption的集成案例。

# 案例:使用Pod Priority and Preemption和其他调度特性部署微服务应用
# 场景:部署一个微服务应用,包括前端、后端和数据库,使用Pod Priority and Preemption和其他调度特性优化调度
# 问题:
– 微服务应用包含不同优先级的组件
– 前端和后端需要低延迟通信
– 数据库需要高IO性能
– 资源有限,需要确保关键组件能够获得资源
# 解决方案:
1. 创建PriorityClass:
# 创建PriorityClass
$ cat > priority-classes.yaml << 'EOF' apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: critical-priority value: 1000000 globalDefault: false description: "Critical priority class for critical applications" --- apiVersion: scheduling.k8s.io/v1 kind: PriorityClass metadata: name: normal-priority value: 500000 globalDefault: false description: "Normal priority class for normal applications" EOF,更多学习教程公众号风哥教程itpux_com。 $ kubectl apply -f priority-classes.yaml 2. 为节点添加标签: # 为节点添加标签 $ kubectl label nodes node1 role=frontend $ kubectl label nodes node2 role=backend $ kubectl label nodes node3 role=database 3. 部署数据库StatefulSet: # 创建数据库StatefulSet $ cat > database-statefulset.yaml << 'EOF' apiVersion: apps/v1 kind: StatefulSet metadata: name: database spec: serviceName: database replicas: 2 selector: matchLabels: app: database template: metadata: labels: app: database spec: containers: - name: database-container image: mysql:8.0 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: password - name: MYSQL_DATABASE value: fgedudb resources: requests: cpu: 2 memory: 4Gi affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: role operator: In values: - database podAntiAffinity: requiredDuringSchedulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - database topologyKey: kubernetes.io/hostname priorityClassName: critical-priority EOF $ kubectl apply -f database-statefulset.yaml 4. 部署后端Deployment: # 创建后端Deployment $ cat > backend-deployment.yaml << 'EOF' apiVersion: apps/v1 kind: Deployment metadata: name: backend spec: replicas: 2 selector: matchLabels: app: backend template: metadata: labels: app: backend spec: containers: - name: backend-container image: node:latest ports: - containerPort: 3000 resources: requests: cpu: 1 memory: 2Gi affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: role operator: In values: - backend priorityClassName: critical-priority EOF $ kubectl apply -f backend-deployment.yaml 5. 部署前端Deployment: # 创建前端Deployment $ cat > frontend-deployment.yaml << 'EOF' apiVersion: apps/v1 kind: Deployment metadata: name: frontend spec: replicas: 2 selector: matchLabels: app: frontend template: metadata: labels: app: frontend spec: containers: - name: frontend-container image: nginx:latest ports: - containerPort: 80 resources: requests: cpu: 1 memory: 1Gi affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: role operator: In values: - frontend priorityClassName: normal-priority EOF $ kubectl apply -f frontend-deployment.yaml,from K8S+DB视频:www.itpux.com。 6. 部署批处理Job: # 创建批处理Job $ cat > batch-job.yaml << 'EOF' apiVersion: batch/v1 kind: Job metadata: name: batch-job spec: completions: 5 parallelism: 2 template: spec:。 containers: - name: batch-job-container image: busybox command: ["sh", "-c", "sleep 30"] resources: requests: cpu: 1 memory: 512Mi priorityClassName: low-priority restartPolicy: OnFailure EOF $ kubectl apply -f batch-job.yaml 7. 查看Pod调度情况: # 查看Pod $ kubectl get pods -o wide # 输出 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES backend-675949546d-2q5k2 1/1 Running 0 2m 10.244.1.2 node2
backend-675949546d-5b7c8 1/1 Running 0 2m 10.244.1.3 node2
database-0 1/1 Running 0 3m 10.244.2.2 node3
database-1 1/1 Running 0 2m 10.244.2.3 node3
frontend-675949546d-7f8d9 1/1 Running 0 2m 10.244.0.2 node1
frontend-675949546d-9p6q7 1/1 Running 0 2m 10.244.0.3 node1
8. 测试抢占:
# 创建更多高优先级Pod
$ kubectl scale deployment backend –replicas=3
# 查看Pod状态
$ kubectl get pods -o wide
# 输出
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
backend-675949546d-2q5k2 1/1 Running 0 3m 10.244.1.2 node2
backend-675949546d-5b7c8 1/1 Running 0 3m 10.244.1.3 node2
backend-675949546d-7f8d9 1/1 Running 0 1m 10.244.0.4 node1
database-0 1/1 Running 0 4m 10.244.2.2 node3
database-1 1/1 Running 0 3m 10.244.2.3 node3
frontend-675949546d-7f8d9 0/1 Terminating 0 3m 10.244.0.2 node1
frontend-675949546d-9p6q7 1/1 Running 0 3m 10.244.0.3 node1
9. 清理资源:
# 清理Deployment、StatefulSet和Job
$ kubectl delete deployment frontend backend
$ kubectl delete statefulset database
$ kubectl delete job batch-job
# 清理PriorityClass
$ kubectl delete priorityclass critical-priority normal-priority
# 移除节点标签
$ kubectl label nodes node1 role-
$ kubectl label nodes node2 role-
$ kubectl label nodes node3 role-
# 输出结果:
# Pod Priority and Preemption配置成功
# 高优先级Pod成功抢占低优先级Pod的资源
# 数据库和后端Pod优先获得资源
# 前端Pod次之
# 资源利用率得到优化

Part05-风哥经验总结与分享

5.1 优先级使用技巧

Kubernetes Pod Priority使用的技巧。

  • 合理设置优先级值:根据应用的重要性设置合适的优先级值,避免设置过高或过低
  • 使用PriorityClass:使用PriorityClass定义优先级类别,便于管理和维护
  • 避免过度使用高优先级:避免为太多应用设置高优先级,导致抢占过于频繁
  • 定期审查优先级设置:定期审查和更新优先级设置,确保其符合应用需求
  • 测试优先级效果:在生产环境中使用前,在测试环境中测试优先级效果,确保其正常工作
  • 监控优先级使用情况:监控Pod优先级的使用情况,及时发现和处理问题
  • 结合其他调度特性:结合Node Affinity、Pod Anti-Affinity等其他调度特性,实现更精细的调度控制
  • 考虑集群规模:根据集群规模调整优先级设置,确保在大规模集群中也能正常工作

5.2 抢占使用技巧

Kubernetes Preemption使用的技巧:

  • 合理配置抢占策略:根据集群规模和应用需求配置合适的抢占策略
  • 监控抢占行为:监控抢占行为,及时发现和处理问题
  • 避免不必要的抢占:避免为非关键应用设置过高的优先级,导致不必要的抢占
  • 考虑Pod生命周期:考虑Pod的生命周期,避免频繁的抢占和重新调度
  • 测试抢占效果:在生产环境中使用前,在测试环境中测试抢占效果,确保其正常工作
  • 配置资源请求和限制:合理配置Pod的资源请求和限制,减少抢占的发生
  • 结合Pod Disruption Budget:使用Pod Disruption Budget限制抢占对应用的影响
  • 文档化抢占策略:文档化所有的抢占策略,便于维护和审计

Kubernetes调度的未来趋势:

  • 更智能的调度策略:使用AI技术实现智能化调度,根据应用需求和集群状态自动调整调度策略
  • 多维度调度:考虑更多维度的因素,如能耗、网络延迟、成本等,实现更优化的调度
  • 边缘计算支持:扩展调度策略到边缘节点,支持边缘计算场景
  • 自定义调度器:提供更灵活的自定义调度器接口,允许用户根据特定需求实现自定义调度逻辑
  • 调度可视化:提供更直观的调度可视化工具,帮助用户理解和优化调度策略
  • 跨集群调度:支持跨多个集群的调度,实现资源的更有效利用
  • 实时调度:实现实时调度,根据集群状态的变化及时调整Pod的调度
  • 安全感知调度:考虑安全因素的调度,确保Pod调度到安全的节点上

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息