Sign inSign up

tapassharma/kubectl

By tapassharma

•Updated about 6 years ago

Run kubectl as a container

Image
0

263

tapassharma/kubectl repository overview

Welcome to the kubectl (in a container)

⁠Contents

⁠Releases

Latest Release: Click Here⁠ Build Status

For Version specific releases

⁠Motivation for the project

So, I wanted to label a few nodes on my Kubernetes cluster, and rather than doing it by hand wanted to do it as a job. The solutions to run kubectl as job that I found were either not updated to the latest release or had a different container for each release, that just makes the life difficult as if I update my K8's to a latest one, I need to remember to also update the job that is running with this solution. Here, I can just not pass any variable to control the client version and be rest assured I will always get the latest one.

⁠Examples

All the files used for showing are present here⁠ These were tested on a minikube cluster, except for the helm chart example that is not yet tested.

⁠K8's job to label nodes

  • Get the YAML to label all nodes in your cluster from here⁠
  • Run the following command
$ kubectl apply -f https://raw.githubusercontent.com/tapas-sharma/kubectl-k8s/master/example/job-labelNodes.yaml
  • This will create a job in the kube-system namespace.
$ kubectl -n kube-system get jobs
NAME          COMPLETIONS   DURATION   AGE
labelnode     1/1           13s        32m
  • This job will label your nodes with a label tapas/sharma=muchWow, you can check that using the following command
$ kubectl get nodes --show-labels
NAME       STATUS   ROLES    AGE   VERSION   LABELS
minikube   Ready    master   52m   v1.18.3   ....,tapas/sharma=muchWoW
  • You can also run the unlabel node job to remove this label by running
$ kubectl apply -f https://raw.githubusercontent.com/tapas-sharma/kubectl-k8s/master/example/job-unlabelNodes.yaml

⁠Getting Kubernetes version

  • Here you can use the following command
$ kubectl apply -f https://raw.githubusercontent.com/tapas-sharma/kubectl-k8s/master/example/kubectlversion.yaml
  • To check the version, you need to see the output (read logs) of the pod
$ kubectl logs kubeversion                                                                                                                             
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.6", GitCommit:"dff82dc0de47299ab66c83c626e08b245ab19037", GitTreeState:"clean", BuildDate:"2020-07-15T16:58:53Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
  • Note the client version and server version, they are different as we have not passed the KUBE_VERSION environment variable to the pod, thus forcing it to get the latest kubectl version as of writing this wiki.

⁠Helm integration to run kubectl commands

To integrate with helm, you can refer here⁠ This shows, how to read the K8's version from the helm in-built variables and pass it to the container. This is a modification of label nodes example and can be used as pre-install, post-install or any other hooks by making it as a job/pod etc.

Tag summary

Content type

Image

Digest

Size

16 MB

Last updated

about 6 years ago

docker pull tapassharma/kubectl