Simple KubernetesCluster Setup with Kubespray

Simple  KubernetesCluster  Setup with Kubespray

Introduction

Embark on a journey to effortlessly set up and manage your Kubernetes cluster with Kubespray, your backstage pass to a simplified deployment experience. This open-source project streamlines the creation and handling of Kubernetes clusters, making the process a breeze.

Kubespray relies on Ansible, acting as your choreographer with clear instructions and an extensive library. Picture it guiding the dance of configurations, seamlessly orchestrating everything from the control stage to the worker nodes.

This setup serves as my go-to when installing Kubernetes in my local environment, creating a playground for experimentation.

Pre-requisites

Before diving in, ensure the basics are covered. Picture four performers, VirtualBox or VMware VMs running Ubuntu 20.04, behind the scenes. Three are the core of our Kubernetes cluster – one master node and two worker nodes. The Ansible controller, residing on a MacBook, needs SSH access to these virtual machines, in my case below i have a user "kato"

Playbook 1: Update Servers

# 01-update-servers.yaml

- hosts: all
  remote_user: kato
  become: yes
  tasks:
    - name: "Update Repository cache"
      apt:

Playbook 2: Install Required Packages

# 02-install-requirements.yaml

- hosts: all
  remote_user: kato
  become: yes
  tasks:
    - name: Install required packages
      apt:
        name:
          - htop
          - byobu
          - net-tools
          - ebtables
          - ipset
          - open-iscsi
          - conntrack
          - socat
        state: present

Playbook 3: Disable Swap

# 03-disable-swap.yaml

- hosts: all
  become: yes
  remote_user: kato
  gather_facts: no
  tasks:
    - name: "Disable swap"
      shell: |
        swapoff -a
        sed -i '/ swap / s/^\\(.*\\)$/#\\1/g' /etc/fstab

Playbook 4: Download Kubekey and Create Config

# 04-download-kubekey.yaml

- hosts: master
  become: yes
  remote_user: kato
  gather_facts: no
  tasks: 
    - name: "Download kubekey"
      shell: |
        curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.2 sh -
        chmod +x kk
        ./kk create config --with-kubernetes v1.23.10

Playbook 5: Copy Config File and Install Kubernetes Cluster

# 05-install-kubernetes.yaml

- hosts: master
  remote_user: kato
  become: yes
  gather_facts: no
  tasks:
    - name: Copying configuration file to server
      become: true
      copy:
        src: /Users/bruno/infra/vm-boxs/config-sample.yaml
        dest: ~/
        owner: kato
        group: sudo
    - name: "Install k8 cluster"
      shell: |
        ./kk create cluster -f ~/config-sample.yaml -y

Config File: config-sample.yaml

apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
  name: sample
spec:
  hosts:
  - {name: master1, address: 192.168.20.71, internalAddress: 192.168.20.71, user: kato, password: " "  }
  - {name: node1, address: 192.168.20.72, internalAddress: 192.168.20.72, user: kato, password: " "}
  - {name: node2, address: 192.168.20.73, internalAddress: 192.168.20.73, user: kato, password: " "}
  roleGroups:
    etcd:
    - master1
    control-plane:
    - master1
    worker:
    - node1
    - node2
  controlPlaneEndpoint:
    ## Internal loadbalancer for apiservers 
    # internalLoadbalancer: haproxy

    domain: lb.kubesphere.local
    address: ""
    port: 6443
  kubernetes:
    version: v1.23.10
    clusterName: cluster.local
    autoRenewCerts: true
    containerManager: docker
  etcd:
    type: kubekey
  network:
    plugin: calico
    kubePodsCIDR: 10.233.64.0/18
    kubeServiceCIDR: 10.233.0.0/18
    ## multus support. <https://github.com/k8snetworkplumbingwg/multus-cni>
    multusCNI:
      enabled: false
  registry:
    privateRegistry: ""
    namespaceOverride: ""
    registryMirrors: []
    insecureRegistries: []
  addons: []

Combined Playbooks

# all-playbooks.yaml

- name: Update nodes
  import_playbook: 01-update-servers.yaml

- name: Install required modules
  import_playbook: 02-install-requirements.yaml

- name: Disable swap on all nodes
  import_playbook: 03-disable-swap.yaml

- name: Download & setup kubekey
  import_playbook: 04-download-kubekey.yaml

- name: Install k8
  import_playbook: 05-install-kubernetes.yaml

Conclusion

In conclusion, Kubespray emerges as a remarkably user-friendly tool, streamlining the setup and management of Kubernetes clusters. By harnessing the power of Ansible, it not only simplifies the deployment process but also transforms what could be a complex task into a seamless and efficient experience. Notably, the setup of the clusters, accomplished in a mere 5-7 minutes, underscores the speed and effectiveness of this tool. This guide, tailored for local environment setup, stands as a concise playbook, providing a swift and accessible entry point for your Kubernetes orchestration journey.

To explore the provided playbooks, visit my GitHub repository

Gatete-Bruno/K8s-Install-Kubespray