|
| 1 | +# Lab | Kubernetes kubectl improvements |
| 2 | + |
| 3 | +Whenever you installed Kubernetes following [Kubernetes-Install-Kubelab.md](Kubernetes-Install-Kubelab.md) |
| 4 | +or [Kubernetes-Install-Minikube.md](Kubernetes-Install-Minikube.md), in both |
| 5 | +cases you should have installed also the `kubectl` command to interact with the |
| 6 | +cluster. |
| 7 | + |
| 8 | +This lab helps to improve `kubectl` functionalities by enabling auto completion |
| 9 | +and `krew` utilities. |
| 10 | + |
| 11 | +## Enable kubectl command completion |
| 12 | + |
| 13 | +The `kubectl` command can be used to produce a bash completion file to be |
| 14 | +included in your shell. |
| 15 | + |
| 16 | +The `bash-completion` package is mandatory for both a Red Hat based |
| 17 | +installation: |
| 18 | + |
| 19 | +```console |
| 20 | +$ sudo yum -y install bash-completion |
| 21 | +... |
| 22 | +``` |
| 23 | + |
| 24 | +As well as Debian based: |
| 25 | + |
| 26 | +```console |
| 27 | +$ sudo apt install -y bash-completion |
| 28 | +(no output) |
| 29 | +``` |
| 30 | + |
| 31 | +Bash completion is usually enabled by default, but can be manually activated: |
| 32 | + |
| 33 | +```console |
| 34 | +$ source /etc/profile.d/bash_completion.sh |
| 35 | +(no output) |
| 36 | +``` |
| 37 | + |
| 38 | +Once this is done to enable auto completion with `kubectl` use these commands: |
| 39 | + |
| 40 | +```console |
| 41 | +$ kubectl completion bash > ~/.kubectl-completion |
| 42 | +(no output) |
| 43 | + |
| 44 | +$ echo "source ~/.kubectl-completion" >> ~/.bash_profile |
| 45 | +(no output) |
| 46 | + |
| 47 | +$ source ~/.kubectl-completion |
| 48 | +(no output) |
| 49 | + |
| 50 | +$ kubectl <PRESS TAB> |
| 51 | +annotate attach cluster-info cordon describe exec kustomize patch replace set version |
| 52 | +api-resources auth completion cp diff explain label plugin rollout taint wait |
| 53 | +api-versions autoscale config create drain expose logs port-forward run top |
| 54 | +apply certificate convert delete edit get options proxy scale uncordon |
| 55 | +``` |
| 56 | + |
| 57 | +Remember that "Tab" is your friend. Use it! |
| 58 | + |
| 59 | +## Use krew to extend kubectl functionalities |
| 60 | + |
| 61 | +To Install krew you will need `git` on your system. |
| 62 | + |
| 63 | +If you use Red Hat based systems install it via: |
| 64 | + |
| 65 | +```console |
| 66 | +$ sudo yum -y install git |
| 67 | +... |
| 68 | +``` |
| 69 | + |
| 70 | +or with Debian based system use: |
| 71 | + |
| 72 | +```console |
| 73 | +$ sudo apt install -y git |
| 74 | +... |
| 75 | +``` |
| 76 | + |
| 77 | +Then you can proceed by downloading `krew` and installing it: |
| 78 | + |
| 79 | +```console |
| 80 | +$ curl -LO https://github.com/kubernetes-sigs/krew/releases/download/v0.4.4/krew-linux_amd64.tar.gz |
| 81 | +... |
| 82 | + |
| 83 | +$ tar -xzvf krew-linux_amd64.tar.gz |
| 84 | +... |
| 85 | + |
| 86 | +$ sudo mv krew-linux_amd64 /usr/local/bin/krew |
| 87 | + |
| 88 | +$ krew install krew |
| 89 | +Adding "default" plugin index from https://github.com/kubernetes-sigs/krew-index.git. |
| 90 | +Updated the local copy of plugin index. |
| 91 | +Installing plugin: krew |
| 92 | +Installed plugin: krew |
| 93 | +\ |
| 94 | + | Use this plugin: |
| 95 | + | kubectl krew |
| 96 | + | Documentation: |
| 97 | + | https://krew.sigs.k8s.io/ |
| 98 | + | Caveats: |
| 99 | + | \ |
| 100 | + | | krew is now installed! To start using kubectl plugins, you need to add |
| 101 | + | | krew's installation directory to your PATH: |
| 102 | + | | |
| 103 | + | | * macOS/Linux: |
| 104 | + | | - Add the following to your ~/.bashrc or ~/.zshrc: |
| 105 | + | | export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" |
| 106 | + | | - Restart your shell. |
| 107 | + | | |
| 108 | + | | * Windows: Add %USERPROFILE%\.krew\bin to your PATH environment variable |
| 109 | + | | |
| 110 | + | | To list krew commands and to get help, run: |
| 111 | + | | $ kubectl krew |
| 112 | + | | For a full list of available plugins, run: |
| 113 | + | | $ kubectl krew search |
| 114 | + | | |
| 115 | + | | You can find documentation at |
| 116 | + | | https://krew.sigs.k8s.io/docs/user-guide/quickstart/. |
| 117 | + | / |
| 118 | +/ |
| 119 | + |
| 120 | +$ echo 'export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"' >> .bash_profile |
| 121 | + |
| 122 | +$ export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" |
| 123 | +``` |
| 124 | + |
| 125 | +There are plenty of plugins that can be installed, we will start with `who-can` |
| 126 | +and `tree`: |
| 127 | + |
| 128 | +```console |
| 129 | +$ kubectl krew install who-can |
| 130 | +... |
| 131 | + |
| 132 | +$ kubectl krew install tree |
| 133 | +... |
| 134 | +``` |
| 135 | + |
| 136 | +With this plugins in place it will be possible to know who can list certain |
| 137 | +objects, like Pods: |
| 138 | + |
| 139 | +```console |
| 140 | +$ kubectl who-can list pods |
| 141 | +No subjects found with permissions to list pods assigned through RoleBindings |
| 142 | + |
| 143 | +CLUSTERROLEBINDING SUBJECT TYPE SA-NAMESPACE |
| 144 | +cluster-admin system:masters Group |
| 145 | +kubeadm:cluster-admins kubeadm:cluster-admins Group |
| 146 | +system:controller:attachdetach-controller attachdetach-controller ServiceAccount kube-system |
| 147 | +system:controller:cronjob-controller cronjob-controller ServiceAccount kube-system |
| 148 | +system:controller:daemon-set-controller daemon-set-controller ServiceAccount kube-system |
| 149 | +system:controller:deployment-controller deployment-controller ServiceAccount kube-system |
| 150 | +system:controller:endpoint-controller endpoint-controller ServiceAccount kube-system |
| 151 | +system:controller:endpointslice-controller endpointslice-controller ServiceAccount kube-system |
| 152 | +system:controller:ephemeral-volume-controller ephemeral-volume-controller ServiceAccount kube-system |
| 153 | +system:controller:generic-garbage-collector generic-garbage-collector ServiceAccount kube-system |
| 154 | +system:controller:horizontal-pod-autoscaler horizontal-pod-autoscaler ServiceAccount kube-system |
| 155 | +system:controller:job-controller job-controller ServiceAccount kube-system |
| 156 | +system:controller:namespace-controller namespace-controller ServiceAccount kube-system |
| 157 | +system:controller:node-controller node-controller ServiceAccount kube-system |
| 158 | +system:controller:persistent-volume-binder persistent-volume-binder ServiceAccount kube-system |
| 159 | +system:controller:pod-garbage-collector pod-garbage-collector ServiceAccount kube-system |
| 160 | +system:controller:pvc-protection-controller pvc-protection-controller ServiceAccount kube-system |
| 161 | +system:controller:replicaset-controller replicaset-controller ServiceAccount kube-system |
| 162 | +system:controller:replication-controller replication-controller ServiceAccount kube-system |
| 163 | +system:controller:resourcequota-controller resourcequota-controller ServiceAccount kube-system |
| 164 | +system:controller:statefulset-controller statefulset-controller ServiceAccount kube-system |
| 165 | +system:coredns coredns ServiceAccount kube-system |
| 166 | +system:kube-controller-manager system:kube-controller-manager User |
| 167 | +system:kube-scheduler system:kube-scheduler User |
| 168 | +trivy-operator trivy-operator ServiceAccount trivy-system |
| 169 | +``` |
| 170 | + |
| 171 | +Or the tree structure of a deployment: |
| 172 | + |
| 173 | +```console |
| 174 | +$ kubectl tree -n kube-system deployment coredns |
| 175 | +NAMESPACE NAME READY REASON AGE |
| 176 | +kube-system Deployment/coredns - 36m |
| 177 | +kube-system └─ReplicaSet/coredns-7db6d8ff4d - 35m |
| 178 | +kube-system ├─ConfigAuditReport/replicaset-coredns-7db6d8ff4d - 24m |
| 179 | +kube-system ├─ExposedSecretReport/replicaset-coredns-7db6d8ff4d-coredns - 23m |
| 180 | +kube-system ├─Pod/coredns-7db6d8ff4d-bp7r5 True 35m |
| 181 | +kube-system ├─Pod/coredns-7db6d8ff4d-gwvgl True 35m |
| 182 | +kube-system ├─SbomReport/replicaset-coredns-7db6d8ff4d-coredns - 23m |
| 183 | +kube-system └─VulnerabilityReport/replicaset-coredns-7db6d8ff4d-coredns - 23m |
| 184 | +``` |
0 commit comments