93 lines
2.5 KiB
Bash
Executable File
93 lines
2.5 KiB
Bash
Executable File
#!/bin/sh
|
|
set -o errexit
|
|
|
|
# Script: create_kind_cluster.sh
|
|
# Descripción: Este script automatiza la creación de un clúster de Kubernetes utilizando Kind (Kubernetes en Docker).
|
|
# También configura un registro Docker local y lo conecta al clúster para permitir el uso de un registro local
|
|
# para las imágenes de contenedor. Además, instala el controlador Ingress NGINX en el clúster para manejar recursos de ingreso.
|
|
# Author: Manuel Vergara
|
|
# Web: https://vergaracarmona.es
|
|
#
|
|
|
|
|
|
COLOR_RED='\033[0;31m'
|
|
COLOR_RESET='\033[0m'
|
|
|
|
# Control de salida con Ctrl + C
|
|
function ctrl_c() {
|
|
echo -e "${COLOR_RED}\n\n[!] Saliendo... \n${COLOR_RESET}"
|
|
tput cnorm; exit 1
|
|
}
|
|
|
|
# Ctrl+C
|
|
trap ctrl_c SIGINT
|
|
|
|
|
|
# Función para configurar el registro Docker local
|
|
configurar_registro_local() {
|
|
reg_name='kind-registry'
|
|
reg_port='5001'
|
|
|
|
if [ "$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)" != 'true' ]; then
|
|
docker run -d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" registry:2
|
|
fi
|
|
|
|
if [ "$(docker inspect -f='{{json .NetworkSettings.Networks.kind}}' "${reg_name}")" = 'null' ]; then
|
|
docker network connect "kind" "${reg_name}"
|
|
fi
|
|
}
|
|
|
|
# Función para documentar el registro local
|
|
documentar_registro_local() {
|
|
reg_port='5001'
|
|
echo "\nConfigurando el registro local...\n"
|
|
cat <<EOF | kubectl apply -f -
|
|
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: local-registry-hosting
|
|
namespace: kube-public
|
|
data:
|
|
localRegistryHosting.v1: |
|
|
host: "localhost:${reg_port}"
|
|
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
|
|
EOF
|
|
}
|
|
|
|
# Función para instalar el controlador Ingress NGINX
|
|
instalar_ingress_nginx() {
|
|
echo "\nInstalando el controlador Ingress NGINX...\n"
|
|
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
|
|
}
|
|
|
|
# Main
|
|
configurar_registro_local
|
|
kind create cluster --config=- <<EOF
|
|
kind: Cluster
|
|
apiVersion: kind.x-k8s.io/v1alpha4
|
|
containerdConfigPatches:
|
|
- |-
|
|
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5001"]
|
|
endpoint = ["http://kind-registry:5000"]
|
|
nodes:
|
|
- role: control-plane
|
|
kubeadmConfigPatches:
|
|
- |
|
|
kind: InitConfiguration
|
|
nodeRegistration:
|
|
kubeletExtraArgs:
|
|
node-labels: "ingress-ready=true"
|
|
extraPortMappings:
|
|
- containerPort: 80
|
|
hostPort: 80
|
|
protocol: TCP
|
|
- containerPort: 443
|
|
hostPort: 443
|
|
protocol: TCP
|
|
- role: worker
|
|
- role: worker
|
|
EOF
|
|
|
|
documentar_registro_local
|
|
instalar_ingress_nginx
|