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

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

使用场景

net.core.somaxconn默认监听队列(backlog)长度为128,当服务繁忙不能满足要求时,需要增大监听队列长度。

操作步骤

  1. 修改kubelet配置。

    有两种方法可以修改kubelet配置。
    • 修改节点池kubelet配置(仅支持1.15及以上集群)。

      登录CCE控制台,进入集群,单击节点池后“更多 > 配置管理”,修改kubelet配置参数。

      图1 节点池配置管理
      图2 修改kubelet参数
    • 修改节点kubelet参数
      1. 登录节点。
      2. 编辑/opt/cloud/cce/kubernetes/kubelet/kubelet 文件。1.15之前版本为 /var/paas/kubernetes/kubelet/kubelet 文件。

        开启net.core.somaxconn开关。

        --allowed-unsafe-sysctls=net.core.somaxconn

      3. 重启kubelet

        systemctl restart kubelet

        查看kubelet状态:

        systemctl status kubelet

      1.13及以下版本的集群在修改kubelet后,如果升级集群到更高版本则该配置会还原。

  2. 创建pod安全策略。

    CCE从1.17.17集群版本开始,kube-apiserver开启了pod安全策略,需要在pod安全策略的allowedUnsafeSysctls中增加net.core.somaxconn配置才能生效(1.17.17以下版本的集群可跳过此步骤)。

    • CCE的安全策略介绍,详情请参见Pod安全策略配置
    • K8s社区安全策略介绍,详情请参见PodSecurityPolicy
    • 对于开启net.core.somaxconn开关的集群,需要在对应的Pod安全策略的allowedUnsafeSysctls中增加此项配置,如创建新的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中节点亲和。

    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. 登录部署工作负载的节点,进入容器查看参数配置是否生效。

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

    sysctl -a |grep somax

    图3 查看参数配置

分享:

    相关文档

    相关产品