yevvzi 2020-04-26
Kubernetes创建一个集群用户连接API-Server 使用命令模式
#可以用现有用户的CA证书 【ca.crt ca.key】 去签署一个新创建的用户,进行绑定 文件默认路径/etc/Kubernetes/pki/
创建私钥
Cd /etc/Kubernetes/pki/
Openssl genrsa -out sunwenbo,key 2048
生成证书签署请求
Openssl req -new -key sunwenbo.key -out sunwenbo.csr -subj “/CN=sunwenbo”
用CA证书签证
Openssl x509 -req -in sunwenbo.csr -CA ./ca.crt -Cakey ./ca.key -CAcreateserial -out sunwenbo.crt -days 365
查看签署后的证书内容
Openssl x509 -in sunwenbo.crt -text -noout
Kubectl config set-credentials sunwenbo –client-certificate=./sunwenbo.crt –client-key=./sunwenbo,key –embed-certs=true
Kubectl config view #查看会多出一个users
设置上下文,让新建sunwenbo的用户也可以去访问kubernetes集群
Kubectl config set-context –cluster=Kubernetes –user=sunwenbo
Kubectl config view #查看会多出一个context
1#创建一个自定义的集群
Kubectl config set-cluster mycluster –kubeconfig=/tmp/mycluster.conf –server=”IP:端口号” –certificate-authority=/etc/Kubernetes/pki/car.crt –embed-certs=true
#查看自定义集群的信息
Kubectl config view –kubeconfig=/tmp/mycluster.conf
创建role角色和绑定
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: default createionTimestamp: null name: pods-reader rules: - apiGroup: - "" resources: - pods verbs: - get - list - watch
kubectl apply -f role.yaml kubectl get role kubectl describe role pods-reader
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: creationTimestamp: null name: sunwenbo-read-pods roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: pods-reader subjects: - apiGroup: rbac.authorization.k8s.io
kubectl apply -f rolebanging.yaml kubectl describe rolebinding sunwenbo-erad-pods #查看详细内容
创建clusterrole可以访问所有名称空间
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: cluster-reader rules: - apiGroup: - "" resources: - pods verbs: - get - list - watch
kubectl apply -f cluster.yaml kubectl get role kubectl describe role cluster-reader
kubectl create clusterrolebinging admin-read-pods --clusterrole=cluster-reder –user=sunwenbo –dry -run -o yaml > clusterrolebinging.yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: admin-read-pods roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: cluster-reder subjects: - apiGroup: rbac.authorization.k8s.io kind: User name: sunwenbo
#rolebinding绑定clusterbinding,clusterbinding会自动降级,跟随rolebinding定义的权限规则
#给用户绑定系统默认role,可以指定拥有某个名称空间的权限
kubectl create rolebinging default-ns-admin --clusterrole=admin –user=sunwenbo –dry -run -o yaml > default-user-banding.yaml