Kubectl is a powerful tool for managing a Kubernetes cluster. While kubectl can be used from one of the servers that makes up your cluster, it can also be used from your local command line to manage the cluster remotely. In this learning activity, you will configure kubectl on one machine to interact with a Kubernetes cluster on another machine. After completing this activity, you will have hands-on experience in configuring kubectl to connect to a cluster remotely.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Set the kubectl cluster data.
You can set the cluster data like this. Be sure to set
KUBERNETES_PUBLIC_ADDRESS
to the public IP of your controller.KUBERNETES_PUBLIC_ADDRESS=<Controller public IP> kubectl config set-cluster kubernetes-the-hard-way --certificate-authority=ca.pem --embed-certs=true --server=https://${KUBERNETES_PUBLIC_ADDRESS}:6443
- Set the credentials for kubectl.
The credentials are the certificate and key files
admin.pem
andadmin-key.pem
. These can be found in the home directory on the workspace server. You can set them up for kubectl like this:kubectl config set-credentials admin --client-certificate=admin.pem --client-key=admin-key.pem
- Set the context for the cluster.
Set the context like this:
kubectl config set-context kubernetes-the-hard-way --cluster=kubernetes-the-hard-way --user=admin
- Use the new kubectl context to check which pods are currently running on the cluster.
Configure kubectl to use the new context like this:
kubectl config use-context kubernetes-the-hard-way
Verify that everything is working with:
kubectl get pods
Since there are no pods right now, you should get the following response:
No resources found.
This indicates that kubectl is set up correctly to access the cluster!