Cluster na AWS

Criando as maquinas na AWS

  • Nome: k8s
  • Nº de instâncias: 3
  • OS: Ubuntu Server 22.04 LTS
  • Tipo de instância: t2.medium
  • Key pair: k8s
  • Security group: k8s
  • Network: k8s

Configurações do Security Group

  • Nome: k8s
  • Descrição: k8s
  • Inbound rules:
    • SSH: 22
    • Custom TCP: 6443 (Somente para as maquinas do mesmo security group)
    • Custom TCP: 2379-2380
    • Custom TCP: 10250-10255 (Somente para as maquinas do mesmo security group)
    • Custom TCP: 30000-32767 (se for utilizar o serviço NodePort)
    • Custom TCP: 6783 (Somente para as maquinas do mesmo security group)
    • Custom UDP: 6783-6784 (Somente para as maquinas do mesmo security group)

Configurando as maquinas

Configurando o hostname

sudo hostnamectl set-hostname k8s-control-plane
sudo hostnamectl set-hostname k8s-worker-1
sudo hostnamectl set-hostname k8s-worker-2

Desabilitando o swap

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

Habilitando os módulos do kernel

sudo vim /etc/modules-load.d/k8s.conf
overlay
br_netfilter
sudo modprobe overlay
sudo modprobe br_netfilter

Configurando o sysctl

sudo vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
sudo sysctl --system
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release

Adicionando a chave do repositório

sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

Adicionando o repositório

echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Instalando o kubeadm, kubelet e kubectl

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Instalando o Container Runtime (Containerd)

Adicionando a chave do repositório

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Adicionando o repositório

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get update && sudo apt-get install -y containerd.io

Criando o arquivo de configuração do containerd

sudo containerd config default | sudo tee /etc/containerd/config.toml
sudo sed -i 's/SystemdGroup = false/SystemdGroup = true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
sudo systemctl enable kubelet

Abrindo as portas no firewall

sudo ufw allow 6443/tcp
sudo ufw allow 6783/tcp
sudo ufw allow 10250:10255/tcp
sudo ufw reload

Iniciando o cluster

sudo kubeadm init --pod-network-cidr=10.10.0.0/16 --apiserver-advertise-address={IP da maquina}
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes

Iniciando os nós workers

kubeadm token create --print-join-command
sudo kubeadm join {IP da maquina}:6443 --token {token} --discovery-token-ca-cert-hash {hash}

Instalando o Weave Net (CNI)

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Visualizando mais informações do cluster

kubectl get nodes -o wide
kubectl describe node {nome do node}