Kubernetes 安全秘籍:5 个你必须知道的知识点

Kubernetes 安全和身份验证是确保集群和应用安全的关键。今天将深入探讨 Service Account、身份验证和RBAC的关键概念和实践,帮助您构建安全可靠的应用。今天本文将着重于安全相关的内容,并提供更详细的示例和配置说明,帮助兄弟们更深入地理解和应用 Kubernetes 安全和身份验证机制。

Kubernetes Service Account

Service Account 是 Kubernetes 中用于为 Pod 提供身份和凭据的资源。它允许 Pod 访问 Kubernetes API 和其他资源,而无需使用用户凭据。
在这里插入图片描述

Service Account 工作原理

1.1 Service Account 生命周期

Service Account 的生命周期包括以下阶段:

创建: 创建 Service Account 资源。

绑定: 将 Service Account 绑定到 Pod 或其他资源。

使用: Pod 或其他资源使用 Service Account 的身份和凭据访问 Kubernetes API 或其他资源。

删除: 删除 Service Account 资源。

1.2 Service Account 凭据

Service Account 拥有以下两种类型的凭据:

Token: 用于 Pod 访问 Kubernetes API 的令牌。

Secret: 用于 Pod 访问其他资源的密钥。

2. Service Account 实践

2.1 使用默认 Service Account

每个命名空间都包含一个默认的 Service Account,名为 default。Pod 可以使用默认 Service Account 访问 Kubernetes API 和其他资源,无需显式绑定。

2.2 创建自定义 Service Account

您可以创建自定义 Service Account 来满足特定需求。例如,您可以创建具有不同权限或用于访问不同资源的 Service Account。

2.3 绑定 Service Account

您可以将 Service Account 绑定到 Pod 或其他资源。绑定 Service Account 时,您可以指定要使用的 Service Account 以及要授予的权限。

3. Service Account 高级特性

3.1 使用 Automount Service Account Token

您可以使用 automountServiceAccountToken 字段自动将 Service Account 令牌挂载到 Pod 中。这使得 Pod 可以更容易地访问 Kubernetes API。

3.2 使用 Service Account Token 注入

您可以使用 serviceAccountToken 字段将 Service Account 令牌注入到 Pod 的环境变量中。这使得 Pod 可以更容易地访问其他资源。

3.3 使用 Service Account 与 RBAC 结合

您可以使用 RBAC 控制用户创建和修改 Service Account 的权限。您还可以使用 RBAC 控制 Pod 使用 Service Account 的权限。

4. Service Account 安全最佳实践

使用最小特权原则,仅授予 Service Account 必要的权限。

定期审核 Service Account,确保其符合最新需求。

使用安全扫描工具,例如 Clair,扫描 Service Account 凭据中的安全漏洞。

Kubernetes 身份验证

身份验证是 Kubernetes 安全的核心支柱之一,用于认证用户和 Pod 的身份。

认证流程:

  • 用户或 Pod 向 Kubernetes API Server 发送请求。
  • API Server 询问认证模块是否允许访问。
  • 认证模块根据配置的认证方式进行认证。
  • 如果认证成功,则 API Server 允许访问。
  • 如果认证失败,则 API Server 拒绝访问。
    在这里插入图片描述

认证方式

Kubernetes 支持多种认证方式,包括:

  • 本地认证: 使用本地用户名和密码进行认证。
  • LDAP 认证: 使用 LDAP 服务器进行认证。
  • OIDC 认证: 使用 OpenID Connect 认证。
  • 其他: 支持多种第三方认证插件。

1.1 本地认证

本地认证是最简单的认证方式,适用于小型集群或测试环境。

配置本地认证:

  • 创建一个名为 local-auth 的认证模块。
apiVersion: authentication.k8s.io/v1
kind: TokenReview
metadata:
  name: local-auth
spec:
  audiences:
  - kubernetes.io/serviceaccount
  - kubernetes.io/pod

  • 将 local-auth 认证模块绑定到 API Server。
apiVersion: kubeadm.k8s.io/v1
kind: InitConfiguration
metadata:
  name: init-config
spec:
  bootstrapTokens:
  - token: abcdefghijklmnopqrstuvwxyz
    ttl: 24h0m0s
    usages:
    - signing
    - authentication
  localAPIEndpoint:
    advertiseAddress: 127.0.0.1
  nodeRegistration:
    kubeletExtraArgs:
    - --authentication-token-webhook-cache-ttl=2m0s
  serviceAccountKeyFile: /etc/kubernetes/pki/sa.key
  token: abcdefghijklmnopqrstuvwxyz
  ---
apiVersion: kubeadm.k8s.io/v1
kind: ClusterConfiguration
metadata:
  name: cluster-config
spec:
  authentication:
    authenticator:
      name: local-auth

1.2 LDAP 认证

LDAP 认证适用于大型组织,可以使用现有的 LDAP 服务器进行认证。

配置 LDAP 认证:

创建一个名为 ldap-auth 的认证模块。

apiVersion: authentication.k8s.io/v1
kind: TokenReview
metadata:
  name: ldap-auth
spec:
  audiences:
  - kubernetes.io/serviceaccount
  - kubernetes.io/pod
  tokenReview:
    spec:
      server: ldap://ldap.example.com
      bindDN: cn=admin,dc=example,dc=com
      bindPassword: secret
      baseDN: dc=example,dc=com
      insecure: true

将 ldap-auth 认证模块绑定到 API Server。

apiVersion: kubeadm.k8s.io/v1
kind: InitConfiguration
metadata:
  name: init-config
spec:
  bootstrapTokens:
  - token: abcdefghijklmnopqrstuvwxyz
    ttl: 24h0m0s
    usages:
    - signing
    - authentication
  localAPIEndpoint:
    advertiseAddress: 127.0.0.1
  nodeRegistration:
    kubeletExtraArgs:
    - --authentication-token-webhook-cache-ttl=2m0s
  serviceAccountKeyFile: /etc/kubernetes/pki/sa.key
  token: abcdefghijklmnopqrstuvwxyz
  ---
apiVersion: kubeadm.k8s.io/v1
kind: ClusterConfiguration
metadata:
  name: cluster-config
spec:
  authentication:
    authenticator:
      name: ldap-auth

1.3 OIDC 认证

OIDC 认证适用于需要与其他系统进行单点登录 (SSO) 的环境。

配置 OIDC 认证:

创建一个名为 oidc-auth 的认证模块。

apiVersion: authentication.k8s.io/v1
kind: TokenReview
metadata:
  name: oidc-auth
spec:
  audiences:
  - kubernetes.io/serviceaccount
  - kubernetes.io/pod
  tokenReview:
    spec:
      issuer: https://oidc.example.com
      clientID: my-client-id
      clientSecret: my-client-secret
      extraScopes:
      - profile
      - email

  1. 将 OIDC 认证模块绑定到 API Server
apiVersion: kubeadm.k8s.io/v1
kind: InitConfiguration
metadata:
  name: init-config
spec:
  bootstrapTokens:
  - token: abcdefghijklmnopqrstuvwxyz
    ttl: 24h0m0s
    usages:
    - signing
    - authentication
  localAPIEndpoint:
    advertiseAddress: 127.0.0.1
  nodeRegistration:
    kubeletExtraArgs:
    - --authentication-token-webhook-cache-ttl=2m0s
  serviceAccountKeyFile: /etc/kubernetes/pki/sa.key
  token: abcdefghijklmnopqrstuvwxyz
  ---
apiVersion: kubeadm.k8s.io/v1
kind: ClusterConfiguration
metadata:
  name: cluster-config
spec:
  authentication:
    authenticator:
      name: oidc-auth

用户认证流程

  • 用户访问 Kubernetes API Server。
  • API Server 将请求转发给 OIDC 认证模块。
  • OIDC 认证模块将用户重定向到 OIDC 身份提供者进行认证。
  • 用户在 OIDC 身份提供者处输入身份信息并进行认证。
  • OIDC 身份提供者将认证结果返回给 OIDC 认证模块。
  • OIDC 认证模块生成一个 Kubernetes 令牌并返回给用户。
  • 用户使用Kubernetes 令牌访问 Kubernetes API Server。

代码示例
以下是一个使用 OIDC 认证进行登录的python示例代码:

import requests

# OIDC 身份提供者 URL
oidc_provider_url = "https://oidc.example.com"

# OIDC 客户端 ID
client_id = "my-client-id"

# OIDC 客户端密钥
client_secret = "my-client-secret"

# 重定向 URI
redirect_uri = "https://kubernetes.default.svc.cluster.local/auth/callback"

# 请求授权代码
response = requests.get(
    f"{oidc_provider_url}/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=profile email"
)

# 解析授权代码
code = response.url.split("code=")[1]

# 请求令牌
response = requests.post(
    f"{oidc_provider_url}/token",
    data={
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": redirect_uri,
        "client_id": client_id,
        "client_secret": client_secret,
    },
)

# 解析令牌
access_token = response.json()["access_token"]

# 使用令牌访问 Kubernetes API Server
response = requests.get(
    "https://kubernetes.default.svc.cluster.local/api/v1/nodes",
    headers={"Authorization": f"Bearer {access_token}"},
)

# 打印响应
print(response.text)

关注我,我们一起学习更多知识,带你了解更多职场信息内容.

想要了解更多技术文章请关注公众号“职谷智享”,关注后回复关键字【秒杀】可以领取秒杀系统学习资料

在这里插入图片描述

在这里插入图片描述

  • 23
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值