惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
D
Docker
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
腾讯CDC
P
Proofpoint News Feed
A
About on SuperTechFans
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
MyScale Blog
MyScale Blog
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

小涂博客

mTab书签一些基础的操作教程 分享一个简化服务器管理的在线ssh工具利器-WebTerm JS字符串模板替换一般用于国际化中含有动态书内容使用 蓝易云SCDN — 强大的安全防护CDN,为你的网站提供全方位防护! VsCode的EasyLess扩展的setting.json配置 js复制图片到粘贴板 Ubuntu 22.04版本中,root用户登录、硬盘挂载等新机器操作指南 Linux中iostat命令 Redis基本命令和常用数据类型
为普通用户授权访问k8s资源(tls,rbac)
涂山 · 2023-10-21 · via 小涂博客

为普通用户授权访问Kubernetes(K8s)资源,包括TLS和RBAC(Role-Based Access Control),是一项关键的安全实践,以确保只有经过授权的用户可以执行特定操作。

下面是一个详细的过程,以授权普通用户对K8s资源进行访问。

1. 创建用户证书(TLS):

首先,您需要为普通用户创建TLS证书,以便他们可以通过安全通信连接到K8s集群。您可以使用工具如openssl来生成用户证书和私钥。以下是一个简化的示例:

# 创建用户私钥
openssl genpkey -algorithm RSA -out user.key

# 创建证书签发请求
openssl req -new -key user.key -out user.csr -subj "/CN=your-username"

# 自签发证书(仅用于示例,请使用CA签发实际证书)
openssl x509 -req -in user.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out user.crt -days 365

请注意,上述示例中的ca.crtca.key是您K8s集群中的根证书和私钥。在实际环境中,您应该使用一个正规的证书颁发机构(CA)来签发用户证书。

2. 创建K8s用户对象:

接下来,您需要在K8s中创建一个用户对象,这将与TLS证书关联。这可以通过创建一个ServiceAccount和一个ClusterRoleBinding来完成:

# user-serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: your-username
# user-clusterrolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: your-username-clusterrolebinding
subjects:
  - kind: ServiceAccount
    name: your-username
    namespace: default
roleRef:
  kind: ClusterRole
  name: cluster-admin  # 为用户分配适当的角色
  apiGroup: rbac.authorization.k8s.io

应用上述配置文件:

kubectl apply -f user-serviceaccount.yaml
kubectl apply -f user-clusterrolebinding.yaml

这将创建一个ServiceAccount并将其与ClusterRoleBinding相关联,使用户能够在默认命名空间中执行适当的操作。确保根据需要调整roleRef字段,以授予用户适当的权限。

3. 生成用户kubeconfig文件:

用户需要一个kubeconfig文件,以便连接到K8s集群。您可以使用以下命令为用户生成kubeconfig文件:

kubectl config set-credentials your-username \
  --client-certificate=user.crt \
  --client-key=user.key \
  --embed-certs=true
kubectl config set-context your-username-context \
  --cluster=your-cluster-name \
  --user=your-username
kubectl config use-context your-username-context

替换your-usernameyour-cluster-name为实际的用户名和集群名称。

4. 分发kubeconfig文件:

将生成的kubeconfig文件分发给用户。用户可以通过设置KUBECONFIG环境变量来使用该文件:

export KUBECONFIG=/path/to/your-username-kubeconfig.yaml

这样,用户就可以使用kubectl工具连接到K8s集群并执行其授权的操作。

通过以上步骤,您已经为普通用户创建了TLS证书,设置了RBAC规则,并生成了kubeconfig文件,以允许他们访问K8s资源。请务必小心保护TLS证书和私钥,以确保安全性。根据需要,您可以调整RBAC规则,以授予用户所需的权限,确保他们只能执行授权的操作。