Chapter 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, although you can also use an operator node. 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 operator node.

3.1 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.

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 kube-flannel-ds-2sjbx 1/1 Running 0 23h kube-flannel-ds-njg9r 1/1 Running 0 23h kube-proxy-m2rt2 1/1 Running 0 23h kube-proxy-tbkxd 1/1 Running 0 23h kube-scheduler-control1.example.com 1/1 Running 0 23h kubernetes-dashboard-7646bf6898-d6x2m 1/1 Running 0 23h

3.2 Setting up kubectl on the Operator Node

Oracle Linux 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 the operator node, and you have multiple environments deployed, you may inadvertently manage an unexpected Kubernetes cluster. If you do want to set up the kubectl command to run it the operator node, you need to configure it.

The kubectl command is not set up by default to connect to Kubernetes from the operator node. To set up the kubectl command on the operator node, create a local copy of the Kubernetes configuration file, and use that to connect to the cluster.

To use the kubectl command as a regular user, perform the following steps on the operator node.

To setup kubectl on an operator node:
  1. Get the Kubernetes configuration and copy it to a local file on the operator node. Use the olcnectl module property get command to get the Kubernetes configuration from the kubecfg property of the kubernetes module. For example:

    olcnectl module property get \
    --environment-name myenvironment \
    --name mycluster \
    --property kubecfg | base64 -d > kubeconfig.yaml
  2. You can use the kubeconfig.yaml file directly when running kubectl commands using the --kubeconfig option. For example:

    kubectl get pods -n kube-system --kubeconfig kubeconfig.yaml
  3. You can also save the Kubernetes configuration so you do not need to use the --kubeconfig option. Create the .kube subdirectory in your home directory:

    mkdir -p $HOME/.kube
  4. Copy the Kubernetes kubeconfig.yaml file to the .kube directory:

    cp kubeconfig.yaml $HOME/.kube/config
  5. 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
  6. 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 kube-flannel-ds-2sjbx 1/1 Running 0 23h kube-flannel-ds-njg9r 1/1 Running 0 23h kube-proxy-m2rt2 1/1 Running 0 23h kube-proxy-tbkxd 1/1 Running 0 23h kube-scheduler-control1.example.com 1/1 Running 0 23h kubernetes-dashboard-7646bf6898-d6x2m 1/1 Running 0 23h