In this tutorial, you will learn how to install kubectl, helm, terraform, and kubectx on Linux, MacBook, and Windows. We will also guide you on setting up kubens for managing Kubernetes namespaces more efficiently. This guide includes alias setups for easy command usage, making your workflow more productive.

Table of Contents

    Before You Begin

    Check if /usr/local/bin/ is in Your PATH:

    • Run the following command on Linux to check if /usr/local/bin/ is in your PATH
    echo $PATH
    Code language: Bash (bash)
    • If /usr/local/bin/ is not listed, you can use an alternative directory, such as $HOME/bin. You will also need to add this to your PATH:
    mkdir -p $HOME/bin 
    echo 'export PATH=$HOME/bin:$PATH' >> ~/.bashrc # For Bash source ~/.bashrc
    Code language: Bash (bash)

    1. Installing kubectl

    kubectl is the Kubernetes command-line tool that allows you to manage Kubernetes clusters.

    On Linux/MacOS

    Install Kubectl

    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl gnupg
    curl-fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key|sudogpg--dearmor-o/etc/apt/keyrings/kubernetes-apt-keyring.gpg
    sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg
    sudo apt-get update
    sudo apt-get install -y kubectl

     

    cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/
    enabled=1
    gpgcheck=1
    gpgkey=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/repodata/repomd.xml.key
    EOF
    
    sudo yum update
    sudo yum install -y kubectl
    brew install kubectl
    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" 
    chmod +x kubectl 
    sudo mv kubectl /usr/local/bin/
    On Windows:
    1. Download the kubectl binary for Windows from the official Kubernetes website.
    2. Add the binary to your PATH by editing system environment variables.

    2. Installing helm

    helm is a package manager for Kubernetes.

    On Linux:
    1. Download the script to install Helm:
    curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
    bash get-helm-3Code language: Bash (bash)
    On MacBook:
    1. Use Homebrew to install Helm:
    brew install helmCode language: Bash (bash)
    On Windows:
    1. Download the helm binary from the official Helm website.
    2. Extract and add it to your PATH.

    3. Installing terraform

    terraform is used for infrastructure as code to manage your cloud resources.

    On Linux/MacOS

    Install Terraform

    wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
    echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
    sudo apt update && sudo apt install terraform
    sudo yum install -y yum-utils
    sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
    sudo yum -y install terraform
    sudo yum install -y yum-utils shadow-utils
    sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
    sudo yum -y install terraform
    brew tap hashicorp/tap
    brew install hashicorp/tap/terraform
    On Windows:

    4. Installing kubectx and kubens

    kubectx and kubens are tools to easily switch between Kubernetes contexts and namespaces.

    On Linux and MacBook:
    • Install via brew:
    brew install kubectx
    • Using apt on Debian/ubuntu
    sudo apt install kubectxCode language: Bash (bash)
    On Windows:

    5. Setting Up Aliases

    Creating aliases will allow you to use short commands for kubectx and kubens.

    On Linux and MacBook:
    • Open your shell configuration file (.bashrc, .zshrc, etc.).
    • Add the following aliases: alias kx='kubectx' alias kens='kubens'
    • Source the file to apply changes: source ~/.bashrc # Or source ~/.zshrc
    On Windows (PowerShell):
    • Open your PowerShell profile ($PROFILE).
    • Add the following aliases:
    Set-Alias kx kubectx
    Set-Alias kens kubensCode language: PowerShell (powershell)

    6. kubectx and kubens Usage Tutorial

    Once you have kubectx and kubens installed, you can easily switch between Kubernetes clusters and namespaces.

    Switching Contexts with kubectx:
    • List all available contexts: kubectx
    • Switch to a different context: kx my-context
    Switching Namespaces with kubens:
    • List all namespaces in the current context: kubens
    • Switch to a different namespace: kens my-namespace

    Conclusion

    You have now successfully installed kubectl, helm, terraform, kubectx, and kubens on your Linux, MacBook, or Windows machine. Additionally, setting up aliases like kx for kubectx and kens for kubens can save time when managing your Kubernetes clusters and namespaces.

    Leave a Response

    share on: