Ansible ile Kubernetes Multi Control Plane Kurulumu (Kubespray)

3 dakika okuma

Bu kılavuz, Kubespray kullanarak Yüksek Kullanılabilirlik (HA) ile bir Kubernetes cluster’ının nasıl kurulacağını açıklar. Cluster 3 control plane node, 2 worker node ve 1 harici load balancer’dan oluşur.

Kullanılan sürümler:

  • Kubernetes v1.33.7
  • Ubuntu 22.04 LTS
  • CRI containerd

1. Altyapı Hazırlığı

Minimum Spesifikasyonlar

NodeCPURAM
Ansible + Kubespray2 vCPU4 GB
Load Balancer API2 vCPU4 GB
Control Plane (x3)2 vCPU4 GB
Worker (x3)2 vCPU8 GB

Genel Önkoşullar

  • Ubuntu Server 22.04 LTS
  • Swap kalıcı olarak devre dışı
  • NOPASSWD ile sudo kullanıcısı
  • Tüm node’lar bağlı (iç portlar açık)
  • Tüm node’larda Python3 mevcut

2. Topoloji & IP Adresi

NameIPOS
Ansible192.168.100.12Ubuntu 22.04
LB-master172.16.21.154Ubuntu 22.04
k8s-master-node-1172.16.21.132Ubuntu 22.04
k8s-master-node-2172.16.21.244Ubuntu 22.04
k8s-master-node-3172.16.21.178Ubuntu 22.04
k8s-worker-1172.16.21.202Ubuntu 22.04
k8s-worker-2172.16.21.231Ubuntu 22.04

3. Load Balancer Yapılandırması (NGINX TCP)

apt update -y
apt install nginx -y

/etc/nginx/nginx.conf dosyasını düzenleyin ve alt kısma ekleyin:

stream {
  upstream k8s_api {
    least_conn;
    server 172.16.21.132:6443;
    server 172.16.21.244:6443;
    server 172.16.21.178:6443;
  }

  server {
    listen 6443;
    proxy_pass k8s_api;
    proxy_connect_timeout 3s;
    proxy_timeout 10s;
  }
}

NGINX’i yeniden yükleyin:

nginx -t && systemctl reload nginx

4. Tüm Node’lar İçin Temel Yapılandırma (Master & Worker)

Swap’ı devre dışı bırakın:

swapoff -a
sed -i '/ swap / s/^/#/' /etc/fstab

Şifresiz sudo:

visudo

Ekleyin:

ubuntu ALL=(ALL) NOPASSWD:ALL

5. SSH Ansible Kurulumu

ssh-keygen
for ip in 154 132 244 178 202 231; do
  ssh-copy-id [email protected].$ip
done

6. Kubespray Kurulumu

git clone https://github.com/kubernetes-sigs/kubespray.git
cd kubespray
git checkout release-2.29
git checkout release-2.25 contrib/

Bağımlılıkları yükleyin:

sudo apt install -y python3-pip
pip3 install -r requirements.txt

7. Inventory Oluşturma

cp -rfp inventory/sample inventory/mycluster
declare -a IPS=(172.16.21.132 172.16.21.244 172.16.21.178 172.16.21.202 172.16.21.231)
CONFIG_FILE=inventory/mycluster/hosts.yml python3 contrib/inventory_builder/inventory.py ${IPS[@]}

inventory/mycluster/hosts.yml dosyasını düzenleyin ve doğru gruplamayı sağlayın:

  • kube_control_plane: master-1,2,3
  • etcd: master-1,2,3
  • kube_node: worker-1,2,3
all:
  hosts:
  k8s-master-node-1:
    ansible_host: 172.16.21.132
    ip: 172.16.21.132
    access_ip: 172.16.21.132
  k8s-master-node-2:
    ansible_host: 172.16.21.244
    ip: 172.16.21.244
    access_ip: 172.16.21.244
  k8s-master-node-3:
    ansible_host: 172.16.21.178
    ip: 172.16.21.178
    access_ip: 172.16.21.178
  k8s-worker-node-1:
    ansible_host: 172.16.21.202
    ip: 172.16.21.202
    access_ip: 172.16.21.202
  k8s-worker-node-2:
    ansible_host: 172.16.21.231
    ip: 172.16.21.231
    access_ip: 172.16.21.231
  
  children:
  kube_control_plane:
    hosts:
    k8s-master-node-1:
    k8s-master-node-2:
    k8s-master-node-3:
  
  kube_node:
    hosts:
    k8s-worker-node-1:
    k8s-worker-node-2:
  
  etcd:
    hosts:
    k8s-master-node-1:
    k8s-master-node-2:
    k8s-master-node-3:
  
  k8s_cluster:
    children:
    kube_control_plane:
    kube_node:
  
  calico_rr:
    hosts: {}

8. Kubernetes & Containerd Yapılandırması

Düzenleyin:

inventory/mycluster/group_vars/k8s-cluster/k8s-cluster.yml

Emin olun:

container_manager: containerd
kube_version: v1.35.0

9. Harici API Load Balancer Yapılandırması

Düzenleyin:

inventory/mycluster/group_vars/all/all.yml
apiserver_loadbalancer_domain_name: "k8s-api.example.local"

loadbalancer_apiserver:
  address: 172.16.21.154
  port: 6443

loadbalancer_apiserver_localhost: false

Ansible node üzerindeki /etc/hosts dosyasına ekleyin:

echo "172.16.21.154 k8s-api.example.local" | sudo tee -a /etc/hosts

10. Cluster’ı Deploy Et

ansible-playbook -i inventory/mycluster/hosts.yml cluster.yml --become

SSH private key özel bir yol kullanıyorsa, aşağıdaki komutu çalıştırın:

ansible-playbook -i inventory/mycluster/hosts.yml cluster.yml --become -u ubuntu -e ansible_ssh_private_key_file=/path/to/custom/key

11. Doğrulama

kubectl get nodes -o wide

12. Containerd Notları

Docker komut değişiklikleri:

  • crictl ps
  • crictl images
  • ctr -n k8s.io containers list

Bonus

1. Cluster’ı sıfırla

Tüm cluster’ı başlangıç durumuna sıfırlayın:

ansible-playbook -i inventory/mycluster/hosts.yml reset.yml --become \
  -u ubuntu \
  -e ansible_ssh_private_key_file=/path/to/custom/key

2. Cluster’ı yeniden deploy et

Sıfırlamadan sonra cluster’ı yeniden deploy edin:

ansible-playbook -i inventory/mycluster/hosts.yml cluster.yml --become \
  -u ubuntu \
  -e ansible_ssh_private_key_file=/path/to/custom/key

3. Cluster’ı ölçeklendir

Mevcut cluster’a yeni node’lar ekleyin:

ansible-playbook -i inventory/mycluster/hosts.yml scale.yml --become \
  -u ubuntu \
  -e ansible_ssh_private_key_file=/path/to/custom/key

4. Node kaldır

Cluster’dan node’ları kaldırın:

ansible-playbook -i inventory/mycluster/hosts.yml remove_node.yml --become \
  -u ubuntu \
  -e ansible_ssh_private_key_file=/path/to/custom/key \
  -e node=node1,node2

5. Cluster’ı yükselt

Kubernetes cluster sürümünü yükseltin:

ansible-playbook -i inventory/mycluster/hosts.yml upgrade_cluster.yml --become \
  -u ubuntu \
  -e ansible_ssh_private_key_file=/path/to/custom/key

6. Control plane’ı kurtar

Başarısız control plane’ı kurtartın:

ansible-playbook -i inventory/mycluster/hosts.yml recover_control_plane.yml --become \
  -u ubuntu \
  -e ansible_ssh_private_key_file=/path/to/custom/key

./EOF

Kubernetes v1.33.7 HA cluster kullanıma hazır.

Bu yazıyı paylaş

Faydalı buldunuz mu? Başkalarıyla paylaşın.

comments powered by Disqus