Install Kubernetes with kubespray

Step-by-step instructions

Page content

Howto: installing Kubernetes using Kubespray, including setting up the environment, configuring the inventory, and running the Ansible playbooks.

workers are sparying the cube in the old factory building

Prerequisites

Hardware and Software Requirements

  • Ensure you have multiple machines (at least 3 nodes: 1 master and 2 workers) with the following specifications:
    • Ubuntu 16.04/18.04 or CentOS 7 installed.
    • At least 2 GB of RAM per machine (more RAM is recommended for larger clusters).
    • Network connectivity and SSH access from the Kubespray control machine to each node.
    • For high availability, consider nodes with 16 GB RAM, 100 GB disk, and 6 CPU cores.

Control Machine Setup

  • Install Git if not already present:

    sudo apt install git-all
    
  • Install Python (2.7 or 3.5+) and pip:

    sudo apt update
    sudo apt install python3 python3-pip
    
  • Install Ansible and other dependencies. Kubespray provides a script to handle this:

    git clone https://github.com/kubernetes-sigs/kubespray.git
    cd kubespray
    git checkout release-2.20  # Optional: Check out a stable release
    python3 -m venv venv  # Create a Python virtual environment
    source venv/bin/activate  # Activate the virtual environment
    pip install -r requirements.txt  # Install Ansible and other dependencies
    

Configure the Inventory

  • Copy the example inventory:

    cp -r inventory/sample inventory/mycluster
    
  • Edit the inventory/mycluster/hosts.ini file to add your nodes:

    [all]
    node1 ansible_host=10.10.1.3 ansible_user=ubuntu
    node2 ansible_host=10.10.1.4 ansible_user=ubuntu
    node3 ansible_host=10.10.1.5 ansible_user=ubuntu
    
    [kube-master]
    node1
    
    [kube-node]
    node2
    node3
    
  • If you have a different user, replace ubuntu with your user.

  • You can also use the inventory_builder script to generate the inventory file:

    declare -a IPS=(10.10.1.3 10.10.1.4 10.10.1.5)
    HOST_PREFIX=dcm-cp- KUBE_CONTROL_HOSTS=3 CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}
    

Customize Configuration

  • Customize the Kubernetes cluster settings in the inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml file. For example:

    kube_network_plugin: flannel
    kubeconfig_localhost: true
    
  • For high availability, configure a VIP and load balancer:

    # In inventory/mycluster/group_vars/all/all.yml
    kube_vip_arp_enabled: true
    kube_vip_enabled: true
    kube_vip_controlplane_enabled: true
    kube_vip_address: 10.7.12.250  # Update with correct VIP
    kube_vip_interface: eno1  # Update with correct interface
    

Deploy the Cluster

  • Run the Ansible playbook to deploy Kubernetes:
    ansible-playbook -i inventory/mycluster/hosts.yaml cluster.yml -b -v --become --become-user=root --private-key=~/.ssh/private_key
    

Post-Deployment

  • Once the deployment is complete, you will find a kubeconfig file in the inventory/mycluster/artifacts directory. This file is necessary to interact with your Kubernetes cluster using kubectl.
    export KUBECONFIG=inventory/mycluster/artifacts/admin.conf
    kubectl get nodes
    

Troubleshooting and Access

  • To troubleshoot or access your cluster, ensure you have the kubectl binary installed on your control machine or any machine where you plan to manage the cluster from.
  • If you encounter issues like worker nodes not being ready or other pod scheduling problems, check the logs and adjust configurations as necessary.