openssl genrsa -out daniel.key 2048
1.2 Generate a Client Sign Request, CN must match user, O must match group.
openssl req -new -key daniel.key -out daniel.csr -subj "/CN=daniel/O=dev"
1.3 Generate the certificate. (/etc/kubernetes/pki in production env.)
openssl x509 -req -in daniel.csr -CA ~/.minikube/ca.crt -CAkey ~/.minikube/ca.key -CAcreateserial -out daniel.crt -days 500
1.4 Set a user entry in kubeconfig
kubectl config set-credentials daniel --client-certificate=daniel.crt --client-key=daniel.key
1.5 Set a context entry in kubeconfig
kubectl config set-context daniel-context --cluster=minikube --user=daniel
1.6 Test it
kubectl config use-context daniel-context
kubectl create ns ns-test
will return error.
apply this yaml file. (role-and-binding.yaml)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get","watch","list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User # !!!!!!!
name: daniel
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
kubectl config use-context minikube
kubectl apply -f role-and-binding.yaml
kubectl config use-context daniel-context
kubectl create ns ns-test
will also return error.
kubectl get pods
will return success.
openssl genrsa -out daniel.key 2048
2.2 Generate a Client Sign Request, CN must match user, O must match group.
openssl req -new -key daniel.key -out daniel.csr -subj "/CN=daniel/O=dev"
2.3 Generate the certificate. (/etc/kubernetes/pki in production env.)
openssl x509 -req -in daniel.csr -CA ~/.minikube/ca.crt -CAkey ~/.minikube/ca.key -CAcreateserial -out daniel.crt -days 500
2.4 Set a user entry in kubeconfig
kubectl config set-credentials daniel --client-certificate=daniel.crt --client-key=daniel.key
2.5 Set a context entry in kubeconfig
kubectl config set-context daniel-context --cluster=minikube --user=daniel
2.6 Test it
kubectl config use-context daniel-context
kubectl create ns ns-test
will return error.
apply this yaml file. (role-and-binding.yaml)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get","watch","list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: Group # !!!!!!!
name: dev
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
kubectl config use-context minikube
kubectl apply -f role-and-binding.yaml
kubectl config use-context daniel-context
kubectl create ns ns-test
will also return error.
kubectl get pods
will return success.
kubectl create namespace dev
3.2 Create service account (service-account.yaml)
apiVersion: v1
kind: ServiceAccount
metadata:
name: daniel
namespace: dev
kubectl apply -f service-account.yaml
3.3 Create role and rolebinding (role-and-binding.yaml)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: dev
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get","watch","list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: dev
subjects:
- kind: ServiceAccount # !!!!!!!
name: daniel
namespace: dev
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
kubectl apply -f role-and-binding.yaml
3.4 Test it create pod yaml (kubectl-pod.yaml)
apiVersion: v1
kind: Pod
metadata:
name: kubectl-pod
namespace: dev
spec:
containers:
- name: kubectl
image: bibinwilson/docker-kubectl:latest
serviceAccountName: daniel
kubectl apply -f kubectl-pod.yaml
kubectl exec -it -ndev kubectl-pod -- /bin/bash
root@kubectl-pod:/# kubectl get pods -n dev
will return success
root@kubectl-pod:/# kubectl get nodes
will return error