You are browsing a read-only backup copy of Wikitech. The primary site can be found at wikitech.wikimedia.org

Difference between revisions of "Kubernetes/Kubectl"

From Wikitech-static
Jump to navigation Jump to search
imported>Jelto
imported>Jelto
(add kubectl cheat sheet)
Line 1: Line 1:
<code>kubectl</code> is the cli tool used to access the Kubernetes cluster. For tasks like doing deployments <code>kubectl</code> should not be used. Instead please refere to [[helm]] and [[Kubernetes/Deployments#Deploying with helmfile|helmfile]].
<code>kubectl</code> is the cli tool used to access the Kubernetes cluster. For tasks like doing deployments <code>kubectl</code> should not be used. Instead please refere to [[helm]] and [[Kubernetes/Deployments#Deploying with helmfile|helmfile]].


<code>kubectl</code> can be used to view and troubleshoot cluster components and to do certain maintenance tasks.{{Kubernetes nav}}
<code>kubectl</code> can be used to view and troubleshoot cluster components and to do certain maintenance tasks. See also the [https://kubernetes.io/docs/reference/kubectl/cheatsheet/ kubectl cheatsheet/].{{Kubernetes nav}}
== Setting KUBECONFIG ==
== Setting KUBECONFIG ==
<code>kubectl</code> needs to know how to connect to the cluster and what credentials to use. This information can be stored in the kubeconfig file. By default <code>kubectl</code> uses a variable called <code>KUBECONFIG</code> to find this kubeconfig file. If the variable is not set, it looks in <code>~/.kube/config</code>.<ref>https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable</ref>
<code>kubectl</code> needs to know how to connect to the cluster and what credentials to use. This information can be stored in the kubeconfig file. By default <code>kubectl</code> uses a variable called <code>KUBECONFIG</code> to find this kubeconfig file. If the variable is not set, it looks in <code>~/.kube/config</code>.<ref>https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable</ref>

Revision as of 15:48, 19 September 2021

kubectl is the cli tool used to access the Kubernetes cluster. For tasks like doing deployments kubectl should not be used. Instead please refere to helm and helmfile.

kubectl can be used to view and troubleshoot cluster components and to do certain maintenance tasks. See also the kubectl cheatsheet/.

Setting KUBECONFIG

kubectl needs to know how to connect to the cluster and what credentials to use. This information can be stored in the kubeconfig file. By default kubectl uses a variable called KUBECONFIG to find this kubeconfig file. If the variable is not set, it looks in ~/.kube/config.[1]

In order to have kubectl working with your service the KUBECONFIG variable has to be set. This is of the form:

KUBECONFIG="/etc/kubernetes/${SERVICE}-${CLUSTER}.config"

Running kubectl Commands

For example to get a list of pods for a service:

KUBECONFIG="/etc/kubernetes/termbox-staging.config" kubectl get pods

Using kube_env

The tool kube_env (present on deploy hosts) simplifies the configuration of kubectl and the access to different clusters and services. Use the following command to setup the KUBECONFIG variable (and many other useful variables like namespace and helm variables):

kube_env $SERVICE $CLUSTER

Then you can use kubectl to access the service:

kubectl get pods