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-namespacesThe 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 29mSetting 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 getcommand to get the Kubernetes configuration file for the cluster:olcnectl module property get \ --environment-name myenvironment \ --name mycluster \ --property kubecfg | base64 -d > kubeconfig.yamlA file named
kubeconfig.yamlis 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.yamlfrom 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
.kubein the home directory:mkdir -p $HOME/.kube -
Copy the
kubeconfig.yamlfile to the.kubedirectory:cp /path_to_file/kubeconfig.yaml $HOME/.kube/config -
Export the path to the file for the
KUBECONFIGenvironment variable:export KUBECONFIG=$HOME/.kube/config -
To permanently set this environment variable, add it to the
.bashrcfile:echo 'export KUBECONFIG=$HOME/.kube/config' >> $HOME/.bashrc
-
-
Install
kubectlon 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
kubectlcommand:kubectl get deployments --all-namespacesThe 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