Configuring kubectl for Remote Access

In this lab you will generate a kubeconfig file for the kubectl command line utility based on the admin user credentials.

Run the commands in this lab from the same directory used to generate the admin client certificates.

The Admin Kubernetes Configuration File

Each kubeconfig requires a Kubernetes API Server to connect to. To support high availability the IP address assigned to the external load balancer fronting the Kubernetes API Servers will be used.

Generate a kubeconfig file suitable for authenticating as the admin user:

  1. {
  2. KUBERNETES_PUBLIC_ADDRESS=$(gcloud compute addresses describe kubernetes-the-hard-way \
  3. --region $(gcloud config get-value compute/region) \
  4. --format 'value(address)')
  5. kubectl config set-cluster kubernetes-the-hard-way \
  6. --certificate-authority=ca.pem \
  7. --embed-certs=true \
  8. --server=https://${KUBERNETES_PUBLIC_ADDRESS}:6443
  9. kubectl config set-credentials admin \
  10. --client-certificate=admin.pem \
  11. --client-key=admin-key.pem
  12. kubectl config set-context kubernetes-the-hard-way \
  13. --cluster=kubernetes-the-hard-way \
  14. --user=admin
  15. kubectl config use-context kubernetes-the-hard-way
  16. }

Verification

Check the version of the remote Kubernetes cluster:

  1. kubectl version

output

  1. Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"clean", BuildDate:"2021-04-08T16:31:21Z", GoVersion:"go1.16.1", Compiler:"gc", Platform:"linux/amd64"}
  2. Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"clean", BuildDate:"2021-04-08T16:25:06Z", GoVersion:"go1.16.1", Compiler:"gc", Platform:"linux/amd64"}

List the nodes in the remote Kubernetes cluster:

  1. kubectl get nodes

output

  1. NAME STATUS ROLES AGE VERSION
  2. worker-0 Ready <none> 2m35s v1.21.0
  3. worker-1 Ready <none> 2m35s v1.21.0
  4. worker-2 Ready <none> 2m35s v1.21.0

Next: Provisioning Pod Network Routes