This commit is contained in:
marcel-dempers 2021-09-14 12:59:35 +10:00
parent 9fb836b8d4
commit 18d9ac2ca0
4 changed files with 35 additions and 11 deletions

View File

@ -122,7 +122,7 @@ export KUBECONFIG=~/.kube/new-config
Create a cluster entry which points to the cluster and contains the details of the CA certificate: Create a cluster entry which points to the cluster and contains the details of the CA certificate:
``` ```
kubectl config set-cluster dev-cluster --server=https://127.0.0.1:51972 \ kubectl config set-cluster dev-cluster --server=https://127.0.0.1:52794 \
--certificate-authority=ca.crt \ --certificate-authority=ca.crt \
--embed-certs=true --embed-certs=true
@ -131,9 +131,9 @@ nano ~/.kube/new-config
``` ```
kubectl config set-credentials bob --client-certificate=bob.crt --client-key=bob.key kubectl config set-credentials bob --client-certificate=bob.crt --client-key=bob.key --embed-certs=true
kubectl config set-context dev --cluster=dev-cluster --namespace=shopping --user=bob kubectl config set-context dev --cluster=dev-cluster --namespace=shopping --user=bob
kubectl config use-context dev kubectl config use-context dev
@ -144,6 +144,7 @@ Error from server (Forbidden): pods is forbidden: User "Bob Smith" cannot list r
## Give Bob Smith Access ## Give Bob Smith Access
``` ```
cd kubernetes/rbac
kubectl create ns shopping kubectl create ns shopping
kubectl -n shopping apply -f .\role.yaml kubectl -n shopping apply -f .\role.yaml
@ -163,9 +164,21 @@ Most business apps will not need to connect to the kubernetes API unless you are
Generally applications will use a service account to connect. </br> Generally applications will use a service account to connect. </br>
You can read more about [Kubernetes Service Accounts](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/). You can read more about [Kubernetes Service Accounts](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/).
Let's deploy a service account
``` ```
kubectl -n shopping apply -f serviceaccount.yaml
```
Now we can deploy a pod that uses the service account
```
kubectl -n shopping apply -f pod.yaml
```
Now we can test the access from within that pod by trying to list pods:
```
kubectl -n shopping exec -it shopping-api -- bash
# Point to the internal API server hostname # Point to the internal API server hostname
APISERVER=https://kubernetes.default.svc APISERVER=https://kubernetes.default.svc
@ -183,4 +196,15 @@ CACERT=${SERVICEACCOUNT}/ca.crt
# List pods through the API # List pods through the API
curl --cacert ${CACERT} --header "Authorization: Bearer $TOKEN" -s ${APISERVER}/api/v1/namespaces/shopping/pods/ curl --cacert ${CACERT} --header "Authorization: Bearer $TOKEN" -s ${APISERVER}/api/v1/namespaces/shopping/pods/
```
# we should see an error not having access
```
Now we can allow this pod to list pods in the shopping namespace
```
kubectl -n shopping apply -f serviceaccount-role.yaml
kubectl -n shopping apply -f serviceaccount-rolebinding.yaml
```
If we try run `curl` command again we can see now we are able to get a json
response with pod information

View File

@ -1,9 +1,9 @@
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod
metadata: metadata:
name: nginx name: shopping-api
spec: spec:
containers: containers:
- image: nginx - image: nginx
name: nginx name: shopping-api
serviceAccountName: shopping-api serviceAccountName: shopping-api

View File

@ -2,11 +2,11 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: Role kind: Role
metadata: metadata:
namespace: shopping namespace: shopping
name: pod-reader name: manage-pods
rules: rules:
- apiGroups: [""] - apiGroups: [""]
resources: ["pods", "pods/exec"] resources: ["pods", "pods/exec"]
verbs: ["get", "watch", "list", "create", "delete"] verbs: ["get", "watch", "list", "create", "delete"]
- apiGroups: [""] - apiGroups: ["apps"]
resources: ["deployments"] resources: ["deployments"]
verbs: ["get", "watch", "list", "delete", "create"] verbs: ["get", "watch", "list", "delete", "create"]

View File

@ -1,7 +1,7 @@
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding kind: RoleBinding
metadata: metadata:
name: read-pods name: manage-pods
namespace: shopping namespace: shopping
subjects: subjects:
- kind: User - kind: User
@ -9,5 +9,5 @@ subjects:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
roleRef: roleRef:
kind: Role kind: Role
name: pod-reader name: manage-pods
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io