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
相关推荐
Stewie1213819 小时前
Docker 面试题
运维·docker·容器
vpk11219 小时前
Docker Compose 安装 Redis
redis·docker·容器
没有bug.的程序员20 小时前
Serverless 弹性扩容引发的全线熔断:Spring Boot 启动耗时从 1s 压缩至 0.3s 的物理级绞杀
java·spring boot·kubernetes·serverless·扩容·线上
last demo21 小时前
Docker-compose和图形界面管理
docker·容器·eureka
好像不对劲1 天前
【docker】win10 wsl docker不能挂GPU
运维·docker·容器·wsl
Bonnie3731 天前
云边端一体化解析-什么是云边端,为何能成为AI基础设施核心
人工智能·程序人生·云原生·个人开发
江畔何人初1 天前
Docker、containerd、CRI、shim 之间的关系
运维·docker·云原生·容器·kubernetes
万象.1 天前
docker镜像仓库
运维·docker·容器
2401_891655811 天前
Git + 云原生:如何管理K8s配置版本?
git·云原生·kubernetes
2401_844221321 天前
深入理解K8s中的应用服务:访问、集群与配置
容器·kubernetes·php