{"id":988,"date":"2023-08-22T12:47:36","date_gmt":"2023-08-22T07:17:36","guid":{"rendered":"https:\/\/www.openwriteup.com\/?page_id=988"},"modified":"2023-08-22T12:47:36","modified_gmt":"2023-08-22T07:17:36","slug":"labs-rbac","status":"publish","type":"page","link":"https:\/\/www.openwriteup.com\/?page_id=988","title":{"rendered":"Labs: RBAC"},"content":{"rendered":"<p>In this lab we will cover:<\/p>\n<ul>\n<li>Role and Rolebinding<\/li>\n<li>User mapping<\/li>\n<\/ul>\n<p>Lab1: Role and Rolebinding<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/role.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-989\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/role-300x93.jpg\" alt=\"\" width=\"300\" height=\"93\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/role-300x93.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/role-700x218.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/role-768x239.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/role.jpg 811w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>kubectl create -f role.yaml\r\nkubectl get roles\r\nkubectl describe role pod-reader<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/rb.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-990\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/rb-300x108.jpg\" alt=\"\" width=\"300\" height=\"108\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/rb-300x108.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/rb-700x252.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/rb-768x276.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/rb.jpg 1299w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>kubectl create -f rolebind.yaml\r\n\u00a0kubectl get rolebindings\r\n\u00a0kubectl describe rolebindings read-pods<\/pre>\n<p>Lab 2: Test with user<\/p>\n<pre>#role and role binding testing\r\n#Create a private key for your user. In this example, we will name the file employee.key:\r\nopenssl genrsa -out employee.key 2048\r\n#Create a certificate sign request employee.csr using the private key you just created (employee.key in this example). Make sure you specify your username and group in the -subj section\r\nopenssl req -new -key employee.key -out employee.csr -subj \"\/CN=employee\/O=test\"\r\n#Generate the final certificate employee.crt by approving the certificate sign request, employee.csr, you made earlier. Make sure you substitute the CA_LOCATION placeholder with the location of your cluster CA. In this example, the certificate will be valid for 500 days:\r\nopenssl x509 -req -in employee.csr -CA \/etc\/kubernetes\/pki\/ca.crt -CAkey \/etc\/kubernetes\/pki\/ca.key -CAcreateserial -out employee.crt -days 500\r\nkubectl config set-credentials employee --client-certificate=employee.crt --client-key=employee.key\r\n#Add a new context with the new credentials for your Kubernetes cluster.\r\nkubectl config set-context employee-context --cluster=kubernetes --namespace=default --user=employee\r\nkubectl config get-contexts\r\nkubectl config use-context employee-context\r\nkubectl get pods\r\n kubectl delete pods &lt;pod name&gt;\r\n#Change to admin\r\n kubectl config use-context kubernetes-admin@kubernetes\r\n\r\n<\/pre>\n<p>Lab 3: Clusterrole and Clusterrolebinding<\/p>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusrole.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-991\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusrole-300x94.jpg\" alt=\"\" width=\"300\" height=\"94\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusrole-300x94.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusrole-700x220.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusrole-768x241.jpg 768w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusrole.jpg 1041w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>kubectl create -f clusrole.yaml\r\nkubectl get clusterroles\r\nkubectl describe clusterroles secret-reader<\/pre>\n<p><a href=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusbind.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-992\" src=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusbind-300x155.jpg\" alt=\"\" width=\"300\" height=\"155\" srcset=\"https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusbind-300x155.jpg 300w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusbind-700x363.jpg 700w, https:\/\/www.openwriteup.com\/wp-content\/uploads\/2023\/08\/clusbind.jpg 749w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre>kubectl create -f clusbinding.yaml\r\nkubectl get clusterrolebindings\r\n\u00a0kubectl describe clusterrolebindings read-secrets-global<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this lab we will cover: Role and Rolebinding User mapping Lab1: Role and Rolebinding kubectl create -f role.yaml kubectl get roles kubectl describe role pod-reader kubectl create -f rolebind.yaml \u00a0kubectl get rolebindings \u00a0kubectl describe rolebindings read-pods Lab 2: Test with user #role and role binding testing #Create a private key for your user. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-988","page","type-page","status-publish","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=988"}],"version-history":[{"count":1,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/988\/revisions"}],"predecessor-version":[{"id":993,"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=\/wp\/v2\/pages\/988\/revisions\/993"}],"wp:attachment":[{"href":"https:\/\/www.openwriteup.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}