Using official kubectl instead of microk8s.kubectl

Using microk8s 1.18.3, latest/edge, Ubuntu 20.04

When practicing on the machine where microk8s is installed. Is there any harm to install the official kubectl instead of microk8s.kubectl?

Reason: I do have alias kubectl='microk8s.kubectl' and on the command line, the kubectl alias does work OK in interactive mode. However when running in shell script, the the kubectl alias is not expanded. And I endup getting an error. Example: “./MyScript.sh: line 10: kubectl: command not found”.

Of course, I just need to use microk8s.kubectl in my script. However, I share those shell scripts with other colleagues who don’t have microk8s. Each time they must edit to replace microk8s.kubectl by kubectl. If I could use the official kubectl this would be simpler to maintain the scripts.

Yes, you can use kubectl. The only issue you should be aware of is the case where the version of kubectl is falling behind (not matching) the version of the cluster. Installing the kubectl snap should keep you out of trouble.

Some warning appears when I tried sudo snap install kubectl. I read about classic confinement here and get the idea. But still would like your opinion if it is OK to install in classic mode sudo snap install kubectl --classic ?

$ sudo snap install kubectl

error: This revision of snap "kubectl" was published using classic confinement and thus may perform
       arbitrary system changes outside of the security sandbox that snaps are usually confined to,
       which may put your system at risk.

       If you understand and want to proceed repeat the command including --classic.

Yes it is safe to install kubectl as classic. The kubectl snap is actually packaged by the same team that ships MicroK8s and Charmed Kubernetes [1]. The reason why kubectl is a classic snap is because it needs to access your kubeconfig file and filesystem access is not restricted in strictly confined snaps.

[1] https://ubuntu.com/kubernetes/features

On xubuntu 22.04 I solved this by creating snap alias

sudo snap alias microk8s.kubectl kubectl

1 Like