Configuring Kubectl to Access a Remote Cluster

1 hour
  • 4 Learning Objectives

About this Hands-on Lab

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 and admin-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!

Additional Resources

Your team has set up a Kubernetes cluster. You need to check the cluster to see what pods are currently running, but you are not in the office. You can use kubectl to interact with the cluster remotely, but first you need to configure your local kubectl with the proper kubeconfig. Your task is to configure kubectl so that you can interact with the cluster remotely, then use it to determine which pods are currently running on the cluster.

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

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?