How Do You Install kubectl on a CentOS/RHEL/Fedora Linux server?

Problem scenario
You want the kubectl command to work on a server running a Red Hat distribution of Linux.  How do you install kubectl on a CentOS/RHEL/Fedora Linux server?

Solution
#1  This assumes that you have access to the internet.  You can do it with these three commands:

cd /tmp

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl

sudo mv kubectl /usr/local/bin/

(An alternative way with a .repo file that is more secure would be to use steps #1 through #3 of How Do You Install Kubernetes on an AWS Instance of RedHat Linux? and then run "sudo yum -y install kubectl". )

#2  For kubectl commands to work, you will need a .kube directory in your home directory.  You may want to run this: mkdir ~/.kube

#3  Inside this .kube directory, you will need a file following this naming convention: config-foobar
Replace foobar with the name of your cluster.

#4  The contents of this config-foobar file should be something like this if you are using AWS:

apiVersion: v1
clusters:
- cluster:
    server: <endpoint-url>
    certificate-authority-data: <base64-encoded-ca-cert>
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: aws
  name: aws
current-context: aws
kind: Config
preferences: {}
users:
- name: aws
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      command: aws-iam-authenticator
      args:
        - "token"
        - "-i"
        - "<cluster-name>"
         - "-r"
         - "<role-arn>"

#5  If you are not using AWS, you will need to modify the above template to the best of your ability.  If you are using AWS, follow these four steps (each quoted from https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html ):

"Replace the <endpoint-url> with the endpoint URL that was created for your cluster."  (It will appear under the "API server endpoint" value.)

"Replace the <base64-encoded-ca-cert> with the certificateAuthority.data that was created for your cluster."  (It will appear under the "Certificate Authority" value.)

"Replace the <cluster-name> with your cluster name."

Replace the <role-arn> with the value for the "Role ARN" as the web UI shows.

#6  You will need to run this command (but replace foobar with the name of your cluster):

export KUBECONFIG=$KUBECONFIG:~/.kube/config-foobar

(You may want to do this: "cd ~/.kube" and run "ls -lh".)

You may want to see these directions too.

Leave a comment

Your email address will not be published. Required fields are marked *