Baremetal Local
Para esta instalación solamente vamos a configurar 3 nodos, 1 master y 2 workers.
Esta será la base para las instalaciones con multi masters y multi etcd.
Obviamente no es una instalación con alta disponibilidad, pero sirve para aprender los requisitos básicos de la instalación.
Vamos a utilizar vagrant para provisionar rápidamente estas máquinas en virtualbox, de esta manera es necesario que VirtualBox esté instalado.
Los requisitos necesarios:
sudo apt-get install vagrant
sudo apt-get install virtualbox
En versiones más recientes de VirtualBox, necesité modificar la configuración para liberar el rango de IPs.
sudo mkdir /etc/vbox
sudo echo "* 10.0.0.0/8 192.168.0.0/16" >> /etc/vbox/networks.conf
sudo echo "* 2001::/64" >> /etc/vbox/networks.conf
El archivo Vagrantfile contiene la configuración de las máquinas y las llamadas de los scripts necesarios para la instalación.
El archivo bootstrap.sh será el script que todos los nodos necesitan ejecutar al final del despliegue de cada uno de los nodos.
Por último, si analizas el vagrantfile, en caso de ser master ejecutará el script bootstrap_master.sh y en caso de ser worker ejecutará el script bootstrap_worker.sh.
En el script para el master, crea el clúster y guarda el comando join en un ejecutable para que el worker ejecute el mismo a través de ssh hacia el master.
Comandos básicos de vagrant
Las máquinas definidas en el vagrant file son: master, worker1, y worker2
Iniciar todas las máquinas.
vagrant up
Destruir todas las máquinas.
vagrant destroy
Iniciar una máquina específica.
vagrant up master
Destruir una máquina específica.
vagrant destroy worker1
Apagar todas las máquinas.
vagrant halt
Apagar una máquina específica.
vagrant halt worker1
Entrar en una máquina utilizando ssh
vagrant ssh master
Salida de la instalación
❯ vagrant up
Bringing machine 'master' up with 'virtualbox' provider...
Bringing machine 'worker1' up with 'virtualbox' provider...
Bringing machine 'worker2' up with 'virtualbox' provider...
==> master: Importing base box 'ubuntu/jammy64'...
==> master: Matching MAC address for NAT networking...
==> master: Checking if box 'ubuntu/jammy64' version '20220718.0.0' is up to date...
==> master: Setting the name of the VM: master
==> master: Clearing any previously set network interfaces...
==> master: Preparing network interfaces based on configuration...
master: Adapter 1: nat
master: Adapter 2: hostonly
==> master: Forwarding ports...
master: 22 (guest) => 2222 (host) (adapter 1)
==> master: Running 'pre-boot' VM customizations...
==> master: Booting VM...
==> master: Waiting for machine to boot. This may take a few minutes...
master: SSH address: 127.0.0.1:2222
master: SSH username: vagrant
master: SSH auth method: private key
master:
master: Vagrant insecure key detected. Vagrant will automatically replace
master: this with a newly generated keypair for better security.
master:
master: Inserting generated public key within guest...
master: Removing insecure key from the guest if it's present...
master: Key inserted! Disconnecting and reconnecting using new SSH key...
==> master: Machine booted and ready!
==> master: Checking for guest additions in VM...
master: The guest additions on this VM do not match the installed version of
master: VirtualBox! In most cases this is fine, but in rare cases it can
master: prevent things such as shared folders from working properly. If you see
master: shared folder errors, please make sure the guest additions within the
master: virtual machine match the version of VirtualBox you have installed on
master: your host and reload your VM.
master:
master: Guest Additions Version: 6.0.0 r127566
master: VirtualBox Version: 6.1
==> master: Setting hostname...
==> master: Configuring and enabling network interfaces...
==> master: Mounting shared folders...
master: /vagrant => /home/david/projects/pessoais/study-kubernetes/Instalacoes/Baremetal Local
==> master: Running provisioner: shell...
master: Running: /tmp/vagrant-shell20220726-606777-6pw4vn.sh
master: Desactivando el swap
master: Desactivando el firewall
master: Activando módulos del kernel necesarios para containerd
master: Agregando configuraciones del kernel para kubernetes
master: Instalando containerd con systemd de cgroups
master: Agregando el repositorio de kubernetes
master: Instalando binarios de Kubernetes (kubeadm, kubelet y kubectl)
master: Activando la autenticación por ssh
master: Configurando la contraseña de root
master: Actualizando los hosts en el archivo /etc/hosts
==> master: Running provisioner: shell...
master: Running: /tmp/vagrant-shell20220726-606777-flrq4g.sh
master: Descargando las imágenes necesarias para los contenedores en el master
master: Inicializando el clúster
master: Creando la carpeta .kube para el usuario vagrant
master: ##### Creando el .kube/config a partir del admin.conf #####
master: Despliegue del cni weavenet
master: WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short. Use --output=yaml|json to get the full version.
master: The connection to the server localhost:8080 was refused - did you specify the right host or port?
master: Creando el comando join para los workers /joincluster.sh
==> worker1: You assigned a static IP ending in ".1" to this machine.
==> worker1: This is very often used by the router and can cause the
==> worker1: network to not work properly. If the network doesn't work
==> worker1: properly, try changing this IP.
==> worker1: Importing base box 'ubuntu/jammy64'...
==> worker1: Matching MAC address for NAT networking...
==> worker1: You assigned a static IP ending in ".1" to this machine.
==> worker1: This is very often used by the router and can cause the
==> worker1: network to not work properly. If the network doesn't work
==> worker1: properly, try changing this IP.
==> worker1: Checking if box 'ubuntu/jammy64' version '20220718.0.0' is up to date...
==> worker1: Setting the name of the VM: worker1
==> worker1: Fixed port collision for 22 => 2222. Now on port 2200.
==> worker1: Clearing any previously set network interfaces...
==> worker1: Preparing network interfaces based on configuration...
worker1: Adapter 1: nat
worker1: Adapter 2: hostonly
==> worker1: Forwarding ports...
worker1: 22 (guest) => 2200 (host) (adapter 1)
==> worker1: Running 'pre-boot' VM customizations...
==> worker1: Booting VM...
==> worker1: Waiting for machine to boot. This may take a few minutes...
worker1: SSH address: 127.0.0.1:2200
worker1: SSH username: vagrant
worker1: SSH auth method: private key
worker1:
worker1: Vagrant insecure key detected. Vagrant will automatically replace
worker1: this with a newly generated keypair for better security.
worker1:
worker1: Inserting generated public key within guest...
worker1: Removing insecure key from the guest if it's present...
worker1: Key inserted! Disconnecting and reconnecting using new SSH key...
==> worker1: Machine booted and ready!
==> worker1: Checking for guest additions in VM...
worker1: The guest additions on this VM do not match the installed version of
worker1: VirtualBox! In most cases this is fine, but in rare cases it can
worker1: prevent things such as shared folders from working properly. If you see
worker1: shared folder errors, please make sure the guest additions within the
worker1: virtual machine match the version of VirtualBox you have installed on
worker1: your host and reload your VM.
worker1:
worker1: Guest Additions Version: 6.0.0 r127566
worker1: VirtualBox Version: 6.1
==> worker1: Setting hostname...
==> worker1: Configuring and enabling network interfaces...
==> worker1: Mounting shared folders...
worker1: /vagrant => /home/david/projects/pessoais/study-kubernetes/Instalacoes/Baremetal Local
==> worker1: Running provisioner: shell...
worker1: Running: /tmp/vagrant-shell20220726-606777-nzd7b9.sh
worker1: Desactivando el swap
worker1: Desactivando el firewall
worker1: Activando módulos del kernel necesarios para containerd
worker1: Agregando configuraciones del kernel para kubernetes
worker1: Instalando containerd con systemd de cgroups
worker1: Agregando el repositorio de kubernetes
worker1: Instalando binarios de Kubernetes (kubeadm, kubelet y kubectl)
worker1: Activando la autenticación por ssh
worker1: Configurando la contraseña de root
worker1: Actualizando los hosts en el archivo /etc/hosts
==> worker1: Running provisioner: shell...
worker1: Running: /tmp/vagrant-shell20220726-606777-7qs118.sh
worker1: Ejecutando el join del clúster
==> worker2: Importing base box 'ubuntu/jammy64'...
==> worker2: Matching MAC address for NAT networking...
==> worker2: Checking if box 'ubuntu/jammy64' version '20220718.0.0' is up to date...
==> worker2: Setting the name of the VM: worker2
==> worker2: Fixed port collision for 22 => 2222. Now on port 2201.
==> worker2: Clearing any previously set network interfaces...
==> worker2: Preparing network interfaces based on configuration...
worker2: Adapter 1: nat
worker2: Adapter 2: hostonly
==> worker2: Forwarding ports...
worker2: 22 (guest) => 2201 (host) (adapter 1)
==> worker2: Running 'pre-boot' VM customizations...
==> worker2: Booting VM...
==> worker2: Waiting for machine to boot. This may take a few minutes...
worker2: SSH address: 127.0.0.1:2201
worker2: SSH username: vagrant
worker2: SSH auth method: private key
worker2: Warning: Remote connection disconnect. Retrying...
worker2: Warning: Connection reset. Retrying...
worker2:
worker2: Vagrant insecure key detected. Vagrant will automatically replace
worker2: this with a newly generated keypair for better security.
worker2:
worker2: Inserting generated public key within guest...
worker2: Removing insecure key from the guest if it's present...
worker2: Key inserted! Disconnecting and reconnecting using new SSH key...
==> worker2: Machine booted and ready!
==> worker2: Checking for guest additions in VM...
worker2: The guest additions on this VM do not match the installed version of
worker2: VirtualBox! In most cases this is fine, but in rare cases it can
worker2: prevent things such as shared folders from working properly. If you see
worker2: shared folder errors, please make sure the guest additions within the
worker2: virtual machine match the version of VirtualBox you have installed on
worker2: your host and reload your VM.
worker2:
worker2: Guest Additions Version: 6.0.0 r127566
worker2: VirtualBox Version: 6.1
==> worker2: Setting hostname...
==> worker2: Configuring and enabling network interfaces...
==> worker2: Mounting shared folders...
worker2: /vagrant => /home/david/projects/pessoais/study-kubernetes/Instalacoes/Baremetal Local
==> worker2: Running provisioner: shell...
worker2: Running: /tmp/vagrant-shell20220726-606777-rfgvit.sh
worker2: Desactivando el swap
worker2: Desactivando el firewall
worker2: Activando módulos del kernel necesarios para containerd
worker2: Agregando configuraciones del kernel para kubernetes
worker2: Instalando containerd con systemd de cgroups
worker2: Agregando el repositorio de kubernetes
worker2: Instalando binarios de Kubernetes (kubeadm, kubelet y kubectl)
worker2: Activando la autenticación por ssh
worker2: Configurando la contraseña de root
worker2: Actualizando los hosts en el archivo /etc/hosts
==> worker2: Running provisioner: shell...
worker2: Running: /tmp/vagrant-shell20220726-606777-ffyab8.sh
worker2: Ejecutando el join del clúster
Ahora vamos a entrar en el master y verificar si el clúster está activo
❯ vagrant ssh master
vagrant@master:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane 10m v1.24.0
worker1 Ready <none> 8m20s v1.24.0
worker2 Ready <none> 6m39s v1.24.0
vagrant@master:~$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-6d4b75cb6d-2xwkd 1/1 Running 0 13m
kube-system coredns-6d4b75cb6d-mfbkv 1/1 Running 0 13m
kube-system etcd-master 1/1 Running 0 13m
kube-system kube-apiserver-master 1/1 Running 0 13m
kube-system kube-controller-manager-master 1/1 Running 0 13m
kube-system kube-proxy-567z7 1/1 Running 0 10m
kube-system kube-proxy-92j9g 1/1 Running 0 11m
kube-system kube-proxy-gsvd5 1/1 Running 0 13m
kube-system kube-scheduler-master 1/1 Running 0 13m
kube-system weave-net-25k8d 2/2 Running 1 (9m17s ago) 10m
kube-system weave-net-4pvlc 2/2 Running 1 (13m ago) 13m
kube-system weave-net-sxsdz 2/2 Running 1 (11m ago) 11m
vagrant@master:~$
Remote kubectl
Instala kubectl en tu máquina.
sudo apt-get install kubectl
Copia el contenido de .kube/config desde dentro de la máquina master
vagrant ssh master
cat /home/vagrant/.kube/config
Pega toda la salida
mkdir /home/$USER/.kube
vim config # o nano da igual
# pega, guarda y sal
Ahora solo tienes que ejecutar desde tu máquina
kubectl get nodes
Si ya tienes un .kube/config configurado para otro clúster en tu máquina, crea otro archivo, por ejemplo config-vagrant-local, y pega el contenido.
kubectl --kubeconfig <ruta para config-vagrant-local> get nodes
❯ kubectl --kubeconfig /home/$USER/.kube/config-vagrant-local get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane 41m v1.24.0
worker1 Ready <none> 40m v1.24.0
worker2 Ready <none> 38m v1.24.0
Existen mejores maneras para gestionar el kube config, las veremos durante el estudio.