Configuring Kubectl to Access a Remote Cluster

Hands-On Lab

 

Photo of Will Boyd

Will Boyd

DevOps Team Lead in Content

Length

01:00:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Configuring Kubectl to Access a Remote Cluster

Introduction

In this lab, we'll configure kubectl on one machine to interact with a Kubernetes cluster on another machine. After completing this lab, you will have hands-on experience in configuring kubectl to connect to a cluster remotely.

We’ll use the Workspace server to represent your local workstation. This is the server you will need to log in to so you can configure kubectl. Kubectl is already installed, so you do not need to install it manually.

Open a terminal window and log in via SSH to the Workspace server using the public IP and credentials provided on the lab page.

Set the kubectl Cluster Data

First, set KUBERNETES_PUBLIC_ADDRESS to the public IP of the Controller server on the lab page:

KUBERNETES_PUBLIC_ADDRESS=<Controller public IP>

Then, set the cluster data with this:

kubectl config set-cluster kubernetes-the-hard-way 
  --certificate-authority=ca.pem 
  --embed-certs=true 
  --server=https://${KUBERNETES_PUBLIC_ADDRESS}:6443

You should get the following response:

Cluster "kubernetes-the-hard-way" set.

Set the Credentials for kubectl

The credentials are the certificate and key files admin.pem and admin-key.pem. These can be found in the home directory on the Workspace server.

Set them up for kubectl like this:

kubectl config set-credentials admin 
 --client-certificate=admin.pem 
 --client-key=admin-key.pem

You should get the following response:

User "admin" set.

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

You should get the following response:

Context "kubernetes-the-hard-way" created.

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

You should get the following response:

Switched to context "kubernetes-the-hard-way".

Verify 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 kubectl is set up correctly to access the cluster!

Conclusion

Congratulations on completing this lab!