RKE Hardening Guide

    备注

    This hardening guide describes how to secure the nodes in your cluster. We recommended that you follow this guide before you install Kubernetes.

    This hardening guide is intended to be used for RKE clusters and is associated with the following versions of the CIS Kubernetes Benchmark, Kubernetes, and Rancher:

    RKE Hardening Guides - 图2备注

    At the time of writing, the upstream CIS Kubernetes v1.25 benchmark is not yet available in Rancher. At this time Rancher is using the CIS v1.23 benchmark when scanning Kubernetes v1.25 clusters. Due to that, the CIS checks 5.2.3, 5.2.4, 5.2.5 and 5.2.6 might fail.

    For more details on how to evaluate a hardened RKE cluster against the official CIS benchmark, refer to the RKE self-assessment guides for specific Kubernetes and CIS benchmark versions.

    The following sysctl configuration is recommended for all nodes types in the cluster. Set the following parameters in /etc/sysctl.d/90-kubelet.conf:

    Run sysctl -p /etc/sysctl.d/90-kubelet.conf to enable the settings.

    A user account and group for the etcd service is required to be set up before installing RKE.

    Create etcd user and group

    1. groupadd --gid 52034 etcd
    2. useradd --comment "etcd service account" --uid 52034 --gid 52034 etcd --shell /usr/sbin/nologin

    When deploying RKE through its cluster configuration config.yml file, update the uid and gid of the etcd user:

    1. services:
    2. etcd:
    3. gid: 52034
    4. uid: 52034

    Kubernetes runtime requirements

    Set automountServiceAccountToken to false for default service accounts

    Kubernetes provides a default service account which is used by cluster workloads where no specific service account is assigned to the pod. Where access to the Kubernetes API from a pod is required, a specific service account should be created for that pod, and rights granted to that service account. The default service account should be configured such that it does not provide a service account token and does not have any explicit rights assignments.

    For each namespace including default and kube-system on a standard RKE install, the default service account must include this value:

    1. automountServiceAccountToken: false

    Save the following configuration to a file called account_update.yaml.

    Create a bash script file called account_update.sh. Be sure to chmod +x account_update.sh so the script has execute permissions.

    1. #!/bin/bash -e
    2. for namespace in $(kubectl get namespaces -A -o=jsonpath="{.items[*]['metadata.name']}"); do
    3. kubectl patch serviceaccount default -n ${namespace} -p "$(cat account_update.yaml)"
    4. done

    Execute this script to apply the account_update.yaml configuration to default service account in all namespaces.

    Ensure that all Namespaces have Network Policies defined

    Running different applications on the same Kubernetes cluster creates a risk of one compromised application attacking a neighboring application. Network segmentation is important to ensure that containers can communicate only with those they are supposed to. A network policy is a specification of how selections of pods are allowed to communicate with each other and other network endpoints.

    Network Policies are namespace scoped. When a network policy is introduced to a given namespace, all traffic not allowed by the policy is denied. However, if there are no network policies in a namespace all traffic will be allowed into and out of the pods in that namespace. To enforce network policies, a container network interface (CNI) plugin must be enabled. This guide uses to provide the policy enforcement. Additional information about CNI providers can be found here.

    Once a CNI provider is enabled on a cluster a default network policy can be applied. For reference purposes a permissive example is provided below. If you want to allow all traffic to all pods in a namespace (even if policies are added that cause some pods to be treated as “isolated”), you can create a policy that explicitly allows all traffic in that namespace. Save the following configuration as default-allow-all.yaml. Additional about network policies can be found on the Kubernetes site.

    This network policy is just an example and is not recommended for production use.

    1. ---
    2. apiVersion: networking.k8s.io/v1
    3. kind: NetworkPolicy
    4. metadata:
    5. name: default-allow-all
    6. spec:
    7. podSelector: {}
    8. ingress:
    9. - {}
    10. egress:
    11. - {}
    12. policyTypes:
    13. - Ingress
    14. - Egress

    Create a bash script file called apply_networkPolicy_to_all_ns.sh. Be sure to chmod +x apply_networkPolicy_to_all_ns.sh so the script has execute permissions.

    1. #!/bin/bash -e
    2. for namespace in $(kubectl get namespaces -A -o=jsonpath="{.items[*]['metadata.name']}"); do
    3. kubectl apply -f default-allow-all.yaml -n ${namespace}
    4. done

    Execute this script to apply the default-allow-all.yaml configuration with the permissive NetworkPolicy to all namespaces.

    • Rancher exec shell and view logs for pods are not functional in a hardened setup when only a public IP is provided when registering custom nodes. This functionality requires a private IP to be provided when registering the custom nodes.
    • When setting default_pod_security_policy_template_id: to restricted or restricted-noroot, based on the pod security policies (PSP) by Rancher, Rancher creates RoleBindings and ClusterRoleBindings on the default service accounts. The CIS check 5.1.5 requires that the default service accounts have no roles or cluster roles bound to it apart from the defaults. In addition, the default service accounts should be configured such that it does not provide a service account token and does not have any explicit rights assignments.

    Reference Hardened RKE cluster.yml Configuration

    The reference cluster.yml is used by the RKE CLI that provides the configuration needed to achieve a hardened installation of RKE. RKE provides additional details about the configuration items. This reference cluster.yml does not include the required nodes directive which will vary depending on your environment. Documentation for node configuration in RKE can be found here.

    The example cluster.yml configuration file contains an Admission Configuration policy in the services.kube-api.admission_configuration field. This policy contains the namespace exemptions necessary for an imported RKE cluster to run properly in Rancher, similar to Rancher’s pre-defined rancher-restricted policy.

    If you prefer to use RKE’s default restricted policy, then leave the services.kube-api.admission_configuration field empty and set services.pod_security_configuration to restricted. See for more information.

    • v1.25 and Newer
    • v1.24 and Older

    备注

    If you intend to import an RKE cluster into Rancher, please consult the documentation for how to configure the PSA to exempt Rancher system namespaces.

    1. # If you intend to deploy Kubernetes in an air-gapped environment,
    2. # please consult the documentation on how to configure custom RKE images.
    3. nodes: []
    4. kubernetes_version: # Define RKE version
    5. services:
    6. etcd:
    7. uid: 52034
    8. gid: 52034
    9. kube-api:
    10. secrets_encryption_config:
    11. enabled: true
    12. audit_log:
    13. event_rate_limit:
    14. enabled: true
    15. pod_security_policy: true
    16. kube-controller:
    17. extra_args:
    18. feature-gates: RotateKubeletServerCertificate=true
    19. kubelet:
    20. extra_args:
    21. feature-gates: RotateKubeletServerCertificate=true
    22. protect-kernel-defaults: true
    23. generate_serving_certificate: true
    24. addons: |
    25. # Upstream Kubernetes restricted PSP policy
    26. # https://github.com/kubernetes/website/blob/564baf15c102412522e9c8fc6ef2b5ff5b6e766c/content/en/examples/policy/restricted-psp.yaml
    27. apiVersion: policy/v1beta1
    28. kind: PodSecurityPolicy
    29. metadata:
    30. name: restricted-noroot
    31. spec:
    32. privileged: false
    33. # Required to prevent escalations to root.
    34. allowPrivilegeEscalation: false
    35. requiredDropCapabilities:
    36. # Allow core volume types.
    37. volumes:
    38. - 'configMap'
    39. - 'emptyDir'
    40. - 'projected'
    41. - 'secret'
    42. - 'downwardAPI'
    43. # Assume that ephemeral CSI drivers & persistentVolumes set up by the cluster admin are safe to use.
    44. - 'csi'
    45. - 'persistentVolumeClaim'
    46. - 'ephemeral'
    47. hostNetwork: false
    48. hostIPC: false
    49. hostPID: false
    50. runAsUser:
    51. # Require the container to run without root privileges.
    52. rule: 'MustRunAsNonRoot'
    53. seLinux:
    54. # This policy assumes the nodes are using AppArmor rather than SELinux.
    55. rule: 'RunAsAny'
    56. supplementalGroups:
    57. rule: 'MustRunAs'
    58. ranges:
    59. # Forbid adding the root group.
    60. - min: 1
    61. max: 65535
    62. fsGroup:
    63. rule: 'MustRunAs'
    64. ranges:
    65. # Forbid adding the root group.
    66. - min: 1
    67. max: 65535
    68. readOnlyRootFilesystem: false
    69. ---
    70. apiVersion: rbac.authorization.k8s.io/v1
    71. kind: ClusterRole
    72. metadata:
    73. name: psp:restricted-noroot
    74. rules:
    75. - apiGroups:
    76. - extensions
    77. resourceNames:
    78. - restricted-noroot
    79. resources:
    80. - podsecuritypolicies
    81. verbs:
    82. - use
    83. ---
    84. apiVersion: rbac.authorization.k8s.io/v1
    85. kind: ClusterRoleBinding
    86. metadata:
    87. name: psp:restricted-noroot
    88. roleRef:
    89. apiGroup: rbac.authorization.k8s.io
    90. kind: ClusterRole
    91. name: psp:restricted-noroot
    92. subjects:
    93. - apiGroup: rbac.authorization.k8s.io
    94. kind: Group
    95. name: system:serviceaccounts
    96. - apiGroup: rbac.authorization.k8s.io
    97. kind: Group
    98. name: system:authenticated
    99. ---
    100. apiVersion: networking.k8s.io/v1
    101. kind: NetworkPolicy
    102. metadata:
    103. name: default-allow-all
    104. spec:
    105. podSelector: {}
    106. ingress:
    107. egress:
    108. - {}
    109. policyTypes:
    110. - Ingress
    111. - Egress
    112. ---
    113. apiVersion: v1
    114. kind: ServiceAccount
    115. metadata:
    116. name: default
    117. automountServiceAccountToken: false

    The reference RKE cluster template provides the minimum required configuration to achieve a hardened installation of Kubernetes. RKE templates are used to provision Kubernetes and define Rancher settings. Follow the Rancher documentation for additional information about installing RKE and its template details.

    • v1.25 and Newer
    1. #
    2. # Cluster Config
    3. #
    4. default_pod_security_admission_configuration_template_name: rancher-restricted
    5. enable_network_policy: true
    6. local_cluster_auth_endpoint:
    7. enabled: true
    8. name: # Define cluster name
    9. #
    10. # Rancher Config
    11. #
    12. rancher_kubernetes_engine_config:
    13. addon_job_timeout: 45
    14. authentication:
    15. strategy: x509|webhook
    16. kubernetes_version: # Define RKE version
    17. services:
    18. etcd:
    19. uid: 52034
    20. gid: 52034
    21. kube-api:
    22. audit_log:
    23. enabled: true
    24. event_rate_limit:
    25. enabled: true
    26. pod_security_policy: false
    27. secrets_encryption_config:
    28. enabled: true
    29. kube-controller:
    30. extra_args:
    31. feature-gates: RotateKubeletServerCertificate=true
    32. tls-cipher-suites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_GCM_SHA256
    33. kubelet:
    34. extra_args:
    35. feature-gates: RotateKubeletServerCertificate=true
    36. protect-kernel-defaults: true
    37. tls-cipher-suites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_GCM_SHA256
    38. generate_serving_certificate: true
    39. scheduler:
    40. extra_args:
    41. tls-cipher-suites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_GCM_SHA256
    1. #
    2. # Cluster Config
    3. #
    4. default_pod_security_policy_template_id: restricted-noroot
    5. enable_network_policy: true
    6. local_cluster_auth_endpoint:
    7. enabled: true
    8. name: # Define cluster name
    9. #
    10. # Rancher Config
    11. #
    12. rancher_kubernetes_engine_config:
    13. addon_job_timeout: 45
    14. authentication:
    15. strategy: x509|webhook
    16. kubernetes_version: # Define RKE version
    17. services:
    18. etcd:
    19. uid: 52034
    20. gid: 52034
    21. kube-api:
    22. audit_log:
    23. enabled: true
    24. event_rate_limit:
    25. enabled: true
    26. pod_security_policy: true
    27. secrets_encryption_config:
    28. enabled: true
    29. kube-controller:
    30. extra_args:
    31. feature-gates: RotateKubeletServerCertificate=true
    32. tls-cipher-suites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_GCM_SHA256
    33. kubelet:
    34. extra_args:
    35. feature-gates: RotateKubeletServerCertificate=true
    36. protect-kernel-defaults: true
    37. tls-cipher-suites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_GCM_SHA256
    38. generate_serving_certificate: true
    39. scheduler:
    40. extra_args:
    41. tls-cipher-suites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_GCM_SHA256

    Conclusion