diff --git a/cephrook.pdf b/cephrook.pdf deleted file mode 100644 index fba107a..0000000 Binary files a/cephrook.pdf and /dev/null differ diff --git a/cluster_init.pdf b/cluster_init.pdf deleted file mode 100644 index b129b23..0000000 Binary files a/cluster_init.pdf and /dev/null differ diff --git a/comprobaciones.pdf b/comprobaciones.pdf deleted file mode 100644 index 385c1d0..0000000 Binary files a/comprobaciones.pdf and /dev/null differ diff --git a/haproxy/values.yaml b/haproxy/values.yaml index 1c3f2a1..812cd63 100644 --- a/haproxy/values.yaml +++ b/haproxy/values.yaml @@ -8,7 +8,7 @@ controller: service: enabled: true type: LoadBalancer - loadBalancerIP: 192.168.1.100 + loadBalancerIP: 192.168.0.101 ports: http: port: 80 diff --git a/kubevirt.pdf b/kubevirt.pdf deleted file mode 100644 index df17211..0000000 Binary files a/kubevirt.pdf and /dev/null differ diff --git a/redes_internet.pdf b/redes_internet.pdf deleted file mode 100644 index 3c7b891..0000000 Binary files a/redes_internet.pdf and /dev/null differ diff --git a/wireguard/daemonset/iptables-daemonset.yaml b/wireguard/daemonset/iptables-daemonset.yaml new file mode 100644 index 0000000..ac83fce --- /dev/null +++ b/wireguard/daemonset/iptables-daemonset.yaml @@ -0,0 +1,28 @@ +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: wg-nat-rules + namespace: wireguard +spec: + selector: + matchLabels: + app: wg-nat-rules + template: + metadata: + labels: + app: wg-nat-rules + spec: + hostNetwork: true + containers: + - name: iptables + image: alpine:latest + securityContext: + privileged: true + command: + - /bin/sh + - -c + - | + sysctl -w net.ipv4.ip_forward=1 + iptables -t nat -C POSTROUTING -s 192.168.254.0/24 -d 192.168.0.0/24 -j MASQUERADE || iptables -t nat -A POSTROUTING -s 192.168.254.0/24 -d 192.168.0.0/24 -j MASQUERADE + iptables -t nat -C POSTROUTING -s 192.168.254.0/24 -d 10.42.0.0/16 -j MASQUERADE || iptables -t nat -A POSTROUTING -s 192.168.254.0/24 -d 10.42.0.0/16 -j MASQUERADE + sleep infinity diff --git a/wireguard/deployments/deployment.yaml b/wireguard/deployments/deployment.yaml new file mode 100644 index 0000000..61ece73 --- /dev/null +++ b/wireguard/deployments/deployment.yaml @@ -0,0 +1,49 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: wg-easy + namespace: wireguard +spec: + replicas: 1 + selector: + matchLabels: + app: wg-easy + template: + metadata: + labels: + app: wg-easy + spec: + containers: + - name: wg-easy + image: ghcr.io/wg-easy/wg-easy + env: + - name: WG_HOST + value: "wireguard.c2et.net" + - name: WG_PORT + value: "51819" + - name: WG_DEFAULT_ADDRESS + value: "192.168.254.x" + - name: WG_DEFAULT_ALLOWEDIPS + value: "192.168.0.0/24,10.42.0.0/16" + - name: WG_DEFAULT_DNS + value: "192.168.0.1" + - name: PASSWORD_HASH + valueFrom: + secretKeyRef: + name: wg-easy-secret + key: PASSWORD_HASH + ports: + - containerPort: 51819 + protocol: UDP + - containerPort: 51821 + protocol: TCP + volumeMounts: + - name: config + mountPath: /etc/wireguard + securityContext: + capabilities: + add: ["NET_ADMIN", "SYS_MODULE"] + volumes: + - name: config + persistentVolumeClaim: + claimName: wg-easy-config diff --git a/wireguard/ingress/ingress.yaml b/wireguard/ingress/ingress.yaml new file mode 100644 index 0000000..5028ab1 --- /dev/null +++ b/wireguard/ingress/ingress.yaml @@ -0,0 +1,26 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: wg-easy-web + namespace: wireguard + annotations: + cert-manager.io/cluster-issuer: "letsencrypt-prod" + nginx.ingress.kubernetes.io/ssl-redirect: "true" + nginx.ingress.kubernetes.io/backend-protocol: "HTTP" +spec: + ingressClassName: nginx + tls: + - hosts: + - wireguard.c2et.net + secretName: wg-easy-tls + rules: + - host: wireguard.c2et.net + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: wg-easy-web + port: + number: 51821 diff --git a/wireguard/kustomization.yaml b/wireguard/kustomization.yaml new file mode 100644 index 0000000..dda74af --- /dev/null +++ b/wireguard/kustomization.yaml @@ -0,0 +1,9 @@ +resources: + - namespace.yaml + - pvc/pvc.yaml + - secret.yaml + - deployments/deployment.yaml + - services/service-tcp.yaml + - services/service-udp.yaml + - daemonset/iptables-daemonset.yaml + - ingress/ingress.yaml diff --git a/wireguard/namespace.yaml b/wireguard/namespace.yaml new file mode 100644 index 0000000..75e9be3 --- /dev/null +++ b/wireguard/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: wireguard diff --git a/wireguard/pvc/pvc.yaml b/wireguard/pvc/pvc.yaml new file mode 100644 index 0000000..85f710d --- /dev/null +++ b/wireguard/pvc/pvc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: wg-easy-config + namespace: wireguard +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + storageClassName: ceph-rbd diff --git a/wireguard/readme.md b/wireguard/readme.md new file mode 100644 index 0000000..fffdf33 --- /dev/null +++ b/wireguard/readme.md @@ -0,0 +1,159 @@ +# WireGuard en Kubernetes (wg-easy) + +Este despliegue proporciona acceso VPN mediante **WireGuard** dentro de un clúster Kubernetes, utilizando la imagen `wg-easy`, expuesto por un `Service` tipo `NodePort` y gestionado con reglas de `iptables` en el host. + +## 🔐 ¿Qué es WireGuard? + +WireGuard es un protocolo de VPN moderno, rápido y seguro, que utiliza criptografía de última generación para establecer túneles cifrados entre dispositivos. + +Este despliegue usa `wg-easy`, una interfaz web y API para administrar fácilmente instancias de WireGuard. + +--- + +## ⚖️ Estructura del despliegue + +``` +. +├── daemonset +│ └── iptables-daemonset.yaml # Reglas de NAT +├── deployments +│ └── deployment.yaml # Despliegue de wg-easy +├── ingress +│ └── ingress.yaml # (opcional) +├── pvc +│ └── pvc.yaml # Configuración persistente +├── services +│ └── service.yaml # Service tipo NodePort +├── secret.yaml # HASH de contraseña para interfaz web +├── namespace.yaml +└── kustomization.yaml +``` + +--- + +## 🚫 Seguridad y acceso + +### Contraseña + +Para acceder a la interfaz web protegida de wg-easy, se define un `Secret` con un hash de contraseña. + +Puedes generar el hash con `htpasswd` (instalado con `apache2-utils`): + +```bash +apt install apache2-utils +htpasswd -nBC 10 "admin" +``` + +Copia el resultado (sin el `admin:`) en el archivo `secret.yaml` como `PASSWORD_HASH`. + +--- + +## 📁 Volúmenes + +La configuración de WireGuard se guarda en `/etc/wireguard` dentro del contenedor. Esto se monta con un `PersistentVolumeClaim` para que los peers se conserven aunque se reinicie el pod. + +--- + +## 🛂 Puertos y exposición + +El `Service` es tipo `NodePort`, lo que significa: + +* Puerto del contenedor WireGuard: `51820/UDP` +* Puerto de la interfaz web: `51821/TCP` +* Puerto `NodePort` expuesto: + + * UDP: `31820` (WireGuard) + * TCP: `31821` (Interfaz Web) + +Los `NodePort` deben estar en el rango 30000–32767 (por defecto en Kubernetes). + +El router debe redirigir estos puertos al nodo donde corre `wg-easy`. + +Ejemplo de reenvío: + +* `31820/UDP` → nodo `tartaro`, puerto `31820` +* `31821/TCP` → nodo `tartaro`, puerto `31821` + +--- + +## 🚀 Reenvío de tráfico y NAT + +WireGuard entrega a los clientes direcciones como `192.168.200.x`, pero por defecto no tendrían acceso a otras redes como: + +* Red de administración: `192.168.1.0/24` +* Red de pods (Flannel): `10.42.0.0/16` +* Red Multus (ejemplo): `192.168.201.0/24` + +Para ello, se despliega un `DaemonSet` que aplica reglas `iptables` directamente en el host físico: + +```yaml +iptables -t nat -A POSTROUTING -s 192.168.200.0/24 -d 192.168.1.0/24 -j MASQUERADE +iptables -t nat -A POSTROUTING -s 192.168.200.0/24 -d 10.42.0.0/16 -j MASQUERADE +``` + +Con esto, los paquetes generados por los clientes WireGuard se traducen a la IP del host, y así acceden a otras redes internas. + +--- + +## 🛠️ Acceso a redes aisladas con Multus + +Si se desea permitir acceso a una red definida por un `NetworkAttachmentDefinition` (NAD) con Multus, como por ejemplo: + +```yaml +apiVersion: "k8s.cni.cncf.io/v1" +kind: NetworkAttachmentDefinition +metadata: + name: br-servicios + namespace: default +spec: + config: '{ + "cniVersion": "0.3.1", + "type": "bridge", + "bridge": "br-servicios", + "ipam": { + "type": "host-local", + "subnet": "192.168.201.0/24", + "rangeStart": "192.168.201.100", + "rangeEnd": "192.168.201.199" + } + }' +``` + +Entonces basta con añadir una regla más en el `DaemonSet`: + +```bash +iptables -t nat -A POSTROUTING -s 192.168.200.0/24 -d 192.168.201.0/24 -j MASQUERADE +``` + +No es necesario conectar el contenedor de wg-easy directamente a esta red: el tráfico se enruta y NATea desde el host físico. + +--- + +## 🔄 Reaplicar cambios + +Siempre que modifiques el `DaemonSet` o el `Deployment`, puedes aplicar los cambios con: + +```bash +kubectl apply -k . +``` + +--- + +## ✨ Resultado + +Una vez desplegado y con el reenvío del router correctamente configurado, puedes acceder a la interfaz web en: + +``` +https://manabo.org:31821 +``` + +Y configurar tus clientes WireGuard escaneando el QR generado, o descargando el archivo de configuración. + +Los clientes podrán acceder a: + +* `192.168.1.0/24` (red local) +* `10.42.0.0/16` (pods del clúster) +* `192.168.201.0/24` (red multus, si se añade la regla correspondiente) + +--- + diff --git a/wireguard/secret.yaml b/wireguard/secret.yaml new file mode 100644 index 0000000..248c64d --- /dev/null +++ b/wireguard/secret.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Secret +metadata: + name: wg-easy-secret + namespace: wireguard +type: Opaque +stringData: + PASSWORD_HASH: "$2y$10$zsEKyp1SjKQO0.KKX/.0dueSxrPRVIDp0Zqg47zp0zOckuT/gRsLu" diff --git a/wireguard/services/service-tcp.yaml b/wireguard/services/service-tcp.yaml new file mode 100644 index 0000000..8fc5ddd --- /dev/null +++ b/wireguard/services/service-tcp.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: wg-easy-web + namespace: wireguard +spec: + type: ClusterIP + selector: + app: wg-easy + ports: + - name: web + port: 51821 + targetPort: 51821 + protocol: TCP diff --git a/wireguard/services/service-udp.yaml b/wireguard/services/service-udp.yaml new file mode 100644 index 0000000..eeae887 --- /dev/null +++ b/wireguard/services/service-udp.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: wg-easy-udp + namespace: wireguard +spec: + type: LoadBalancer + loadBalancerIP: 192.168.0.101 + selector: + app: wg-easy + ports: + - name: wireguard-udp + port: 51819 + targetPort: 51819 + protocol: UDP