A detailed guide on creating a 3-master, 3-worker Kubernetes cluster using Ubuntu 22.04 and Docker as the container runtime. This setup utilizes the kubeadm tool for cluster initialization and assumes a static IP address range of 192.168.0.165 - 180 for all nodes.
Preparation:
- Choose Your Nodes: Select six Ubuntu 22.04 machines (three for masters and three for workers). Ensure they have SSH access enabled and meet the minimum system requirements for running Kubernetes (https://kubernetes.io/docs/home/).
- Disable Swap: On all machines, disable swap to avoid potential issues with Kubernetes. You can check if swap is enabled with
swapon -sand disable it permanently with edits to/etc/fstab. Refer to Ubuntu documentation for specific instructions (https://pimylifeup.com/ubuntu-turn-off-swap/). - Update and Upgrade: Update the package lists and upgrade all packages on each node using:
sudo apt update
sudo apt upgrade -y
- Install Docker: Install Docker following the official Ubuntu documentation (https://docs.docker.com/engine/install/ubuntu/). Verify Docker installation with
docker version.
Master Node Configuration (Perform on all three master nodes):
- Install kubeadm: Install the
kubeadmpackage used for initializing the Kubernetes cluster:
sudo apt install -y kubeadm kubelet
-
Initialize the Cluster (One Master Only):
Important Note: Perform this step only on one of the master nodes. This node will become the cluster control plane.
Bash# Replace "<master_ip>" with the IP address of the current master node (e.g., 192.168.0.165) sudo kubeadm init --apiserver-advertise-address <master_ip> --pod-network-cidr 10.244.0.0/16This command will generate various outputs, including a kubeadm join configuration token. Keep this information readily available for joining worker nodes later.
-
Configure Flannel Pod Network (Optional):
While
kubeadm initcreates a basic pod network, Flannel is a popular pod networking solution for Kubernetes clusters. You can follow the official Flannel documentation for installation on all nodes (https://github.com/flannel-io/flannel). -
Configure kubelet for non-root users (Optional):
By default,
kubeletruns with root privileges. To improve security, consider configuring it to run as a non-root user. Refer to the official Kubernetes documentation for detailed instructions (https://kubernetes.io/docs/tasks/administer-cluster/kubelet-in-userns/).
Worker Node Configuration (Perform on all three worker nodes):
-
Join the Cluster:
Use the kubeadm join configuration token obtained from the master node initialization to join each worker node to the cluster:
Bash# Replace "<master_ip>" with the IP address of any master node and "<token>" with the actual join token sudo kubeadm join <master_ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash_value>Replace
<hash_value>with the actual hash value provided during the master node initialization output.
Verification:
-
Verify Cluster Status:
On any master node, use the
kubectl get nodescommand to check if all nodes (masters and workers) are registered and in a "Ready" state. -
Test Deployment (Optional):
Once the cluster is up and running, you can deploy a simple pod to verify functionality. Here's an example of deploying an Nginx pod:
Bashkubectl create deployment my-nginx --image=nginx kubectl expose deployment my-nginx --type=LoadBalancer --port=80This will create an Nginx deployment and expose it as a service on port 80. You can then access the Nginx service from any machine on your network by finding the assigned external IP address using
kubectl get service.
Additional Notes:
- This guide provides a basic setup for a Kubernetes cluster. For a production environment, further configuration and security best practices should be implemented.
- Consider using tools like Kubectl RBAC for role-based access control within the cluster.
- Explore Kubernetes documentation for managing deployments, pods, services, and other cluster resources (https://kubernetes.io/docs/home/).
IP Allocation:
Assigning static IPs from the 192.168.0.165 - 180 range to each node:
Static IP Configuration:
- Configure Network Interfaces:
Before proceeding, ensure you know the network interface names on each node (e.g., eth0, ens33). You can identify them using the ip addr command.
On each node, edit the network configuration file (/etc/netplan/00-installer-config.yaml) using a text editor like nano.
Here's an example configuration for the first master node (192.168.0.165):
network:
version: 2
renderer: networkd
ethernets:
<interface_name>: # Replace "<interface_name>" with your actual interface name (e.g., eth0)
addresses:
- 192.168.0.165/24
gateway: 192.168.0.1
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
Replace <interface_name> with the actual network interface name on your system. Adapt the IP address, subnet mask, gateway, and nameserver details according to your network configuration.
Repeat this configuration for all remaining nodes, assigning static IPs within the 192.168.0.166 - 180 range and adjusting the interface name if necessary.
- Apply Network Changes:
After editing the configuration file, apply the changes using:
sudo netplan apply
- Verify Network Connectivity:
Verify if the static IP addresses are applied correctly by running ip addr and checking if the assigned IP addresses are listed for the network interface.
Additionally, ping other nodes using their static IPs to confirm network connectivity between them.
Important Note:
- Ensure your network configuration (gateway, subnet mask, DNS servers) aligns with your actual network setup.
- Assigning static IPs might require additional configuration on your network router or DHCP server to avoid IP conflicts.
With these steps completed, you should have a 3-master, 3-worker Kubernetes cluster set up with static IPs using kubeadm and Docker on Ubuntu 22.04.
Remember, this is a basic configuration. Consider exploring further security best practices and advanced cluster management techniques for production environments.
-------
Ansible playbook with the naming convention:
Inventory File (inventory.yaml):
[masters]
master01.cloud.local ansible_host=192.168.0.165
master02.cloud.local ansible_host=192.168.0.166
master03.cloud.local ansible_host=192.168.0.167
[workers]
worker01.cloud.local ansible_host=192.168.0.168
worker02.cloud.local ansible_host=192.168.0.169
worker03.cloud.local ansible_host=192.168.0.170
[k8s_all:children]
masters
workers
[k8s_all:vars]
k8s_version: "1.24.0" # Update with desired Kubernetes version
Playbook (k8s-cluster.yml):
---
- hosts: k8s_all
become: true
tasks:
- name: Update package lists
apt: update_cache=yes
- name: Upgrade all packages
apt: upgrade=dist
- name: Install Docker pre-requisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
state: present
- name: Install Docker Engine
apt:
name: docker-ce
state: present
- name: Create Docker group
group:
name: docker
state: present
- name: Add current user to Docker group
user:
name: "{{ ansible_user }}"
groups: docker
append: yes
state: present
- name: Enable Docker service
service:
name: docker
enabled: yes
- name: Restart Docker service
service:
name: docker
state: restarted
- name: Install kubeadm, kubelet, and kubectl
apt:
name:
- kubeadm
- kubelet
- kubectl
state: present
- hosts: masters
become: true
tasks:
- name: Initialize Kubernetes cluster (on one master only)
command: kubeadm init --apiserver-advertise-address {{ ansible_host }} --pod-network-cidr 10.244.0.0/16
when: inventory_hostname == "master01.cloud.local" # Execute only on the first master
- name: Configure Flannel Pod Network (Optional)
# Include tasks for Flannel installation here
- name: Get join token (on any master)
shell: kubeadm token list
register: join_token_output
when: inventory_hostname != "master01.cloud.local"
- name: Set fact for join token
set_fact:
join_token: "{{ join_token_output.stdout | regex_replace('^(.*)token ([^ ]+).*$', '\\2') }}"
when: inventory_hostname != "master01.cloud.local"
- hosts: workers
become: true
tasks:
- name: Join the Kubernetes cluster
command: kubeadm join {{ master01.cloud.local }}:6443 --token {{ join_token }} --discovery-token-ca-cert-hash sha256:<hash_value>
when: true
register: join_output
- debug:
msg: "Node {{ inventory_hostname }} joined the cluster with status {{ join_output.stdout }}"
The changes are reflected in the inventory file and playbook:
- Hostnames in the
mastersandworkersgroups now use the `master01.cloud.
No comments:
Post a Comment