├── Kubernetes-Chrome-Bookmarks.html ├── LICENSE ├── README.md └── challenges ├── 01 - Encrypting Secret Data at Rest.md ├── 02 - Expanding PVC Storage Size.md ├── 03 - Startup Probe.md └── 04 - Check ServiceAccount Permissions.md /Kubernetes-Chrome-Bookmarks.html: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Bookmarks 7 |

Bookmarks

8 |

9 |

Bookmarks bar

10 |

11 |

CKA

12 |

13 |

1.Cheat Sheet 14 |
Assign Pods to Nodes using Node Affinity | Kubernetes 15 |
CLI - Kube-System-PODS 16 |
ConfigMaps | CM keys created as files 17 |
Container Port Named HTTP and 80 18 |
CSR 19 |
CSR - Create 20 |
DaemonSet 21 |
Define Dependent Environment Variables 22 |
Deployments 23 |
Deployments - set image record 24 |
Deployments - Strategy 25 |
DNS - nslookup 26 |
ETCD - Backup 27 |
Field Selectors | Kubernetes 28 |
fieldRef - downward API 29 |
HPA - Autoscaler 30 |
imagePullSecrets 31 |
Ingress - Host Path 32 |
Ingress - Path 33 |
Init Containers 34 |
InitContainers 35 |
Install Docker 36 |
Job - cron - successful-failed-JobsHistoryLimit 37 |
Job - CronJob 38 |
Job - CRONJOB - startingDeadlineSeconfs 39 |
Jobs 40 |
Jobs - Parallel 41 |
JSONPath 42 |
kube-scheduler | Kubernetes 43 |
Kubeadm - Create Cluster 44 |
Kubeadm AIO 45 |
Kubeadm Install 46 |
Kubeadm Upgrade & Cluster 47 |
Kubectl API - Config 48 |
Kubectl CP Copy 49 |
Kubectl proxy --port=8080 & 50 |
Labels - Get Pods -Lapp 51 |
Labels and Selectors | Kubernetes 52 |
List All Container Images Running in a Cluster 53 |
Liveness, Readiness Startup 54 |
Logging Architecture - Kubernetes 55 |
Multiple Schedulers 56 |
Netpol 57 |
NetPol - Test svc from another Pod 58 |
Nginx HTMl INDEX page PATH 59 |
Node Affinity 60 |
NodeSelector 61 |
OPENSSL 62 |
PV 63 |
PVC 64 |
PVC - resize - SC - Expansion 65 |
Resources - CPU - Memory - Quotas 66 |
Resources - Limits Requests 67 |
Role Cluster Role 68 |
Secret - Config YAML 69 |
Secrets - Docker 70 |
Secrets - envFrom 71 |
Secrets - FilePermissions 72 |
Secrets - Vol - Specific Path 73 |
Security Context - Cap 74 |
Security Context - runAs 75 |
serviceAccountName 76 |
Static Pod 77 |
Storage Class - Dynamic Volume Provisioning 78 |
Storage Classes - Local 79 |
Taints and Tolerations 80 |
Troubleshoot Applications 81 |
Troubleshoot Clusters 82 |
Volumes - CM 83 |
Volumes - EmptyDir 84 |
Volumes - HostPath 85 |
Volumes - PVC 86 |
Volumes - Secret 87 |
Volumes - Secret 88 |
Weave Net CNI 89 |

90 |

91 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Beyond Kubernetes Certification - Challenges 2 | 3 | To keep myself updated and involved with K8S, I will be exploring K8S beyond the certification topics and create challenges here on my findings. These challenges are `good-to-know` and might be `overkill` for CKA/CKAD based certifications. 4 | 5 | I just mentioned few tips and nothing else for `CKA,CKAD` certifications as the internet is flooded with many different blogs, repos, videos, training, exercises to prepare for all 3 Kubernetes certifications. 6 | 7 | If you are looking for `CKS` resources, scroll to the bottom of this page. 8 | 9 | **Note** - Please feel free to make a pull request if there's something wrong, should be added, or updated. 10 | 11 | ## Sections 12 | 1. [CKA and CKAD - Beyond Certification Challeneges](https://github.com/sidd-harth/kubernetes#cka-ckad-challenges) 13 | 2. [CKA and CKAD Exam Tips](https://github.com/sidd-harth/kubernetes#cka-ckad-exam-tips) 14 | - [Using aliases](https://github.com/sidd-harth/kubernetes#using-aliases) 15 | - [VIM editor changes](https://github.com/sidd-harth/kubernetes#vim-editor-changes) 16 | - [Bookmarks](https://github.com/sidd-harth/kubernetes#bookmarks) 17 | 3. [CKS Resources](https://github.com/sidd-harth/kubernetes#cks-resources) 18 | 19 | ## CKA CKAD Challenges 20 | - Challenge 1 - [Encrypting Secret Data at Rest](https://github.com/sidd-harth/kubernetes/blob/main/challenges/01%20-%20Encrypting%20Secret%20Data%20at%20Rest.md) 21 | - Challenge 2 - [Expanding PVC Storage Size](https://github.com/sidd-harth/kubernetes/blob/main/challenges/02%20-%20Expanding%20PVC%20Storage%20Size.md) 22 | - Challenge 3 - [Startup Probe](https://github.com/sidd-harth/kubernetes/blob/main/challenges/03%20-%20Startup%20Probe.md) 23 | - Challenge 4 - [Check ServiceAccount Permissions](https://github.com/sidd-harth/kubernetes/blob/main/challenges/04%20-%20Check%20ServiceAccount%20Permissions.md) 24 | 25 | ## CKA CKAD Exam Tips 26 | Kubectl `aliases` 27 | ``` 28 | alias k=kubectl 29 | alias kn='k config set-context --current --namespace ' 30 | alias kd='k -o yaml --dry-run=client' 31 | alias kall='k get all -o wide --show-labels' 32 | alias kc='k config get-contexts' 33 | ``` 34 | #### Using `aliases` 35 | In the exam, every question has a `context` given, we need to switch over to that context. Some questions are expected to work on specific `namespaces`. Sometimes we tend to forget adding `-n` argument to create resources in a specific namespace. 36 | 37 | These `aliases` will help in quickly changing the `namespace` and also checking the `current context` before answering/debugging the questions. 38 | 39 | - Example - 40 | - Create a Deployment name `nginx-frontend` 41 | - Expose it using a Service named `nginx-svc` 42 | - Write the output of all Service `Endpoints` to /opt/INC002/endpoints.txt 43 | - Everything needs to be done in `rs67` namespace. 44 | 45 | `Without aliases` 46 | ``` 47 | k create deploy nginx-frontend --image nginx -n rs67 48 | k expose deploy nginx-frontend --name nginx-svc --port 80 -n rs67 49 | k get ep -n rs67 > /opt/INC002/endpoints.txt 50 | ``` 51 | `With aliases` 52 | ``` 53 | kn rs67 # changing context to use rs67 namespace 54 | kc # shows the current context and the namespace details 55 | 56 | k create deploy nginx-frontend --image nginx 57 | k expose deploy nginx-frontend --name nginx-svc --port 80 58 | k get ep > /opt/INC002/endpoints.txt 59 | 60 | kn default # I feel it is a good practice to switch back to default namespace after every question 61 | ``` 62 | 63 | #### `VIM` Editor changes 64 | These two additions were enough for me to edit/create `YAMLs` using VI 65 | ``` 66 | sudo vi /etc/vim/vimrc 67 | set number 68 | set paste 69 | ``` 70 | 71 | #### Bookmarks 72 | During the exam, you can keep only one other browser tab open to refer to official documentation. I have uploaded the bookmarks which I have used for 1.19version. These bookmarks can be used for both CKA/CKAD. 73 | | Name | Resource | 74 | | ---- | ------ | 75 | | Bookmark | [Kubernetes-Chrome-Bookmarks](https://github.com/sidd-harth/kubernetes/blob/main/Kubernetes-Chrome-Bookmarks.html) | 76 | 77 | ## CKS Resources 78 | - [Walid Shaari](https://github.com/walidshaari/Certified-Kubernetes-Security-Specialist) 79 | - [ibrahim Jelliti](https://github.com/ibrahimjelliti/CKSS-Certified-Kubernetes-Security-Specialist) 80 | - [Kim Wuestkamp](https://wuestkamp.medium.com/kubernetes-cks-full-course-simulator-3893120baa1d) 81 | - [Kubernetes CKS 2020 Complete Course + Simulator](https://www.udemy.com/course/certified-kubernetes-security-specialist/) 82 | 83 | -------------------------------------------------------------------------------- /challenges/01 - Encrypting Secret Data at Rest.md: -------------------------------------------------------------------------------- 1 | # Encrypting Secret Data at Rest 2 | - I have used `Kubeadm` based Cluster 3 | - Version - 1.19 4 | - etcd v3.0 or later is required 5 | 6 | ### Create `Secret` and Retreive `plain-text` `Secrets` from `ETCD` 7 |

show 8 |

9 | 10 | 1. Create a new secret called `secretpassword` in the default namespace with `password=s3cR3t!` data: 11 | ``` 12 | kubectl create secret generic secretpassword --from-literal=password=s3cR3t! 13 | ``` 14 | 15 | 2. Using the `etcdctl` command line, read that `secret` out of `etcd`: 16 | ``` 17 | ETCDCTL_API=3 etcdctl get /registry/secrets/default/secretpassword \ 18 | --cacert /etc/kubernetes/pki/etcd/ca.crt \ 19 | --cert /etc/kubernetes/pki/etcd/server.crt \ 20 | --key /etc/kubernetes/pki/etcd/server.key 21 | ``` 22 | ![un-encrypted](https://user-images.githubusercontent.com/28925814/100071395-05444e00-2e61-11eb-81e5-2f2eeefd5c5b.png) 23 | 24 | - Pipe the above command with `hexdump -C` 25 | ``` 26 | ETCDCTL_API=3 etcdctl get /registry/secrets/default/secretpassword \ 27 | --cacert /etc/kubernetes/pki/etcd/ca.crt \ 28 | --cert /etc/kubernetes/pki/etcd/server.crt \ 29 | --key /etc/kubernetes/pki/etcd/server.key | hexdump -C 30 | ``` 31 | ![un-encrypted-hexdump](https://user-images.githubusercontent.com/28925814/100071459-17be8780-2e61-11eb-9e5f-68ec75d3fbf8.png) 32 | 33 | > In both these images, we can see that the secret data is saved as `plain` text. Anyone with access to `etcd` can query and get the data. 34 | 35 |

36 |
37 | 38 | ### Encrypting `Secrets` in `ETCD` 39 |
show 40 |

41 | 42 | 1. Generate a 32-byte random key and base64 encode it. 43 | ``` 44 | head -c 32 /dev/urandom | base64 45 | ``` 46 | 2. Create a new encryption config file and replace the `` with the previous step output: 47 | ```yaml 48 | #saving this YAML in /etc/kubernetes/pki/encrypt-secrets.yml 49 | apiVersion: apiserver.config.k8s.io/v1 50 | kind: EncryptionConfiguration 51 | resources: 52 | - resources: 53 | - secrets 54 | providers: 55 | - aescbc: 56 | keys: 57 | - name: key1 58 | secret: 59 | - identity: {} 60 | ``` 61 | 3. Set the `--encryption-provider-config` flag on the `kube-apiserver` to point to the location of the config file. 62 | ![kube-apiserver](https://user-images.githubusercontent.com/28925814/100076227-f6f93080-2e66-11eb-847c-f2cd9ba00888.png) 63 | 64 | 4. Restart your API server. In Kubeadm based cluster saving changes to `/etc/kubernetes/manifests/kube-apiserver.yml` will restart the `kube-apiserver` 65 | > Caution: Your config file contains keys that can decrypt the content in etcd, so you must properly restrict permissions on your masters so only the user who runs the kube-apiserver can read it. 66 | 67 |

68 |
69 | 70 | ### Verifying Encrypted `Secrets` 71 |
show 72 |

73 | 74 | 1. After the `kube-apiserver` gets restarted, any newly created `secret` will be encrypted. 75 | 2. Data is encrypted when written to etcd. So any previously created `secrets` are still in `plain-text` 76 | 3. Performing an update on the existing `secret` will encrypt that content. 77 | ``` 78 | kubectl get secrets --all-namespaces -o json | kubectl replace -f - 79 | ``` 80 | 4. Using the `etcdctl` command line, read that `secret` out of `etcd`: 81 | ``` 82 | ETCDCTL_API=3 etcdctl get /registry/secrets/default/secretpassword \ 83 | --cacert /etc/kubernetes/pki/etcd/ca.crt \ 84 | --cert /etc/kubernetes/pki/etcd/server.crt \ 85 | --key /etc/kubernetes/pki/etcd/server.key 86 | ``` 87 | ![encrypted-secret](https://user-images.githubusercontent.com/28925814/100077317-3a07d380-2e68-11eb-93d3-60c81158d7c4.png) 88 | 89 | - Pipe the above command with `hexdump -C` 90 | ``` 91 | ETCDCTL_API=3 etcdctl get /registry/secrets/default/secretpassword \ 92 | --cacert /etc/kubernetes/pki/etcd/ca.crt \ 93 | --cert /etc/kubernetes/pki/etcd/server.crt \ 94 | --key /etc/kubernetes/pki/etcd/server.key | hexdump -C 95 | ``` 96 | ![encrypted-secret-hexdump](https://user-images.githubusercontent.com/28925814/100077308-37a57980-2e68-11eb-805c-22691d0c56c2.png) 97 | 98 | As seen in the above images, the `secret` is encrypted in `etcd`. 99 | 100 |

101 |
-------------------------------------------------------------------------------- /challenges/02 - Expanding PVC Storage Size.md: -------------------------------------------------------------------------------- 1 | # Expanding PVC Storage Size 2 | 3 | Expanding `PVC` Storage Size depends upon the `StorageClasses` being used. 4 | 5 | Edit any existing or create a `StorageClass` with `allowVolumeExpansion` field set to `true` 6 | 7 | ```yaml 8 | apiVersion: storage.k8s.io/v1 9 | kind: StorageClass 10 | metadata: 11 | name: standard 12 | provisioner: kubernetes.io/aws-ebs 13 | parameters: 14 | type: gp2 15 | reclaimPolicy: Retain 16 | allowVolumeExpansion: true # <--- this should be true 17 | mountOptions: 18 | - debug 19 | volumeBindingMode: Immediate 20 | ``` 21 | 22 | The following types of volumes support volume expansion, when the underlying `StorageClass` has the field `allowVolumeExpansion set to true`. 23 | 24 | |Volume type | Required Kubernetes version| 25 | |----|----| 26 | |gcePersistentDisk |1.11| 27 | |awsElasticBlockStore |1.11| 28 | |Cinder |1.11| 29 | |glusterfs |1.11| 30 | |rbd |1.11| 31 | |Azure File |1.11| 32 | |Azure Disk |1.11| 33 | |Portworx |1.11| 34 | |FlexVolume |1.13| 35 | |CSI |1.14 (alpha), 1.16 (beta)| 36 | 37 | ### Resize a existing PVC named `myclaim` 38 | ``` 39 | kubectl patch pvc myclaim -p '{"spec":{"resources":{"requests":{"storage":"10Gi"}}}}' 40 | ``` 41 | #### Points to note - 42 | - only dynamically provisioned pvc can be resized 43 | - the storageclass that provisions the pvc must support resize 44 | ``` 45 | Error from server (Forbidden): persistentvolumeclaims "myclaim" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize 46 | ``` 47 | - storage size can only be increased 48 | ``` 49 | The PersistentVolumeClaim "myclaim" is invalid: spec.resources.requests.storage: Forbidden: field can not be less than previous value 50 | ``` -------------------------------------------------------------------------------- /challenges/03 - Startup Probe.md: -------------------------------------------------------------------------------- 1 | # Startup Probe 2 | 3 | A Probe is a diagnostic performed periodically by the kubelet on a Container. 4 | 5 | The kubelet can optionally perform and react to three kinds of probes on running containers. The most common and widely known `livenessProbe` and `redinessProbe`. 6 | 7 | **Startup probes** are similar to readiness probes but only executed at startup. 8 | They are optimized for slow starting containers or applications. 9 | We can configure `failureThreshold` and `periodSeconds` 10 | ```yaml 11 | startupProbe: 12 | httpGet: 13 | path: /healthz 14 | port: 8080 15 | failureThreshold: 40 16 | periodSeconds: 5 17 | 18 | livenessProbe: 19 | httpGet: 20 | path: /healthz 21 | port: 8080 22 | failureThreshold: 1 23 | periodSeconds: 10 24 | 25 | readinessProbe: 26 | httpGet: 27 | path: /ready 28 | port: 8080 29 | failureThreshold: 1 30 | periodSeconds: 10 31 | ``` 32 | With this config the application will have a maximum of `200 seconds (40*5)` to finish its startup. Once the startup probe has succeeded, the `liveness probe` takes over. 33 | 34 | If the `startup probe` never succeeds, the container is killed after 200s and subject to the pod's `restartPolicy` -------------------------------------------------------------------------------- /challenges/04 - Check ServiceAccount Permissions.md: -------------------------------------------------------------------------------- 1 | # How to QUICKLY check whether an action is allowed through a ServiceAccount 2 | 3 | ### can-i 4 | `can-i` simply checks with the API to see if an action can be performed. It can take the following options 5 | ```bash 6 | kubectl auth can-i VERB [TYPE | TYPE/NAME | NONRESOURCEURL] 7 | 8 | kubectl auth can-i delete pod/compute 9 | ``` 10 | 11 | ### --as 12 | By default `can-i` checks if the current user has permission to perform an action. To check it for a specific `user` we use `--as` 13 | 14 | We can use the `--as` with or without `auth can-i` 15 | ```bash 16 | kubectl auth can-i VERB [TYPE | TYPE/NAME | NONRESOURCEURL] --as [USERNAME] 17 | 18 | kubectl auth can-i delete pod/compute --as sid 19 | ``` 20 | 21 | ### Service Accounts 22 | We can use the same `--as` with or without `auth can-i` to see what actions a `serviceaccount` can perform before using that in a `pod`. 23 | 24 | ```bash 25 | alias k=kubectl 26 | 27 | kubectl create ns ks001uv 28 | kubectl get sa -n ks001uv 29 | 30 | NAME SECRETS AGE 31 | default 1 66m 32 | ``` 33 | 34 | The default `serviceaccount` in a `namespace` has no permissions other than those of an unauthenticated user. 35 | 36 | Let's try to list the `services` in `ks001uv` namespace using the `default` serviceaccount 37 | ```bash 38 | kubectl auth can-i get svc --as=system:serviceaccount:: 39 | 40 | #with auth can-i 41 | kubectl auth can-i get svc --as=system:serviceaccount:ks001uv:default 42 | no 43 | 44 | # with out auth can-i 45 | kubectl get svc --as=system:serviceaccount:ks001uv:default 46 | Error from server (Forbidden): services is forbidden: User "system:serviceaccount:ks001uv:default" cannot list resource "services" in API group "" inthe namespace "ks001uv" 47 | 48 | ``` 49 | ![no](https://user-images.githubusercontent.com/28925814/100622000-5adb9780-3346-11eb-98cb-a5d8a299f6f7.png) 50 | ![no-as](https://user-images.githubusercontent.com/28925814/100620885-f5d37200-3344-11eb-8fe2-3c8b2081336b.png) 51 | 52 | **We should get `no` or `Forbidden` response as the `default` serviceaccount cannot `list` the services.** 53 | 54 | ### Role and Rolebinding 55 | Let us create a `Role` and `Rolebinding` which would allow the `default` serviceaccount in `ks001uv` namespace to `list` the `services`. 56 | 57 | ```yaml 58 | #### saving this to /root/role-role-binding.yaml 59 | ### Role 60 | apiVersion: rbac.authorization.k8s.io/v1 61 | kind: Role 62 | metadata: 63 | creationTimestamp: null 64 | name: list-svc-role 65 | namespace: ks001uv 66 | rules: 67 | - apiGroups: 68 | - "" 69 | resources: 70 | - services 71 | verbs: 72 | - get 73 | - list 74 | --- 75 | ### Role Binding 76 | apiVersion: rbac.authorization.k8s.io/v1 77 | kind: RoleBinding 78 | metadata: 79 | creationTimestamp: null 80 | name: list-svc-role-binding 81 | namespace: ks001uv 82 | roleRef: 83 | apiGroup: rbac.authorization.k8s.io 84 | kind: Role 85 | name: list-svc-role 86 | subjects: 87 | - kind: ServiceAccount 88 | name: default 89 | namespace: ks001uv 90 | ``` 91 | ```bash 92 | kubectl apply -f /root/role-role-binding.yaml 93 | 94 | role.rbac.authorization.k8s.io/list-svc-role created 95 | rolebinding.rbac.authorization.k8s.io/list-svc-role-binding created 96 | ``` 97 | 98 | ### Testing Service Account 99 | ```bash 100 | #with auth can-i 101 | kubectl auth can-i get svc --as=system:serviceaccount:ks001uv:default 102 | yes 103 | 104 | # with out auth can-i 105 | kubectl get svc --as=system:serviceaccount:ks001uv:default 106 |