文档首页/ 云容器引擎 CCE/ 最佳实践/ 网络/ 通过配置容器内核参数增大监听队列长度
更新时间:2024-12-17 GMT+08:00
分享

通过配置容器内核参数增大监听队列长度

使用场景

net.core.somaxconn默认监听队列(backlog)长度为128,当服务繁忙时,如果连接请求超过了监听队列的长度,新的连接请求将会被拒绝。为了避免这种情况的发生,您可以通过配置内核参数net.core.somaxconn来增大监听队列的长度。

操作步骤

  1. 修改kubelet配置。

    直接修改节点kubelet参数后,如果集群升级到更高版本或重置节点会导致配置被还原,请谨慎使用。建议使用修改节点池kubelet配置的方式。

  2. (仅v1.25以下集群需执行)创建Pod安全策略。

    在v1.25以下的CCE集群中kube-apiserver开启了Pod安全策略,需要在Pod安全策略的allowedUnsafeSysctls中增加net.core.somaxconn配置才能生效。关于CCE的安全策略介绍请参见Pod安全策略配置

    示例如下:
    apiVersion: policy/v1beta1
    kind: PodSecurityPolicy
    metadata:
      annotations:
        seccomp.security.alpha.kubernetes.io/allowedProfileNames: '*'
      name: sysctl-psp
    spec:
      allowedUnsafeSysctls:
      - net.core.somaxconn
      allowPrivilegeEscalation: true
      allowedCapabilities:
      - '*'
      fsGroup:
        rule: RunAsAny
      hostIPC: true
      hostNetwork: true
      hostPID: true
      hostPorts:
      - max: 65535
        min: 0
      privileged: true
      runAsGroup:
        rule: RunAsAny
      runAsUser:
        rule: RunAsAny
      seLinux:
        rule: RunAsAny
      supplementalGroups:
        rule: RunAsAny
      volumes:
      - '*'

    创建Pod安全策略sysctl-psp后,还需要为它绑定RBAC权限控制。

    示例如下:

    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: sysctl-psp
    rules:
      - apiGroups:
          - "*"
        resources:
          - podsecuritypolicies
        resourceNames:
          - sysctl-psp
        verbs:
          - use
    
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      name: sysctl-psp
    roleRef:
      kind: ClusterRole
      name: sysctl-psp
      apiGroup: rbac.authorization.k8s.io
    subjects:
    - kind: Group
      name: system:authenticated
      apiGroup: rbac.authorization.k8s.io

  3. 创建工作负载,并配置内核参数值,且需要与1中开启net.core.somaxconn的节点亲和。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        description: ''
      labels:
        appgroup: ''
      name: test1
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: test1
      template:
        metadata:
          annotations:
            metrics.alpha.kubernetes.io/custom-endpoints: '[{"api":"","path":"","port":"","names":""}]'
          labels:
            app: test1
        spec:
          containers:
            - image: 'nginx:1.14-alpine-perl'
              name: container-0
              resources:
                requests:
                  cpu: 250m
                  memory: 512Mi
                limits:
                  cpu: 250m
                  memory: 512Mi
          imagePullSecrets:
            - name: default-secret
          securityContext:
            sysctls:
              - name: net.core.somaxconn
                value: '3000'
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                  - matchExpressions:
                      - key: kubernetes.io/hostname
                        operator: In
                        values:
                          - 192.168.x.x       #节点名称

  4. 进入容器查看参数配置是否生效。

    kubectl exec -it <pod name> -- /bin/sh

    在容器中执行如下命令查询配置参数是否生效。

    sysctl -a |grep somax
    图3 查看参数配置

相关文档