How to Setup Kubernetes Cluster with Kubespray

How to Setup Kubernetes Cluster with Kubespray

Introduction

Kubespray is open source automation tool to deploy kubernetes cluster, use ansible for deployment kubernetes packages, can use in any environment (on-premise/cloud).

Prerequisites

Linux machine to deploy your kubernetes cluster.

For this tutorial, i want deploy kubernetes cluster with 3 machine, named:

  • kube-cp
  • kube-node1
  • kube-node2

I deploy use deployer machine, you can use your laptop/pc (but makesure the internet connection is reliable with all machine)

Requirements package on deployer/laptop/PC

  • Git
  • Python
  • PIP
  • Ansible

Setup Kubespray

Install git on your deployer machine

apt-get install git -y

Clone kubespray repository

https://github.com/kubernetes-sigs/kubespray.git
cd kubespray
git checkout release-2.28

Install requirements package , Use python venv.

apt-get install python3.10-venv -y
python3 -m venv kubespray-venv
source kubespray-venv/bin/activate
pip3 install -r requirements.txt

Copy sample kubespray configuration, i copied as directory cluster-arya

cp -r inventory{sample,cluster-arya}

Add your machine to inventory.ini

vim inventory/cluster-arya/inventory.ini
[kube_control_plane]
arya-cp ansible_host=54.242.81.x ip=172.31.19.230

[etcd:children]
kube_control_plane

[kube_node]
arya-node1 ansible_host=18.234.81.x ip=172.31.19.223
arya-node2 ansible_host=3.90.212.x ip=172.31.17.64

If your machine is under NAT, you need define private machine ip in ip= section

Change other configuration in inventory/cluster-arya/k8s_cluster

inventory/cluster-arya/group_vars/
├── all
│   ├── all.yml
│   ├── aws.yml
│   ├── azure.yml
│   ├── containerd.yml
│   ├── coreos.yml
│   ├── cri-o.yml
│   ├── docker.yml
│   ├── etcd.yml
│   ├── gcp.yml
│   ├── hcloud.yml
│   ├── huaweicloud.yml
│   ├── oci.yml
│   ├── offline.yml
│   ├── openstack.yml
│   ├── upcloud.yml
│   └── vsphere.yml
└── k8s_cluster
    ├── addons.yml
    ├── k8s-cluster.yml
    ├── k8s-net-calico.yml
    ├── k8s-net-cilium.yml
    ├── k8s-net-custom-cni.yml
    ├── k8s-net-flannel.yml
    ├── k8s-net-kube-ovn.yml
    ├── k8s-net-kube-router.yml
    ├── k8s-net-macvlan.yml
    └── kube_control_plane.yml

For example , change network plugin used cilium

vim inventory/cluster-arya/group_vars/k8s_cluster/k8s-cluster.yml
# Choose network plugin (cilium, calico, kube-ovn or flannel. Use cni for generic cni plugin)
# Can also be set to 'cloud', which lets the cloud provider setup appropriate routing
kube_network_plugin: cilium

# Setting multi_networking to true will install Multus: https://github.com/k8snetworkplumbingwg/multus-cni
kube_network_plugin_multus: false

# Kubernetes internal network for services, unused block of space.
kube_service_addresses: 10.233.0.0/18

# internal network. When used, it will assign IP
# addresses from this range to individual pods.
# This network must be unused in your network infrastructure!
kube_pods_subnet: 10.233.64.0/18

Deploy Kubernetes cluster using Kubespray

Run kubespray playbook

ansible-playbook -i inventory/cluster-arya/ cluster.yml -b --user=ubuntu -v --private-key=~/.ssh/id_ed25519

Wait until ansible-playbook finished deploy cluster

PLAY RECAP ************************************************************************************************************************************************
arya-cp                    : ok=561  changed=38   unreachable=0    failed=0    skipped=905  rescued=0    ignored=4
arya-node1                 : ok=467  changed=22   unreachable=0    failed=0    skipped=698  rescued=0    ignored=1
arya-node2                 : ok=423  changed=18   unreachable=0    failed=0    skipped=615  rescued=0    ignored=1

And check kubernetes cluster by accesing control plane machine

root@arya-cp:~# kubectl get nodes
NAME         STATUS   ROLES           AGE   VERSION
arya-cp      Ready    control-plane   32m   v1.33.2
arya-node1   Ready    <none>          31m   v1.33.2
arya-node2   Ready    <none>          31m   v1.33.2

Congrats! You have successfully set up and accessed a kubernetes cluster using kubespray.