首页 > 其他 > 详细

K8S 创建用户账号-User Account(二)

时间:2020-08-10 15:07:28      阅读:315      评论:0      收藏:0      [点我收藏+]

 

使用cfssl方法创建普通用户

准备工作

mkdir /root/pki/
将k8s ca.pem  ca-key.pem ca-config.json证书拷贝到此目录
cp /opt/kubernetes/ssl/ca-key.pem  /root/pki/
cp /opt/kubernetes/ssl/ca.pem  /root/pki/
cp /root/k8s/cert/k8s/ca-config.json /root/pki/   

与openssl方法 这里多出一个ca-config.json 文件

 

查看ca-config.json

 

[root@master k8s]# cat /root/k8s/cert/k8s/ca-config.json
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
        "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ],
        "expiry": "87600h"
      }
    }
  }
}

 

安装cfssl

下载安装包:
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64


chmod -x cfssl*

for x in cfssl*; do mv $x ${x%*_linux-amd64};  done

mv cfssl* /usr/bin

 

一、生成普通用户证书

cat > devuser-csr.json <<EOF
{
  "CN": "devuser",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}
EOF

 

 

二、执行

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes devuser-csr.json | cfssljson -bare devuser

 

设置集群参数:

kubectl config set-cluster k8s   --server=https://192.168.124.61:6443 \
  --certificate-authority=ca.pem   --embed-certs=true   --kubeconfig=/root/devuser.conf

 

设置上下文参数:

kubectl config set-context dev@k8s   --cluster=k8s   --user=devuser   --kubeconfig=/root/devuser.conf

  

设置客户端参数

kubectl config set-credentials devuser   --client-certificate=devuser.pem   --client-key=devuser-key.pem   --embed-certs=true   --kubeconfig=/root/devuser.conf

 

切换context

kubectl config use-context dev@k8s --kubeconfig=/root/dev.conf
kubectl config view --kubeconfig=/root/dev.conf

创建系统用户

useradd dev
mkdir -p /home/dev/.kube
cp /root/dev.conf /home/dev/.kube/config
chown dev.dev -R /home/dev/
su - dev

  

创建Role 

root@k8s-master:~# cat > pods-reader.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pods-reader
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  - watch
 EOF

创建Rolebinding

root@k8s-master:~# cat >test-pods-reader.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cbmljs-pods-reader
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: pods-reader
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: dev
EOF

  

  

  

 

  

K8S 创建用户账号-User Account(二)

原文:https://www.cnblogs.com/Tempted/p/13469730.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!