您好,登录后才能下订单哦!
# K8S中的APF是怎么用的
## 一、什么是APF
API Priority and Fairness(APF)是Kubernetes 1.20+引入的API请求流量管理机制,属于API Server的流量控制子系统。它通过以下核心机制优化集群稳定性:
1. **多级优先级划分**:将API请求分为不同优先级类别
2. **公平排队算法**:相同优先级的请求按公平性原则分配资源
3. **动态配额调整**:根据系统负载自动调整流量分配
## 二、为什么需要APF
### 传统限流方案的不足
```bash
# 旧版max-requests-inflight配置示例
apiVersion: kubescheduler.config.k8s.io/v1beta2
kind: KubeSchedulerConfiguration
...
maxRequestsInflight: 200
传统方案存在三大缺陷: 1. 粗粒度控制(仅能限制全局请求数) 2. 无法区分请求重要性 3. 缺乏突发流量处理能力
定义请求分类规则,典型配置示例:
apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
kind: FlowSchema
metadata:
  name: kube-controller-manager
spec:
  priorityLevelConfiguration:
    name: workload-high
  matchingPrecedence: 800
  rules:
    - resourceRules:
        - apiGroups: ["*"]
          resources: ["*"]
          verbs: ["*"]
      subjects:
        - kind: User
          user:
            name: system:kube-controller-manager
关键字段说明:
- matchingPrecedence:匹配优先级(数值越小优先级越高)
- resourceRules:资源匹配规则
- subjects:请求主体标识
定义处理优先级,配置示例:
apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
kind: PriorityLevelConfiguration
metadata:
  name: global-default
spec:
  type: Limited
  limited:
    assuredConcurrencyShares: 20
    limitResponse:
      type: Queue
      queuing:
        queues: 16
        queueLengthLimit: 50
        handSize: 8
参数含义:
- assuredConcurrencyShares:并发份额权重
- queues:排队队列数
- handSize:哈希分配参数
kubectl get flowschemas.flowcontrol.apiserver.k8s.io
kubectl get prioritylevelconfigurations.flowcontrol.apiserver.k8s.io
为CI系统创建专用流:
# ci-flow-schema.yaml
apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
kind: FlowSchema
metadata:
  name: ci-jobs
spec:
  priorityLevelConfiguration:
    name: ci-priority
  matchingPrecedence: 900
  rules:
    - resourceRules:
        - apiGroups: ["batch"]
          resources: ["jobs"]
          verbs: ["create"]
      subjects:
        - kind: Group
          group:
            name: "ci-service-accounts"
# ci-priority-level.yaml
apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
kind: PriorityLevelConfiguration
metadata:
  name: ci-priority
spec:
  type: Limited
  limited:
    assuredConcurrencyShares: 50
    limitResponse:
      type: Queue
      queuing:
        queues: 8
        queueLengthLimit: 100
kubectl apply -f ci-flow-schema.yaml
kubectl apply -f ci-priority-level.yaml
关键Prometheus指标:
- apiserver_flowcontrol_current_inqueue_requests
- apiserver_flowcontrol_request_execution_seconds
- apiserver_flowcontrol_rejected_requests_total
Grafana看板配置示例:
{
  "panels": [{
    "title": "APF Queue Depth",
    "targets": [{
      "expr": "sum by(priority_level) (apiserver_flowcontrol_current_inqueue_requests)"
    }]
  }]
}
常见调整参数:
- 增加高优先级FlowSchema的assuredConcurrencyShares
- 调整queueLengthLimit防止内存溢出
- 优化handSize改善哈希分布
分级配置原则
workload-high优先级容量规划建议
单优先级并发量 = (总并发量 × assuredConcurrencyShares) / ∑所有份额
异常处理
apiserver_flowcontrol_rejected_requests_total激增queueLengthLimit检查步骤:
kubectl get --raw /debug/api_priority_and_fairness/dump_requests
验证方法:
kubectl get --raw /metrics | grep apiserver_flowcontrol
优化方向: - 减少FlowSchema数量(合并相似规则) - 增加PriorityLevel并发份额 - 调整排队参数
| Kubernetes版本 | APF特性状态 | 
|---|---|
| 1.18-1.19 | Alpha | 
| 1.20+ | Beta(默认启用) | 
| 1.26+ | GA | 
APF机制通过精细化的流量分类和优先级管理,显著提升了K8S集群的稳定性。合理配置FlowSchema和PriorityLevelConfiguration可以实现: - 关键系统组件的高可用保障 - 多租户环境下的公平资源分配 - 突发流量的优雅降级处理
建议生产环境至少为以下组件配置独立FlowSchema: 1. kube-controller-manager 2. kube-scheduler 3. cluster-autoscaler 4. 核心业务组件 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。