Security
Some of the scenario questions here are based on Kodekloud's CKA course labs.
CKAD and CKA can have similar scenario questions. It is recommended to go through the CKAD practice tests.
Shortcuts
First run the two commands below for shortcuts.
export do="--dry-run=client -o yaml"
export now="--force --grace-period=0"
Questions
-
We don't want to have to specify the kubeconfig file option on each command. Make the my-kube-config file the default kubeconfig.
Answer
There is no kubectl command to do this. Simply copy the contents of the custom kubeconfig to the default kubeconfig file.
controlplane ~ ➜ cp my-kube-config .kube/config -
With the current-context set to research, we are trying to access the cluster. However something seems to be wrong. Identify and fix the issue.
Try running the kubectl get pods command and look for the error. All users certificates are stored at /etc/kubernetes/pki/users.
Answer
controlplane ~ ✖ k config get-contextsCURRENT NAME CLUSTER AUTHINFO NAMESPACEaws-user@kubernetes-on-aws kubernetes-on-aws aws-user* research test-cluster-1 dev-usertest-user@development development test-usertest-user@production production test-usercontrolplane ~ ➜ k get poerror: unable to read client-cert /etc/kubernetes/pki/users/dev-user/developer-user.crt for dev-user due to open /etc/kubernetes/pki/users/dev-user/developer-user.crt: no such file or directoryIncorrect cert defined in the kubeconfig. Fix it and then try to get the pods again.
controlplane ~ ➜ ls -l /etc/kubernetes/pki/users/dev-user/total 12-rw-r--r-- 1 root root 1025 Dec 30 10:08 dev-user.crt-rw-r--r-- 1 root root 924 Dec 30 10:08 dev-user.csr-rw------- 1 root root 1675 Dec 30 10:08 dev-user.key- name: dev-useruser:client-certificate: /etc/kubernetes/pki/users/dev-user/dev-user.crtclient-key: /etc/kubernetes/pki/users/dev-user/dev-user.keycontrolplane ~ ➜ k get poNo resources found in default namespace. -
Inspect the environment and identify the authorization modes configured on the cluster.
Answer
controlplane ~ ➜ k get po -ANAMESPACE NAME READY STATUS RESTARTS AGEblue blue-app 1/1 Running 0 10mblue dark-blue-app 1/1 Running 0 10mdefault red-697496b845-fkmrr 1/1 Running 0 10mdefault red-697496b845-wxks5 1/1 Running 0 10mkube-flannel kube-flannel-ds-k46ss 1/1 Running 0 12mkube-system coredns-5d78c9869d-bmfpk 1/1 Running 0 12mkube-system coredns-5d78c9869d-s27fp 1/1 Running 0 12mkube-system etcd-controlplane 1/1 Running 0 12mkube-system kube-apiserver-controlplane 1/1 Running 0 12mkube-system kube-controller-manager-controlplane 1/1 Running 0 12mkube-system kube-proxy-tjxfp 1/1 Running 0 12mkube-system kube-scheduler-controlplane 1/1 Running 0 12mcontrolplane ~ ➜ k describe -n kube-system po kube-apiserver-controlplane | grep -i auth--authorization-mode=Node,RBAC--enable-bootstrap-token-auth=true -
How many roles exist in the default namespace?
Answer
controlplane ~ ➜ k api-resources | grep -i roleclusterrolebindings rbac.authorization.k8s.io/v1 false ClusterRoleBindingclusterroles rbac.authorization.k8s.io/v1 false ClusterRolerolebindings rbac.authorization.k8s.io/v1 true RoleBindingroles rbac.authorization.k8s.io/v1 true Rolecontrolplane ~ ➜ k get rolesNo resources found in default namespace. -
How many roles exist in all namespaces together?
Answer
controlplane ~ ➜ k get roles -ANAMESPACE NAME CREATED ATblue developer 2023-12-30T15:24:38Zkube-public kubeadm:bootstrap-signer-clusterinfo 2023-12-30T15:22:42Zkube-public system:controller:bootstrap-signer 2023-12-30T15:22:40Zkube-system extension-apiserver-authentication-reader 2023-12-30T15:22:40Zkube-system kube-proxy 2023-12-30T15:22:43Zkube-system kubeadm:kubelet-config 2023-12-30T15:22:41Zkube-system kubeadm:nodes-kubeadm-config 2023-12-30T15:22:41Zkube-system system::leader-locking-kube-controller-manager 2023-12-30T15:22:40Zkube-system system::leader-locking-kube-scheduler 2023-12-30T15:22:40Zkube-system system:controller:bootstrap-signer 2023-12-30T15:22:40Zkube-system system:controller:cloud-provider 2023-12-30T15:22:40Zkube-system system:controller:token-cleaner 2023-12-30T15:22:40Z -
What are the resources the kube-proxy role in the kube-system namespace is given access to?
Answer
controlplane ~ ➜ k get roles -ANAMESPACE NAME CREATED ATblue developer 2023-12-30T15:24:38Zkube-public kubeadm:bootstrap-signer-clusterinfo 2023-12-30T15:22:42Zkube-public system:controller:bootstrap-signer 2023-12-30T15:22:40Zkube-system extension-apiserver-authentication-reader 2023-12-30T15:22:40Zkube-system kube-proxy 2023-12-30T15:22:43Zkube-system kubeadm:kubelet-config 2023-12-30T15:22:41Zkube-system kubeadm:nodes-kubeadm-config 2023-12-30T15:22:41Zkube-system system::leader-locking-kube-controller-manager 2023-12-30T15:22:40Zkube-system system::leader-locking-kube-scheduler 2023-12-30T15:22:40Zkube-system system:controller:bootstrap-signer 2023-12-30T15:22:40Zkube-system system:controller:cloud-provider 2023-12-30T15:22:40Zkube-system system:controller:token-cleaner 2023-12-30T15:22:40Zcontrolplane ~ ✖ k describe role kube-proxy -n kube-systemName: kube-proxyLabels: <none>Annotations: <none>PolicyRule:Resources Non-Resource URLs Resource Names Verbs--------- ----------------- -------------- -----configmaps [] [kube-proxy] [get] -
Which account is the kube-proxy role assigned to?
Answer
It is binded to a group: system:bootstrappers:kubeadm:default-node-token
controlplane ~ ➜ k get rolebindings.rbac.authorization.k8s.io -n kube-systemNAME ROLE AGEkube-proxy Role/kube-proxy 17mkubeadm:kubelet-config Role/kubeadm:kubelet-config 17mkubeadm:nodes-kubeadm-config Role/kubeadm:nodes-kubeadm-config 17msystem::extension-apiserver-authentication-reader Role/extension-apiserver-authentication-reader 17msystem::leader-locking-kube-controller-manager Role/system::leader-locking-kube-controller-manager 17msystem::leader-locking-kube-scheduler Role/system::leader-locking-kube-scheduler 17msystem:controller:bootstrap-signer Role/system:controller:bootstrap-signer 17msystem:controller:cloud-provider Role/system:controller:cloud-provider 17msystem:controller:token-cleaner Role/system:controller:token-cleaner 17mcontrolplane ~ ➜ k describe rolebindings.rbac.authorization.k8s.io -n kube-system kube-proxyName: kube-proxyLabels: <none>Annotations: <none>Role:Kind: RoleName: kube-proxySubjects:Kind Name Namespace---- ---- ---------Group system:bootstrappers:kubeadm:default-node-token -
A user dev-user is created. User's details have been added to the kubeconfig file. Inspect the permissions granted to the user. Check if the user can list pods in the default namespace.
Use the --as dev-user option with kubectl to run commands as the dev-user.
Answer
controlplane ~ ➜ k get po --as dev-userError from server (Forbidden): pods is forbidden: User "dev-user" cannot list resource "pods" in API group "" in the namespace "default" -
Create the necessary roles and role bindings required for the dev-user to create, list and delete pods in the default namespace. Use the given spec:
-
Role: developer
-
Role Resources: pods
-
Role Actions: list
-
Role Actions: create
-
Role Actions: delete
-
RoleBinding: dev-user-binding
-
RoleBinding: Bound to dev-user
Answer
## role-rolebinding.yamlapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:namespace: defaultname: developerrules:- apiGroups: [""] # "" indicates the core API groupresources: ["pods"]verbs: ["delete", "create", "list"]---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: dev-user-bindingnamespace: defaultsubjects:- kind: Username: dev-userapiGroup: rbac.authorization.k8s.ioroleRef:kind: Role #this must be Role or ClusterRolename: developerapiGroup: rbac.authorization.k8s.iocontrolplane ~ ➜ k apply -f role-rolebinding.yamlrole.rbac.authorization.k8s.io/developer createdrolebinding.rbac.authorization.k8s.io/dev-user-binding createdcontrolplane ~ ➜ k get roleNAME CREATED ATdeveloper 2023-12-30T15:49:59Zcontrolplane ~ ➜ k get rolebindings.rbac.authorization.k8s.ioNAME ROLE AGEdev-user-binding Role/developer 9s -
-
A set of new roles and role-bindings are created in the blue namespace for the dev-user. However, the dev-user is unable to get details of the dark-blue-app pod in the blue namespace. Investigate and fix the issue.
controlplane ~ ➜ k get role -n blueNAME CREATED ATdeveloper 2023-12-30T15:24:38Zcontrolplane ~ ➜ k get rolebindings -n blueNAME ROLE AGEdev-user-binding Role/developer 26mAnswer
controlplane ~ ➜ k get po dark-blue-app -n blue --as dev-userError from server (Forbidden): pods "dark-blue-app" is forbidden: User "dev-user" cannot get resource "pods" in API group "" in the namespace "blue"controlplane ~ ➜ k get role -n blue developer -o yaml > blue-dev-role.yamlcontrolplane ~ ➜ k get rolebindings.rbac.authorization.k8s.io -n blue dev-user-binding -o yaml > blue-dev-rolebinding.yamlCheck the role. Here we can see that the resource name is incorrect.
## blue-dev-rolebinding.yamlapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:creationTimestamp: "2023-12-30T15:24:38Z"name: developernamespace: blueresourceVersion: "619"uid: 994093a1-b5e4-4256-b911-533769b6eb63rules:- apiGroups:- ""resourceNames:- blue-appresources:- podsverbs:- get- watch- create- deleteFix it.
## blue-dev-rolebinding.yamlapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:creationTimestamp: "2023-12-30T15:24:38Z"name: developernamespace: blueresourceVersion: "619"uid: 994093a1-b5e4-4256-b911-533769b6eb63rules:- apiGroups:- ""resourceNames:- dark-blue-appresources:- podsverbs:- get- watch- create- deletecontrolplane ~ ➜ k delete -f blue-dev-role.yamlrole.rbac.authorization.k8s.io "developer" deletedcontrolplane ~ ➜ k apply -f blue-dev-role.yamlrole.rbac.authorization.k8s.io/developer createdcontrolplane ~ ➜ k get po dark-blue-app -n blue --as dev-userNAME READY STATUS RESTARTS AGEdark-blue-app 1/1 Running 0 37m