Skip to content

Commit e9c857b

Browse files
committed
[Common] Split kubectl improvements
Since these steps applies both to Kubelab and Minikube, lte's have a dedicated file for kubectl improvements.
1 parent 71fbd45 commit e9c857b

File tree

3 files changed

+198
-36
lines changed

3 files changed

+198
-36
lines changed

Common/Kubernetes-Install-Kubelab.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ And then launch the playbooks pointing to the `training_kfs_kubelab` inventory,
2121
using the `training_kfs_kubelab` as `k8s_host_group`:
2222

2323
```console
24-
(ansible-venv) $ ansible-playbook -i kiralab/ -e k8s_host_group=training_kfs_kubelab mmul.kubelab/tests/kubelab.yml
24+
(ansible-venv) $ ansible-playbook -i kiralab/ -e k8s_host_group=training_kfs mmul.kubelab/tests/kubelab.yml
2525
...
2626
PLAY RECAP *******************************************************************************************************
2727
training-kfs-01 : ok=61 changed=43 unreachable=0 failed=0 skipped=30 rescued=0 ignored=0
2828
training-kfs-02 : ok=39 changed=30 unreachable=0 failed=0 skipped=37 rescued=0 ignored=0
2929
training-kfs-03 : ok=39 changed=30 unreachable=0 failed=0 skipped=37 rescued=0 ignored=0
3030
```
3131

32-
Then make `kubectl` available to the system:
32+
## Install kubectl
33+
34+
Make `kubectl` available to the system:
3335

3436
```console
35-
$ export KUBE_VERSION='v1.30'
37+
$ export KUBE_VERSION='v1.30.4'
3638

3739
$ curl -LO "https://dl.k8s.io/release/${KUBE_VERSION}/bin/linux/amd64/kubectl"
3840
...
@@ -44,7 +46,7 @@ mode of 'kubectl' changed from 0664 (rw-rw-r--) to 0775 (rwxrwxr-x)
4446
And use it to check the cluster:
4547

4648
```console
47-
$ export KUBECONFIG=training-kfs/admin.conf
49+
$ export KUBECONFIG=~/training-kfs/admin.conf
4850
(no output)
4951

5052
$ kubectl cluster-info
@@ -59,3 +61,8 @@ training-kfs-01 Ready control-plane 3m39s v1.30.4
5961
training-kfs-02 Ready control-plane 3m8s v1.30.4
6062
training-kfs-03 Ready control-plane 2m32s v1.30.4
6163
```
64+
65+
## Extend kubectl functionalities
66+
67+
There are plenty of ways to extend `kubectl` functionalies, follow [Kubernetes-Kubectl-Improvements.md](Kubernetes-Kubectl-Improvements.md)
68+
to activate some of them.

Common/Kubernetes-Install-Minikube.md

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -149,36 +149,7 @@ kube-system kube-scheduler-minikube 1/1 Running 0
149149
kube-system storage-provisioner 1/1 Running 2 (6m ago) 6m13s
150150
```
151151

152-
### Enable kubectl command completion
152+
## Extend kubectl functionalities
153153

154-
The `kubectl` command can be used to produce a bash completion file to be
155-
included in your shell.
156-
157-
The `bash-completion` package is mandatory:
158-
159-
```console
160-
$ sudo yum -y install bash-completion
161-
...
162-
163-
$ source /etc/profile.d/bash_completion.sh
164-
(no output)
165-
```
166-
167-
And then the completion can be activated:
168-
169-
```console
170-
$ kubectl completion bash > ~/.kubectl-completion
171-
(no output)
172-
173-
$ echo "source ~/.kubectl-completion" >> ~/.bash_profile
174-
(no output)
175-
176-
$ source ~/.kubectl-completion
177-
(no output)
178-
179-
$ kubectl <PRESS TAB>
180-
annotate attach cluster-info cordon describe exec kustomize patch replace set version
181-
api-resources auth completion cp diff explain label plugin rollout taint wait
182-
api-versions autoscale config create drain expose logs port-forward run top
183-
apply certificate convert delete edit get options proxy scale uncordon
184-
```
154+
There are plenty of ways to extend `kubectl` functionalies, follow [Kubernetes-Kubectl-Improvements.md](Kubernetes-Kubectl-Improvements.md)
155+
to activate some of them.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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

Comments
 (0)