k8s的User Account

给user2用户授权aming命名空间Pod读取权限

1.生成ca证书

bash 复制代码
cd /etc/kubernetes/pki/
openssl genrsa -out user1.key 2048
openssl req -new -key user1.key -out user1.csr -subj "/CN=user1"
openssl x509 -req -in user1.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out user1.crt -days 3650

2.生成kubeconfig授权文件

bash 复制代码
# 设置集群
kubectl config set-cluster myk8s \
--certificate-authority=/etc/kubernetes/pki/ca.crt \
--embed-certs=true \
--server=https://192.168.222.101:6443 \  #修改成master节点地址
--kubeconfig=/root/user1.kubecfg

# 查看user1配置,users和context都为空
kubectl config view --kubeconfig=/root/user1.kubecfg

# 设置客户端认证
kubectl config set-credentials user1 \
--client-key=user1.key \
--client-certificate=user1.crt \
--embed-certs=true \
--kubeconfig=/root/user1.kubecfg

# 查看user1配置,users有内容了
kubectl config view --kubeconfig=/root/user1.kubecfg

# 设置context
kubectl config set-context user1@myk8s \
--cluster=myk8s \
--user=user1 \
--kubeconfig=/root/user1.kubecfg

# 查看user1配置,context已经有内容了
kubectl config view --kubeconfig=/root/user1.kubecfg

# 切换context
kubectl config use-context user1@myk8s --kubeconfig=/root/user1.kubecfg

3.创建角色

bash 复制代码
cat > user1-role.yaml <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: aming
  name: user1-role
rules:
- apiGroups:  
  - ""
  resources:  
  - pods
  verbs: 
  - get
  - list
  - watch
EOF

kubectl apply -f user1-role.yaml

4.将用户与角色绑定

bash 复制代码
cat > user1-rolebinding.yaml <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: user1-rolebinding
  namespace: aming
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: user1-role
subjects:
- kind: User
  name: user1
  apiGroup: rbac.authorization.k8s.io
EOF

kubectl apply -f user1-rolebinding.yaml

5.创建系统用户并使用user1的配置

bash 复制代码
useradd aming
mkdir /home/aming/.kube
cp /root/user1.kubecfg /home/aming/.kube/config
chown -R aming.aming /home/aming/.kube/

6.切换到普通用下并访问k8s

bash 复制代码
su - aming
$ kubectl get po
$ kubectl get po -n aming
$ kubectl get deploy -n aming
相关推荐
`Levie4 小时前
Docker-安装部署、容器管理(一)
docker·容器·eureka
东风微鸣6 小时前
QuePaxa VS Raft:广域网共识,为什么「慢但不停」胜过「快但一抖就死」
分布式·云原生·raft
Z21-X9 小时前
云原生运维学习笔记:Kubernetes 自动扩缩容与资源管控实战
运维·笔记·学习·云原生·kubernetes
qq_4523962318 小时前
第三篇:Transactional Outbox 模式详解
java·kubernetes·rocketmq
阿里云云原生1 天前
近期开源的 Durable Harness 对 Always-on 有什么帮助?
云原生
Diligently_1 天前
第 05 篇|慢查询优化实战 —— 一条 SQL 的重构全过程
容器
keke.shengfengpolang1 天前
计算机科学与技术专业考证路线:开发、网络、数据分析与云原生的6项选择
网络·云原生·数据分析
赤生云电竞2 天前
算力调度系统架构实践:计费、配额与抢占策略设计——面向Kubernetes GPU集群的多租户治理
容器·系统架构·kubernetes
FriendshipT2 天前
YOLOE Docker 部署实战:三模式推理、掩码输出与跨图视觉的完整实现
运维·人工智能·python·深度学习·docker·容器
the局外人2 天前
Docker 部署:从镜像构建到容器上线
后端·docker·容器