3 Setting up the Kubernetes Command-Line Interface (kubectl)
This chapter describes how to set up the Kubernetes CLI (kubectl
). The
kubectl
command is part of Kubernetes and is used to create and manage the
containerized applications you deploy on the Kubernetes cluster.
The kubectl
utility is a command line tool that interfaces with the
Kubernetes API server to run commands against the Kubernetes cluster. The
kubectl
command is typically run on a control plane node of the
cluster (the recommended option), although you can set up kubectl
access on
an external node that's not in the cluster, if required. The kubectl
utility
effectively grants full administrative rights to the cluster and all nodes in the cluster.
This chapter discusses setting up the kubectl
command to access a
Kubernetes cluster from either a control plane node or an external node (not part of the
Kubernetes cluster).
Setting up kubectl on a Control Plane Node
To set up the kubectl
command on a control plane node,
copy, and paste these commands to a terminal in the home directory on a control plane node:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
echo 'export KUBECONFIG=$HOME/.kube/config' >> $HOME/.bashrc
Verify that you can use the kubectl
command using any
kubectl
command such as:
kubectl get deployments --all-namespaces
The output looks similar to:
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
externalip-validation-system externalip-validation-webhook 1/1 1 1 29m
kube-system coredns 2/2 2 2 30m
kubernetes-dashboard kubernetes-dashboard 1/1 1 1 29m
ocne-modules ocne-module-operator 1/1 1 1 29m
Setting up kubectl on a Non-Cluster Node
Oracle Cloud Native Environment lets you create many environments from the
operator node. With this in mind, we recommend that you use kubectl
on a
control plane node in the Kubernetes cluster. If you use kubectl
from outside
the cluster, and you have many environments deployed, you might inadvertently manage an
unexpected Kubernetes cluster. However, if you need to set up kubectl
to run
from outside the cluster, you need to configure it.
The following example shows you how to set up a host that's not in the cluster with
kubectl
to access to a Kubernetes cluster.
Note:
The following example assumes the OS of the node is Oracle Linux. However, you can also set
up kubectl
on macOS and Microsoft Windows hosts by leveraging the
Kubernetes community package. For Microsoft Windows hosts you also need to install Windows
Subsystem for Linux 2 (WLS 2).
To set up kubectl
on a host that's not in the cluster:
-
On the operator node, use the
olcnectl module property get
command to get the Kubernetes configuration file for the cluster:olcnectl module property get \ --environment-name myenvironment \ --name mycluster \ --property kubecfg | base64 -d > kubeconfig.yaml
A file named
kubeconfig.yaml
is created that contains the Kubernetes configuration information required to access the cluster. -
Set up the Kubernetes file on the host. Log in to the host and copy the
kubeconfig.yaml
from the operator node to a local directory on the host.Caution:
Follow security best practices when copying a configuration file with sensitive information between hosts.
-
Create a subdirectory named
.kube
in the home directory:mkdir -p $HOME/.kube
-
Copy the
kubeconfig.yaml
file to the.kube
directory:cp /path_to_file/kubeconfig.yaml $HOME/.kube/config
-
Export the path to the file for the
KUBECONFIG
environment variable:export KUBECONFIG=$HOME/.kube/config
-
To permanently set this environment variable, add it to the
.bashrc
file:echo 'export KUBECONFIG=$HOME/.kube/config' >> $HOME/.bashrc
-
-
Install
kubectl
on the host.Set up the node with the required access to Oracle Cloud Native Environment packages by enabling repositories or channels as required. See Installation for more information.
Installkubectl
:sudo dnf install kubectl
-
Verify you can use the
kubectl
command:kubectl get deployments --all-namespaces
The output looks similar to:
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE externalip-validation-system externalip-validation-webhook 1/1 1 1 29m kube-system coredns 2/2 2 2 30m kubernetes-dashboard kubernetes-dashboard 1/1 1 1 29m ocne-modules ocne-module-operator 1/1 1 1 29m