安全加固指南 - v2.5.0 - CIS1.6

加固指南旨在与特定版本的 CIS Kubernetes Benchmark,Kubernetes 和 Rancher 一起使用:

下面的安全加固指南是针对在生产环境的 Rancher v2.5.4 中使用 Kubernetes v1.15 版本的集群。它概述了如何满足互联网安全中心(CIS)提出的 Kubernetes 安全标准。

有关如果根据官方 CIS 基准评估集群的更多详细信息,请参阅CIS Benchmark Rancher 自测指南 - Rancher v2.5.4

已知问题

如果注册自定义节点时只提供了公共 IP,在 CIS 1.6 加固设置中,将无法正常在 Rancher UI 中使用执行命令行查看日志功能。

  • 如果注册自定义节点时只提供了公共 IP,在 CIS 1.6 加固设置中,将无法正常在 Rancher UI 中使用执行命令行查看日志功能。如果想要使用上述两个功能,请在注册自定义节点时提供私有 IP 地址。
  • default_pod_security_policy_template_id:restricted时,Rancher 在默认的 service account 中创建角色绑定集群角色绑定。CIS 1.6 要求默认 service account 没有绑定任何角色,不提供 service account 的 token,不分配特定的权限。

配置内核运行时参数

对于集群中的所有类型的节点,都建议使用以下的sysctl配置。在/etc/sysctl.d/90-kubelet.conf中设置以下参数:

Copy

执行sysctl -p /etc/sysctl.d/90-kubelet.conf来启用配置。

在安装 RKE 之前,需要设置etcd服务的用户帐户和组。etcd用户的uidgid将在 RKE 的config.yml中使用,以在安装期间为文件和目录设置适当的权限。

以下命令使用52034作为uidgid的例子。任何有效的未使用的uidgid也可以用来代替52034

要创建etcd组,请运行以下控制台命令。

  1. addgroup --gid 52034 etcd
  2. useradd --comment "etcd service account" --uid 52034 --gid 52034 etcd

使用etcd用户的uidgid更新 RKE 的 config.yml文件:

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

Copy

default服务账号的automountServiceAccountToken设置为 false

Kubernetes 提供了一个默认服务账号(Service Account),如果集群的工作负载中没有为 Pod 分配任何特定服务账号,那么它将会使用这个default的服务账号。在需要从 Pod 访问 Kubernetes API 的情况下,应为该 Pod 创建一个特定的服务账号,并向该服务账号授予权限。这个default的服务账户应该被设置为不提供服务账号令牌(service account token)和任何权限。将automountServiceAccountToken设置为 false 之后,Kubernetes 在启动 Pod 时,将不会自动注入default服务账户。

对于标准 RKE 安装中包括defaultkube-system在内的每个命名空间,default服务账户必须包含这个值。

Copy

把下面的 yaml 另存为account_update.yaml

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: default
  5. automountServiceAccountToken: false

Copy

创建一个名称为account_update.sh的脚本。通过运行chmod +x account_update.sh,使这个脚本有执行权限。

  1. #!/bin/bash -e
  2. for namespace in $(kubectl get namespaces -o custom-columns=NAME:.metadata.name --no-headers); do
  3. kubectl patch serviceaccount default -n ${namespace} -p "$(cat account_update.yaml)"
  4. done

Copy

确保所有命名空间均已定义网络策略

在同一个 Kubernetes 集群上运行不同的应用程序会产生一个风险,那就是应用可能受到相邻应用程序的攻击。为了确保容器只能与预期的容器进行通信,网络细分是必不可少的。通过设置网络策略(Network Policy),可以设置哪些 Pod 之间可以通信,以及是否可以和其他网络端点进行通信。

网络策略是作用于命名空间范围的。将网络策略应用于给定命名空间时,所有不被这个策略允许的流量将被拒绝。然而,如果命名空间中没有设置网络策略,那么进出这个命名空间中 Pod 的所有流量都将被允许。要使用网络策略,必须启用 CNI(容器网络接口)插件。本指南使用提供策略实施。您可以在这里找到有关 CNI 插件的其他信息。

重要

这个NetworkPolicy示例不建议在生产环境中使用。

Copy

创建一个名称为apply_networkPolicy_to_all_ns.sh的脚本。通过运行chmod +x apply_networkPolicy_to_all_ns.sh,使这个脚本有执行权限

  1. #!/bin/bash -e
  2. for namespace in $(kubectl get namespaces -o custom-columns=NAME:.metadata.name --no-headers); do
  3. kubectl apply -f default-allow-all.yaml -n ${namespace}
  4. done

Copy

运行脚本,以使全部的命名空间使用这个default-allow-all.yaml文件中的宽松NetworkPolicy

您可以用这个供您参考的cluster.yml,通过 RKE CLI 来创建安全加固的 Rancher Kubernetes Engine(RKE)集群。有关每个配置的详细信息,请参阅RKE 文档。这个cluster.yml问号不包括所需的nodes指令,它将根据你的环境而变化。有关如何节点配置的文档可以参考。

  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. # https://rancher.com/docs/rke/latest/en/installation/
  4. # the nodes directive is required and will vary depending on your environment
  5. # documentation for node configuration can be found here:
  6. # https://rancher.com/docs/rke/latest/en/config-options/nodes
  7. nodes: []
  8. services:
  9. etcd:
  10. image: ""
  11. extra_args: {}
  12. extra_binds: []
  13. extra_env: []
  14. win_extra_args: {}
  15. win_extra_binds: []
  16. win_extra_env: []
  17. external_urls: []
  18. ca_cert: ""
  19. cert: ""
  20. key: ""
  21. path: ""
  22. uid: 52034
  23. gid: 52034
  24. snapshot: false
  25. retention: ""
  26. creation: ""
  27. backup_config: null
  28. kube-api:
  29. image: ""
  30. extra_args: {}
  31. extra_binds: []
  32. extra_env: []
  33. win_extra_args: {}
  34. win_extra_binds: []
  35. win_extra_env: []
  36. service_cluster_ip_range: ""
  37. service_node_port_range: ""
  38. pod_security_policy: true
  39. always_pull_images: false
  40. secrets_encryption_config:
  41. enabled: true
  42. custom_config: null
  43. audit_log:
  44. enabled: true
  45. configuration: null
  46. admission_configuration: null
  47. event_rate_limit:
  48. enabled: true
  49. configuration: null
  50. kube-controller:
  51. image: ""
  52. extra_args:
  53. feature-gates: RotateKubeletServerCertificate=true
  54. extra_binds: []
  55. extra_env: []
  56. win_extra_args: {}
  57. win_extra_binds: []
  58. win_extra_env: []
  59. cluster_cidr: ""
  60. service_cluster_ip_range: ""
  61. scheduler:
  62. image: ""
  63. extra_args: {}
  64. extra_binds: []
  65. extra_env: []
  66. win_extra_binds: []
  67. win_extra_env: []
  68. kubelet:
  69. image: ""
  70. extra_args:
  71. feature-gates: RotateKubeletServerCertificate=true
  72. protect-kernel-defaults: "true"
  73. 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
  74. extra_binds: []
  75. extra_env: []
  76. win_extra_args: {}
  77. win_extra_binds: []
  78. win_extra_env: []
  79. cluster_domain: cluster.local
  80. infra_container_image: ""
  81. cluster_dns_server: ""
  82. fail_swap_on: false
  83. generate_serving_certificate: true
  84. kubeproxy:
  85. image: ""
  86. extra_args: {}
  87. extra_binds: []
  88. extra_env: []
  89. win_extra_args: {}
  90. win_extra_binds: []
  91. win_extra_env: []
  92. network:
  93. plugin: ""
  94. options: {}
  95. mtu: 0
  96. node_selector: {}
  97. update_strategy: null
  98. authentication:
  99. strategy: ""
  100. sans: []
  101. webhook: null
  102. addons: |
  103. apiVersion: policy/v1beta1
  104. kind: PodSecurityPolicy
  105. metadata:
  106. name: restricted
  107. spec:
  108. requiredDropCapabilities:
  109. - NET_RAW
  110. privileged: false
  111. allowPrivilegeEscalation: false
  112. defaultAllowPrivilegeEscalation: false
  113. fsGroup:
  114. rule: RunAsAny
  115. runAsUser:
  116. rule: MustRunAsNonRoot
  117. seLinux:
  118. rule: RunAsAny
  119. supplementalGroups:
  120. rule: RunAsAny
  121. volumes:
  122. - emptyDir
  123. - secret
  124. - persistentVolumeClaim
  125. - downwardAPI
  126. - configMap
  127. - projected
  128. ---
  129. apiVersion: networking.k8s.io/v1
  130. kind: NetworkPolicy
  131. metadata:
  132. name: default-allow-all
  133. spec:
  134. podSelector: {}
  135. ingress:
  136. - {}
  137. egress:
  138. - {}
  139. policyTypes:
  140. - Ingress
  141. - Egress
  142. ---
  143. apiVersion: v1
  144. kind: ServiceAccount
  145. metadata:
  146. name: default
  147. automountServiceAccountToken: false
  148. addons_include: []
  149. system_images:
  150. etcd: ""
  151. alpine: ""
  152. nginx_proxy: ""
  153. cert_downloader: ""
  154. kubernetes_services_sidecar: ""
  155. kubedns: ""
  156. dnsmasq: ""
  157. kubedns_sidecar: ""
  158. kubedns_autoscaler: ""
  159. coredns: ""
  160. coredns_autoscaler: ""
  161. nodelocal: ""
  162. kubernetes: ""
  163. flannel: ""
  164. flannel_cni: ""
  165. calico_node: ""
  166. calico_controllers: ""
  167. calico_ctl: ""
  168. calico_flexvol: ""
  169. canal_node: ""
  170. canal_cni: ""
  171. canal_controllers: ""
  172. canal_flannel: ""
  173. canal_flexvol: ""
  174. weave_node: ""
  175. weave_cni: ""
  176. pod_infra_container: ""
  177. ingress: ""
  178. ingress_backend: ""
  179. metrics_server: ""
  180. windows_pod_infra_container: ""
  181. ssh_key_path: ""
  182. ssh_cert_path: ""
  183. ssh_agent_auth: false
  184. authorization:
  185. mode: ""
  186. options: {}
  187. ignore_docker_version: false
  188. kubernetes_version: v1.18.12-rancher1-1
  189. private_registries: []
  190. ingress:
  191. provider: ""
  192. options: {}
  193. node_selector: {}
  194. extra_args: {}
  195. dns_policy: ""
  196. extra_envs: []
  197. extra_volumes: []
  198. extra_volume_mounts: []
  199. update_strategy: null
  200. http_port: 0
  201. https_port: 0
  202. network_mode: ""
  203. cluster_name:
  204. cloud_provider:
  205. name: ""
  206. prefix_path: ""
  207. win_prefix_path: ""
  208. addon_job_timeout: 0
  209. bastion_host:
  210. address: ""
  211. port: ""
  212. user: ""
  213. ssh_key: ""
  214. ssh_key_path: ""
  215. ssh_cert: ""
  216. ssh_cert_path: ""
  217. monitoring:
  218. provider: ""
  219. options: {}
  220. node_selector: {}
  221. update_strategy: null
  222. replicas: null
  223. restore:
  224. restore: false
  225. snapshot_name: ""
  226. dns: null
  227. upgrade_strategy:
  228. max_unavailable_worker: ""
  229. max_unavailable_controlplane: ""
  230. drain: null
  231. node_drain_input: null

Copy

安全加固的 RKE 模板配置参考

这个 RKE 参考模板提供了安装安全加固的 Kubenetes 所需的配置。RKE 模板用于配置 Kubernetes 和定义 Rancher 设置。请参阅获得更多安装和 RKE 模板的详细信息。

Copy

安全加固的 Ubuntu 18.04 LTS cloud-config参考配置

这个供您参考的cloud-config通常被用于云基础架构环境中,来进行计算实例的配置管理。这个参考配置了在安装 kubernetes 之前需要的 Ubuntu 操作系统级别的设置。

  1. #cloud-config
  2. apt:
  3. sources:
  4. docker.list:
  5. source: deb [arch=amd64] http://download.docker.com/linux/ubuntu $RELEASE stable
  6. keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
  7. system_info:
  8. default_user:
  9. groups:
  10. - docker
  11. write_files:
  12. - path: "/etc/apt/preferences.d/docker"
  13. owner: root:root
  14. permissions: "0600"
  15. content: |
  16. Package: docker-ce
  17. Pin: version 5:19*
  18. Pin-Priority: 800
  19. - path: "/etc/sysctl.d/90-kubelet.conf"
  20. owner: root:root
  21. permissions: "0644"
  22. content: |
  23. vm.overcommit_memory=1
  24. vm.panic_on_oom=0
  25. kernel.panic=10
  26. kernel.panic_on_oops=1
  27. kernel.keys.root_maxbytes=25000000
  28. package_update: true
  29. packages:
  30. - docker-ce
  31. - docker-ce-cli
  32. - containerd.io
  33. runcmd:
  34. - sysctl -p /etc/sysctl.d/90-kubelet.conf
  35. - groupadd --gid 52034 etcd

Copy