3 Setting up the Kubernetes Command-Line Interface (kubectl)
This chapter describes how to set up the Kubernetes Command-Line
Interface (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 the control plane node of the
cluster (the recommended option), although you can set up kubectl
access on
an external non-cluster node if required. The kubectl
utility effectively
grants full administrative rights to the cluster and all of the 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 your 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 pods --all-namespaces
The output looks similar to:
NAMESPACE NAME READY STATUS RESTARTS AGE
externalip-validat... externalip-validation-... 1/1 Running 0 1h
kube-system coredns-... 1/1 Running 0 1h
kube-system coredns-... 1/1 Running 0 1h
kube-system etcd-... 1/1 Running 2 1h
kube-system etcd-... 1/1 Running 2 1h
kube-system kube-apiserver-... 1/1 Running 2 1h
kube-system kube-apiserver-... 1/1 Running 2 1h
kube-system kube-controller-manager-... 1/1 Running 5 (1h ago) 1h
kube-system kube-controller-manager-... 1/1 Running 2 1h
kube-system kube-flannel-... 1/1 Running 0 1h
kube-system kube-flannel-... 1/1 Running 0 1h
kube-system kube-flannel-... 1/1 Running 0 1h
kube-system kube-proxy-... 1/1 Running 0 1h
kube-system kube-proxy-... 1/1 Running 0 1h
kube-system kube-proxy-... 1/1 Running 0 1h
kube-system kube-scheduler-... 1/1 Running 5 (1h ago) 1h
kube-system kube-scheduler-... 1/1 Running 2 1h
kubernetes-dashboard kubernetes-dashboard-... 1/1 Running 0 1h
Setting up kubectl on a Non-Cluster Node
Oracle Cloud Native Environment allows you to create multiple environments from the operator
node. With this in mind, it is recommended that you use the kubectl
command
on a control plane node in the Kubernetes cluster. If you use the kubectl
command from outside the cluster, and you have multiple environments deployed, you may
inadvertently manage an unexpected Kubernetes cluster. However, if you need to set up the
kubectl
command to run from outside the cluster, you need to configure
it.
The following example shows you how to set up a non-cluster host with
kubectl
command-line access to a Kubernetes cluster.
Note:
The following example assumes the operating system of the non-cluster 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 would also
need to install Windows Subsystem for Linux 2 (WLS 2).
-
Create a copy of the Kubernetes configuration file.
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 non-cluster node:
-
Log onto the non-cluster node.
-
Copy the
kubeconfig.yaml
from the operator node to a local directory on the non-cluster node.Caution:
It is important to follow security best practices when copying a configuration file with sensitive information between hosts.
-
Create a subdirectory named
.kube
in your 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 your
.bashrc
file:echo 'export KUBECONFIG=$HOME/.kube/config' >> $HOME/.bashrc
-
-
Install
kubectl
on the non-cluster node:-
Set up the node with the required access to Oracle Cloud Native Environment packages by enabling repositories or channels as required. See Getting Started for more information.
-
Install
kubectl
by running the following command:sudo dnf install kubectl
-
-
Verify
kubectl
access from the non-cluster node:Verify that you can use the
kubectl
command:kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-5bc65d7f4b-qzfcc 1/1 Running 0 23h coredns-5bc65d7f4b-z64f2 1/1 Running 0 23h etcd-control1.example.com 1/1 Running 0 23h kube-apiserver-control1.example.com 1/1 Running 0 23h kube-controller-control1.example.com 1/1 Running 0 23h ...