内核优化参数

  1. net.ipv6.conf.all.disable_ipv6=1
  2. net.ipv6.conf.default.disable_ipv6=1
  3. net.ipv6.conf.lo.disable_ipv6=1
  4. net.ipv4.ip_forward=1
  5. net.ipv4.conf.all.forwarding=1
  6. fs.file-max=2097152
  7. vm.swappiness=0
  8. net.ipv4.ip_local_port_range = 4096 65000
  9. vm.overcommit_memory = 0
  10. vm.panic_on_oom = 0
  11. net.ipv4.tcp_max_syn_backlog = 262144
  12. net.core.netdev_max_backlog = 262144
  13. net.ipv4.tcp_max_orphans = 262144
  14. net.ipv4.tcp_synack_retries= 1
  15. net.ipv4.tcp_syn_retries= 1
  16. net.ipv4.tcp_tw_reuse= 1

内核模块加载

br_netfilter
ip6_udp_tunnel
ip_set
ip_set_hash_ip
ip_set_hash_net
iptable_filter
iptable_nat
iptable_mangle
iptable_raw
nf_conntrack_netlink
nf_conntrack
nf_conntrack_ipv4
nf_defrag_ipv4
nf_nat
nf_nat_ipv4
nf_nat_masquerade_ipv4
nfnetlink
udp_tunnel
veth
vxlan
x_tables
xt_addrtype
xt_conntrack
xt_comment
xt_mark
xt_multiport
xt_nat
xt_recent
xt_set
xt_statistic

cluster.yml最佳实践

# local集群
nodes:
  - address: 10.13.13.100
    user: admin
    role: [controlplane,etcd,worker]
    ssh_key_path: ~/.ssh/id_rsa
  - address: 10.13.13.101
    user: admin
    role: [controlplane,etcd,worker]
    ssh_key_path: ~/.ssh/id_rsa
  - address: 10.13.13.102
    user: admin
    role: [controlplane,etcd,worker]
    ssh_key_path: ~/.ssh/id_rsa
private_registries:
   - url: dev-hthxharbor.utfinancing.com
     user: admin
     password: Harbor12345
     is_default: true
# Specify network plugin-in (canal, calico, flannel, weave, or none)
network:
  plugin: canal
# Set the name of the Kubernetes cluster
cluster_name: local
# The Kubernetes version used. 
kubernetes_version: v1.20.8-rancher1-1
# Define Kubernetes Services
services:
  etcd:
    backup_config:                             # rke 版本大于等于0.2.x或rancher版本大于等于2.2.0时使用
      enabled: true                            # 设置true启用ETCD自动备份,设置false禁用
      interval_hours: 12                       # 快照创建间隔时间,不加此参数,默认5分钟;设置每12小时创建快照
      retention: 6                             # etcd备份保留份数;
    extra_args:
      # etcd 数据压缩周期,降低etcd资源占用,提高平台稳定性,默认每10小时
      auto-compaction-retention: 1           
      # 修改空间配额为 $((6*1024*1024*1024)),默认2G,最大8G
      quota-backend-bytes: '6442450944'  
      # 压缩模式,按时间周期性压缩(periodic)和保留版本号的压缩(revision)      
      auto-compaction-mode: periodic     
      # 默认修改数量达到 10000 时才会建立快照     
      snapshot-count: 50000
      max-request-bytes: 10485760
      log-level: info
      debug: false
      enable-pprof: false
      # 心跳间隔和选举超时时间
      heartbeat-interval: '500'
      election-timeout: '5000'

  kube-api:
    # IP range for any services created on Kubernetes
    # This must match the service_cluster_ip_range in kube-controller
    service_cluster_ip_range: 10.43.0.0/16
    # Expose a different port range for NodePort services
    service_node_port_range: 30000-32767
    extra_args:
      watch-cache: true
      # 默认100,默认监听(watch)缓存大小。 如果为零,则将为没有设置默认监视大小的资源禁用监视缓存。
      default-watch-cache-size: 1500
      # 事件保留时间,默认1小时
      event-ttl: 1h0m0s
      # 默认值400,在给定时间内进行中非变更类型请求的最大数量。当超过该值时,服务将拒绝所有请求。零表示无限制。
      max-requests-inflight: 1000
      # 默认值200,在给定时间内进行中变更类型请求的最大个数。当超过该值时,服务将拒绝所有请求。零表示无限制。
      max-mutating-requests-inflight: 400
      # kubelet操作超时,默认5s
      kubelet-timeout: 5s

  kube-controller:
    # CIDR pool used to assign IP addresses to pods in the cluster
    cluster_cidr: 10.42.0.0/16
    # IP range for any services created on Kubernetes
    # This must match the service_cluster_ip_range in kube-api
    service_cluster_ip_range: 10.43.0.0/16

  kubelet:
    extra_args:
      # 修改节点最大 Pod 数量。
      max-pods: "100"
      # 密文和配置映射同步时间,默认 1 分钟。
      sync-frequency: '3s'
      # Kubelet 进程可以打开的文件数(默认 1000000),根据节点配置情况调整。
      max-open-files: '2000000'
      # 与 apiserver 会话时的并发数,默认是 10
      kube-api-burst: '40'
      # 与 apiserver 会话时的 QPS,默认是 5,QPS=并发量/平均响应时间。
      kube-api-qps: '20'
      # 设置每个 pod 的最大进程数。如果为“-1”,则 kubelet 默认为节点可分配的 PID 容量。
      pod-max-pids: -1
      # node.status.images 中报告的最大镜像数量。如果指定了“-1”,则不会应用上限,默认 50 
      node-status-max-images: 50            
      # 节点资源预留,根据实际情况调整
      enforce-node-allocatable: 'pods'
      system-reserved: 'cpu=1,memory=2048Mi'     # 系统预留
      kube-reserved: 'cpu=1,memory=2048Mi'       # kubernetes 组件预留

      # POD驱逐,这个参数只支持内存和磁盘。
      ## 硬驱逐阈值
      ### 当节点上的可用资源降至保留值以下时,就会触发强制驱逐。强制驱逐会强制kill掉POD,不会等POD自动退出。
      eviction-hard: 'memory.available<256Mi,nodefs.available<10%,imagefs.available<15%,nodefs.inodesFree<5%'
      ## 软驱逐阈值
      ### 以下四个参数配套使用,当节点上的可用资源少于这个值时但大于硬驱逐阈值时候,会等待eviction-soft-grace-period设置的时长;
      ### 等待中每10s检查一次,当最后一次检查还触发了软驱逐阈值就会开始驱逐,驱逐不会直接Kill POD,先发送停止信号给POD,然后等待eviction-max-pod-grace-period设置的时长;
      ### 在eviction-max-pod-grace-period时长之后,如果POD还未退出则发送强制kill POD"
      eviction-soft: 'memory.available<512Mi,nodefs.available<15%,imagefs.available<20%,nodefs.inodesFree<10%'
      eviction-soft-grace-period: 'memory.available=1m30s,nodefs.available=1m30s,imagefs.available=1m30s,nodefs.inodesFree=1m30s'
      eviction-max-pod-grace-period: '30'
      eviction-pressure-transition-period: '30s'
      # 指定kubelet多长时间向master发布一次节点状态。注意: 它必须与kube-controller中的nodeMonitorGracePeriod一起协调工作。(默认 10s)
      node-status-update-frequency: 10s
      # 设置cAdvisor全局的采集行为的时间间隔,主要通过内核事件来发现新容器的产生。默认1m0s
      global-housekeeping-interval: 1m0s
      # 每个已发现的容器的数据采集频率。默认10s
      housekeeping-interval: 10s
      # 所有运行时请求的超时,除了长时间运行的 pull, logs, exec and attach。超时后,kubelet将取消请求,抛出错误,然后重试。(默认2m0s)
      runtime-request-timeout: 2m0s
      # 指定kubelet计算和缓存所有pod和卷的卷磁盘使用量的间隔。默认为1m0s
      volume-stats-agg-period: 1m0s

    # 可以选择定义额外的卷绑定到服务
    extra_binds:
      - "/usr/libexec/kubernetes/kubelet-plugins:/usr/libexec/kubernetes/kubelet-plugins"
      - '/lib/modules:/lib/modules:ro'



# custom集群
  services:
    etcd:
      backup_config:
        enabled: true
        interval_hours: 12
        retention: 6
        safe_timestamp: false
        timeout: 300
      creation: 12h
      extra_args:
        auto-compaction-mode: periodic
        auto-compaction-retention: 60m
        debug: 'false'
        election-timeout: '5000'
        enable-pprof: 'false'
        heartbeat-interval: '500'
        log-level: info
        max-request-bytes: '10485760'
        quota-backend-bytes: '6442450944'
        snapshot-count: '50000'
      extra_binds:
        - '/etc/localtime:/etc/localtime'
      gid: 0
      retention: 72h
      snapshot: false
      uid: 0
    kube-api:
      always_pull_images: false
      extra_args:
        authorization-webhook-cache-authorized-ttl: 5m0s
        authorization-webhook-cache-unauthorized-ttl: 30s
        default-not-ready-toleration-seconds: '300'
        default-unreachable-toleration-seconds: '300'
        default-watch-cache-size: '2000'
        delete-collection-workers: '10'
        etcd-compaction-interval: 10m0s
        etcd-count-metric-poll-period: 5m0s
        etcd-db-metric-poll-interval: 30s
        event-ttl: 1h0m0s
        goaway-chance: '0.001'
        http2-max-streams-per-connection: '0'
        kubelet-timeout: 10s
        max-mutating-requests-inflight: '2000'
        max-requests-inflight: '4000'
        min-request-timeout: '1800'
        watch-cache: 'true'
        watch-cache-sizes: 'endpoints#1000,pod#5000'
      extra_binds:
        - '/etc/localtime:/etc/localtime'
      pod_security_policy: false
      secrets_encryption_config:
        enabled: false
      service_node_port_range: 30000-32767
    kube-controller:
      extra_args:
        concurrent-deployment-syncs: '5'
        concurrent-endpoint-syncs: '5'
        concurrent-gc-syncs: '20'
        concurrent-namespace-syncs: '10'
        concurrent-replicaset-syncs: '5'
        concurrent-resource-quota-syncs: '5'
        concurrent-service-endpoint-syncs: '5'
        concurrent-service-syncs: '1'
        concurrent-serviceaccount-token-syncs: '5'
        concurrent-statefulset-syncs: '5'
        concurrent-ttl-after-finished-syncs: '5'
        deployment-controller-sync-period: 30s
        kube-api-burst: '100'
        kube-api-qps: '100'
        leader-elect: 'true'
        namespace-sync-period: 5m0s
        node-eviction-rate: '0.1'
        node-monitor-grace-period: 20s
        node-monitor-period: 5s
        node-startup-grace-period: 30s
        pod-eviction-timeout: 1m
        pvclaimbinder-sync-period: 15s
        resource-quota-sync-period: 5m0s
      extra_binds:
        - '/etc/localtime:/etc/localtime'
    kubelet:
      extra_args:
        cgroup-driver: cgroupfs
        cgroups-per-qos: 'true'
        enforce-node-allocatable: pods
        eviction-hard: >-
          memory.available<200Mi,nodefs.available<10%,imagefs.available<10%,nodefs.inodesFree<5%
        eviction-max-pod-grace-period: '30'
        eviction-pressure-transition-period: 30s
        eviction-soft: >-
          memory.available<500Mi,nodefs.available<15%,imagefs.available<15%,nodefs.inodesFree<10%
        eviction-soft-grace-period: >-
          memory.available=1m,nodefs.available=1m,imagefs.available=1m,nodefs.inodesFree=1m
        file-check-frequency: 20s
        global-housekeeping-interval: 1m0s
        housekeeping-interval: 10s
        http-check-frequency: 20s
        image-gc-high-threshold: '85'
        image-gc-low-threshold: '80'
        image-pull-progress-deadline: 1m0s
        kernel-memcg-notification: 'true'
        kube-api-burst: '40'
        kube-api-qps: '20'
        kube-reserved: 'cpu=500m,memory=2048Mi'
        max-open-files: '2000000'
        max-pods: '200'
        node-status-max-images: '50'
        node-status-update-frequency: 10s
        pod-max-pids: '-1'
        registry-burst: '10'
        registry-qps: '0'
        runtime-request-timeout: 2m0s
        serialize-image-pulls: 'false'
        sync-frequency: 3s
        system-reserved: 'cpu=500m,memory=2048Mi'
        volume-stats-agg-period: 1m0s
      extra_binds:
        - >-
          /usr/libexec/kubernetes/kubelet-plugins:/usr/libexec/kubernetes/kubelet-plugins
        - '/etc/localtime:/etc/localtime'
      fail_swap_on: false
      generate_serving_certificate: false
    kubeproxy:
      extra_args:
        config-sync-period: 15m0s
        conntrack-max-per-core: '32768'
        conntrack-min: '131072'
        conntrack-tcp-timeout-close-wait: 1h0m0s
        conntrack-tcp-timeout-established: 24h0m0s
        iptables-masquerade-bit: '14'
        iptables-min-sync-period: 1s
        iptables-sync-period: 30s
        ipvs-min-sync-period: 5s
        ipvs-sync-period: 30s
        ipvs-tcp-timeout: 5s
        kube-api-burst: '20'
        kube-api-qps: '10'
        proxy-mode: ''
      extra_binds:
        - '/etc/localtime:/etc/localtime'
    scheduler:
      extra_args:
        http2-max-streams-per-connection: '0'
        kube-api-burst: '200'
        kube-api-qps: '100'
      extra_binds:
        - '/etc/localtime:/etc/localtime'

helm安装rancher

helm install rancher \
--set systemDefaultRegistry=172.200.50.5 \
--namespace cattle-system \
--set service.type=NodePort \
--set service.ports.nodePort=30443 \
--set rancherImage=172.200.50.5/cnrancher/rancher \
--set rancherImageTag=v2.5.9-ent \
--set busyboxImage=172.200.50.5/rancher/busybox \
--set privateCA=true \
--set ingress.tls.source=secret \
--set auditLog.destination=hostPath \
--set auditLog.hostPath=/var/log/rancher/auditlog/ \
--set auditLog.maxBackups=100 \
--set auditlog.maxAge=30 \
--set auditLog.maxSize=50 \
--set auditLog.level=3 \
./rancher