162 lines
6.9 KiB
Markdown
162 lines
6.9 KiB
Markdown
# Kubernetes Cluster Management mit Terraform
|
|
|
|
|
|
Dieses Modul erstellt und verwaltet Kubernetes Cluster auf Harvester mit den folgenden Features:
|
|
- Cilium CNI (von Rancher)
|
|
- Aktiviertes KubeProxyReplacement
|
|
- Gateway API Support
|
|
|
|
## Voraussetzungen
|
|
|
|
|
|
Folgende Tools werden benötigt:
|
|
- Terraform v1.5.0 oder höher
|
|
- [Infisical CLI](https://infisical.com/docs/cli/overview) (für Secrets Management)
|
|
- Rancher CLI (für Kubeconfig-Erstellung)
|
|
- kubectl (optional)
|
|
|
|
Vor der ersten Verwendung muss eine kubeconfig-Datei erstellt werden:
|
|
```bash
|
|
./scripts/create_kubeconfig.sh <cluster-name> <umgebung>
|
|
```
|
|
|
|
## Cluster Management mit Workspaces
|
|
|
|
Das Modul verwendet Terraform Workspaces für die Verwaltung verschiedener Cluster-Umgebungen. Jeder Workspace hat seinen eigenen State und spezifische Konfigurationen.
|
|
|
|
### 1. Workspace Verwaltung
|
|
|
|
Verfügbare Workspace-Befehle:
|
|
```bash
|
|
# Workspace erstellen
|
|
terraform workspace new [dev|test|prod]
|
|
|
|
# Workspace auswählen
|
|
terraform workspace select [dev|test|prod]
|
|
|
|
# Alle Workspaces anzeigen
|
|
terraform workspace list
|
|
|
|
# Aktuellen Workspace anzeigen
|
|
terraform workspace show
|
|
```
|
|
|
|
### 2. Cluster Deployment
|
|
|
|
Cluster erstellen oder aktualisieren:
|
|
```bash
|
|
# Workspace auswählen
|
|
terraform workspace select [dev|test|prod]
|
|
|
|
# Konfiguration prüfen
|
|
terraform plan -var-file="cluster-configs/${terraform.workspace}.tfvars"
|
|
|
|
# Cluster erstellen/aktualisieren
|
|
terraform apply -var-file="cluster-configs/${terraform.workspace}.tfvars"
|
|
```
|
|
|
|
### 3. Cluster Entfernen
|
|
|
|
Zum Löschen eines Clusters:
|
|
```bash
|
|
# Workspace auswählen
|
|
terraform workspace select [dev|test|prod]
|
|
|
|
# Cluster löschen
|
|
terraform destroy -var-file="cluster-configs/${terraform.workspace}.tfvars"
|
|
```
|
|
|
|
### 4. State Management
|
|
|
|
Jeder Workspace verwaltet seinen eigenen State in einem separaten Verzeichnis:
|
|
```
|
|
terraform.tfstate.d/
|
|
├── dev/
|
|
│ └── terraform.tfstate
|
|
├── test/
|
|
│ └── terraform.tfstate
|
|
└── prod/
|
|
└── terraform.tfstate
|
|
```
|
|
|
|
|
|
## Cluster Konfigurationen
|
|
|
|
Die Cluster werden je nach Workspace unterschiedlich konfiguriert:
|
|
|
|
| Workspace | Beschreibung | Nodes |
|
|
|-----------|----------------------|--------------------------------------------------------------|
|
|
| dev | Entwicklungsumgebung | 1 All-in-One Node (4 CPU, 16GB RAM) |
|
|
| test | Testumgebung | 1 Control-Plane (2 CPU, 4GB RAM) + 1 Worker (4 CPU, 8GB RAM) |
|
|
| prod | Produktionsumgebung | 3 Control-Plane (2 CPU, 4GB RAM) + 2 Worker (4 CPU, 8GB RAM) |
|
|
|
|
## Nach dem Deployment
|
|
|
|
|
|
Nach erfolgreicher Cluster-Erstellung:
|
|
|
|
1. Kubeconfig generieren:
|
|
```bash
|
|
./scripts/create_kubeconfig.sh ${terraform.workspace}
|
|
```
|
|
Die Kubeconfig wird als `hnrx-${terraform.workspace}-kubeconfig` gespeichert.
|
|
|
|
2. Gateway API CRDs installieren:
|
|
```bash
|
|
# Kubeconfig verwenden
|
|
export KUBECONFIG=hnrx-${terraform.workspace}-kubeconfig
|
|
|
|
# Gateway API CRDs installieren
|
|
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/standard-install.yaml
|
|
```
|
|
|
|
|
|
---
|
|
|
|
# Terraform Module Description
|
|
<!-- BEGIN_TF_DOCS -->
|
|
## Requirements
|
|
|
|
| Name | Version |
|
|
|---------------------------------------------------------------------------|----------|
|
|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
|
|
| <a name="requirement_infisical"></a> [infisical](#requirement\_infisical) | 0.15.21 |
|
|
| <a name="requirement_rancher2"></a> [rancher2](#requirement\_rancher2) | 7.3.2 |
|
|
|
|
## Providers
|
|
|
|
No providers.
|
|
|
|
## Modules
|
|
|
|
| Name | Source | Version |
|
|
|------|--------|---------|
|
|
| <a name="module_rancher_cluster"></a> [rancher\_cluster](#module\_rancher\_cluster) | git::https://git.hnrx.net/terraform-modules/rancher-cluster.git | main |
|
|
|
|
## Resources
|
|
|
|
No resources.
|
|
|
|
## Inputs
|
|
|
|
| Name | Description | Type | Default | Required |
|
|
|----------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|----------|-------------------------------|:--------:|
|
|
| <a name="input_cluster_kubernetes_version"></a> [cluster\_kubernetes\_version](#input\_cluster\_kubernetes\_version) | Kubernetes Version für die Cluster | `string` | `"v1.32.5+rke2r1"` | no |
|
|
| <a name="input_cluster_vm_image"></a> [cluster\_vm\_image](#input\_cluster\_vm\_image) | Image für die VMs | `string` | `"default/image-62xch"` | no |
|
|
| <a name="input_cluster_vm_network"></a> [cluster\_vm\_network](#input\_cluster\_vm\_network) | Netzwerk für die VMs | `string` | `"default/vmnetwork"` | no |
|
|
| <a name="input_harvester_cluster_name"></a> [harvester\_cluster\_name](#input\_harvester\_cluster\_name) | Name des Harvester Clusters | `string` | `"c-g8zlv"` | no |
|
|
| <a name="input_infisical_api_url"></a> [infisical\_api\_url](#input\_infisical\_api\_url) | Infisical API URL | `string` | `"https://app.infisical.com"` | no |
|
|
| <a name="input_infisical_client_id"></a> [infisical\_client\_id](#input\_infisical\_client\_id) | Infisical Client ID für die Authentifizierung | `string` | n/a | yes |
|
|
| <a name="input_infisical_client_secret"></a> [infisical\_client\_secret](#input\_infisical\_client\_secret) | Infisical Client Secret für die Authentifizierung | `string` | n/a | yes |
|
|
| <a name="input_infisical_environment"></a> [infisical\_environment](#input\_infisical\_environment) | Infisical Umgebung (z.B. dev, test, prod) | `string` | `"dev"` | no |
|
|
| <a name="input_infisical_project_id"></a> [infisical\_project\_id](#input\_infisical\_project\_id) | Infisical Projekt ID | `string` | n/a | yes |
|
|
| <a name="input_rancher2_api_url"></a> [rancher2\_api\_url](#input\_rancher2\_api\_url) | Rancher API URL | `string` | n/a | yes |
|
|
| <a name="input_rancher2_insecure"></a> [rancher2\_insecure](#input\_rancher2\_insecure) | SSL-Verifizierung für Rancher API überspringen | `bool` | `false` | no |
|
|
|
|
## Outputs
|
|
|
|
| Name | Description |
|
|
|------|-------------|
|
|
| <a name="output_cluster"></a> [cluster](#output\_cluster) | Details des Clusters |
|
|
|
|
<!-- END_TF_DOCS --> |