Di seguito sonno fornite diverse guide:
- una configurazione veloce di k3s, vedi K3s Rapid Setup
- una configurazzione completa, vedi K3s Multi-Cluster Setup Guide
| K3s Deployment Guide | Steps |
|---|---|
| π K3s-S4T Rapid Setup β‘ | 1. π Installazione di K3s |
| 2. π Clonazione S4T - OPZIONE2 | |
| 3. π Conversione YAML (Kompose) - none | |
| 4. π Deploy su Kubernetes | |
| 5. β Verifica dei Pod e dei Servizi | |
| π K3s-Calico-MetalLB-Istio-S4T Multi-Cluster Setup | 1. βοΈ Installazione di K3s (senza Traefik) |
| 2. π Configurazione di Calico | |
| 3. π‘ Setup di MetalLB | |
| 4. π Deploy di Istio | |
| 5. π Clonazione S4T - OPZIONE2 | |
| 6. π Conversione YAML (Kompose) - none | |
| 7. π Deploy su Kubernetes | |
| 8. β Verifica dei Pod e dei Servizi |
curl -sfL https://get.k3s.io | sh -
sudo chmod 644 /etc/rancher/k3s/k3s.yaml
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
kubectl get nodesπΉ Cosa fa questo comando?
- Installa K3s come master node
- Avvia automaticamente il servizio
β οΈ Nota: Configura kubectl per gestire il cluster S4T - Stack4Things Deploymentβ οΈ Nota: In questa configurazione non sono presenti Calico, istio e MetalLb necessari per alcuni esempiβ οΈ Con questa configurazione si otterrΓ S4T con servizi interni al cluster in una configurazione minimale ma configurabile a piacere.
Con la seguente guida si otterrΓ una configurazione con:
- Calico come CNI
- MeatalLB come Loadbalancer
- Istio come Gateway
- S4T
Questa guida ti aiuterΓ a configurare un cluster K3s utilizzando una macchina come server (control plane) e una o piΓΉ macchine come worker nodes.
- Se preferisci eseguire un'installazione pulita di Calico, salta alla sezione dedicata a Calico piΓΉ avanti.
- Potrebbe essere necessario utilizzare gli IP interni delle macchine.
-
Accedi alla VM che fungerΓ da server, chiamato anche server host. Utilizza SSH per connetterti alla macchina.
-
Esegui il seguente comando per installare K3s sul server:
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --disable traefik --disable servicelb" sh -
Questo comando installerΓ K3s sul server host e disabiliterΓ Traefik e il bilanciamento del servizio (service load balancer).
Dopo aver installato K3s sul server, recupera il token del nodo che sarΓ utilizzato per aggiungere i worker nodes al cluster.
-
Esegui il seguente comando sul server per ottenere il token:
cat /var/lib/rancher/k3s/server/node-token
oppure:
sudo cat /var/lib/rancher/k3s/server/node-token
-
Il token che otterrai servirΓ per il comando successivo =
YourToken.
Per poter comunicare con il cluster, Γ¨ necessario recuperare il certificato di configurazione di K3s.
-
Esegui il seguente comando per ottenere il file di configurazione:
cat /etc/rancher/k3s/k3s.yaml
oppure:
sudo cat /etc/rancher/k3s/k3s.yaml
-
Salva il contenuto del file
k3s.yamlsul nodo worker~/.kube/come un file di configurazione personalizzato (<nome del tuo file>.yaml). -
Modifica il file sostituendo l'IP del server con l'IP corretto del control plane (Server Host) e assicurati che il server utilizzi HTTPS, sia sul nodo master (K3s) e sul nodo worker (
~/.kube/<nome del tuo file>.yaml).Esempio di modifica del file
k3s.yaml:server: https://<Contol Plane IP>:6443
-
Verifica la connessione al cluster:
β οΈ Configura il tuo ambiente:sudo chmod 644 /etc/rancher/k3s/k3s.yaml export KUBECONFIG=/etc/rancher/k3s/k3s.yamlEsegui il comando:
kubectl get nodes
Esempio di output:
NAME STATUS ROLES AGE VERSION ubuntuserver Ready control.plane,master xx xx
Ora puoi aggiungere i nodi di lavoro (worker nodes) al cluster.
-
Accedi alla VM che fungerΓ da worker node. Questo Γ¨ il nodo che eseguirΓ i carichi di lavoro, ed Γ¨ diverso dal nodo server.
-
Esegui il seguente comando sul nodo worker:
curl -sfL https://get.k3s.io | K3S_URL=https://<Contol Plane IP>:6443 K3S_TOKEN=<YourToken> sh -
Sostituisci
<Contol Plane IP>con l'IP del server (control plane) e<YourToken>con il token che hai ottenuto in precedenza. -
Configurazione di
kubectlsul Worker Node:- Crea un file
~/.kube/configsul nodo worker o utilizza quello creato in precedenza. - Copia il contenuto del file
k3s.yamldal server (control plane) in questo file. - Modifica il campo
serverper includere l'IP del server.
- Crea un file
-
Configura
kubectl:Esegui i seguenti comandi sul nodo worker per impostare il tuo ambiente Kubernetes:
sudo chmod 644 /etc/rancher/k3s/k3s.yaml export KUBECONFIG=~/.kube/<nome del tuo file>.yaml kubectl get nodes
Esempio di output:
NAME STATUS ROLES AGE VERSION ubuntuserver Ready control.plane,master xx xx ubuntuworker Ready <none> xx xx
Con questi passaggi avrai un cluster K3s funzionante con un server e uno o piΓΉ nodi worker.
Invece di installare un master ed un worker si puΓ² utilizzare una singolo nodo:
curl -sfL https://get.k3s.io | sh -
sudo chmod 644 /etc/rancher/k3s/k3s.yaml
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
kubectl get nodesCalico Γ¨ un plugin di rete per Kubernetes che fornisce networking e sicurezza per i pod. PuΓ² essere installato in due modi principali:
- Installazione sul control plane.
- Installazione manuale tramite manifest anche sul worker.
- Installazione tramite operator, che applica automaticamente su entrambi se eseguito sul control plane.
Calico puΓ² essere installato tramite due approcci principali:
- Operator: Un metodo automatizzato e gestito per l'installazione e l'aggiornamento di Calico.
- Manifest: Un metodo manuale che applica direttamente i file di configurazione YAML nel cluster.
- Installa l'operatore Calico e le definizioni delle risorse personalizzate.
- Repository di Calico: Link al repository Calico
- Guida all'installazione: Guida ufficiale all'installazione di Calico su K3s
- Installazione tramite Helm: Guida per l'installazione tramite Helm
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.2/manifests/tigera-operator.yaml
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.2/manifests/custom-resources.yaml
kubectl get nodes -o wide # Da eseguire anche sul worker --opzionale
kubectl get pods -n calico-system -o wide
ip route show
kubectl get pods -A | grep -E "calico|flannel" #flannel Γ¨ presente di default se non si segue l'installazione pulitaIl progetto Helm offre due metodi ufficiali per scaricare e installare Helm. Oltre a questi, la community di Helm fornisce anche altri metodi di installazione tramite diversi gestori di pacchetti.
Helm fornisce uno script di installazione che scarica e installa automaticamente l'ultima versione di Helm sul tuo sistema.
Puoi scaricare lo script ed eseguirlo localmente. Γ ben documentato, quindi puoi leggerlo in anticipo per capire cosa fa prima di eseguirlo.
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.shSe vuoi usare l'ultima versione instabile, puoi anche eseguire:
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash- Questo passaggio Γ¨ necessario solo se devi utilizzare certificati emessi dalla CA generata da Rancher (ingress.tls.source=rancher) o richiedere certificati emessi da Let's Encrypt (ingress.tls.source=letsEncrypt).
# Set the KUBECONFIG environment variable
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
# Apply the Cert-Manager Custom Resource Definitions (CRDs)
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/latest/download/cert-manager.crds.yaml
# Add the Jetstack Helm repository
helm repo add jetstack https://charts.jetstack.io
# Update your local Helm chart repository cache
helm repo update
# Install Cert-Manager using Helm
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace
# Verify Cert-Manager pods are running
kubectl get pods --namespace cert-manager
>> OUTPUT: 3 PODS IN RUNNING STATE
# Check installed Custom Resource Definitions (CRDs)
kubectl get crds | grep cert-manager
>> OUTPUT: 6 cert-manager CRDs found# Add the Rancher Helm repository
helm repo add rancher-latest https://releases.rancher.com/server-charts/latest
# Update Helm repositories
helm repo update
# Create the Rancher namespace
kubectl create namespace cattle-system
# Install Rancher using Helm
helm install rancher rancher-latest/rancher \
--namespace cattle-system \
--create-namespace \
--set hostname="hostname" \
--set bootstrapPassword="YourPassword"Usare il seguente comando:
kubectl port-forward -n cattle-system svc/rancher 8443:443
>> FORWARD TRAMITE x.x.x.x:443 -> 444
# Check the rollout status of the Rancher deployment
kubectl -n cattle-system rollout status deploy/rancher
>> OUTPUT: deployment "rancher" successfully rolled out
# Get the deployment details
kubectl -n cattle-system get deploy rancher
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
rancher 3 3 3 3 3m# Aggiungi il repository Helm di MetalLB
helm repo add metallb https://metallb.github.io/metallb
# Installa MetalLB
helm install metallb metallb/metallbSe preferisci installare MetalLB senza Helm, puoi applicare direttamente i manifest ufficiali:
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.10/config/manifests/metallb-native.yamlCreazione del file di configurazione per Layer 2:
echo "Apri il file di configurazione: https://github.com/AsCd1/K3s-Configuration/blob/main/MetalLB/metallb-configuration.yaml"
nano metallb-configuration.yaml- Creazione del file L2Advertisement separato.
nano l2advertisement.yamlApply della configurazione:
kubectl apply -f l2advertisement.yaml
kubectl apply -f metallb-configuration.yamlVerifica della configurazione:
kubectl get ipaddresspools -n metallb-system
>> Output: Lista dei range IP
kubectl get l2advertisements -n metallb-system
>> Output: Nome e range IP
kubectl get pods -n metallb-system
kubectl get svc -ACreazione della directory di prova:
mkdir nginx-prova
cd nginx-provaCreazione del file di configurazione:
nano nginx-hello-deployment.yamlApplicazione della configurazione:
kubectl apply -f .
oppure
kubectl apply -f nginx-hello-deployment.yamlControllo dei servizi:
kubectl get svc -AOutput atteso:
>>> default nginx LoadBalancer InternalIP ExternalIP <- deve essere quello del yaml
Testare l'accesso al servizio:
curl 192.x.x.xOutput atteso:
>>> Hello World!
In caso di problemi, provare:
wget -qO- 192.x.x.xOutput atteso:
>> Hello World!
Applica il Deployment per aumentare le repliche a 3:
kubectl apply -f nginx-hello-world-deployment-3Rep.yaml
kubectl get pods -l app=nginx
>> OUTPUT: 3 pod
curl 192.x.x.x
>> Hello World!Restituzione di Hello World! con il nome del pod che lo ha eseguito:
nano ngnix-monitoring.yaml
kubectl apply -f ngnix-monitoring.yaml
curl 192.x.x.x
>> Hello world from pod: nginx-<Podid>
kubectl get pods -o wideHai creato un cluster K3s con:
- Control Plane su una VM
- Worker Node su un'altra VM
- Calico come CNI (Container Network Interface)
- MetalLB per assegnare IP pubblici ai servizi di tipo LoadBalancer
E infine, hai testato un Deployment Nginx con un Servizio LoadBalancer per verificare la corretta esposizione delle applicazioni.
- Control Plane: gestisce il cluster (schedulazione, stato, API Server).
- Worker Node: esegue i pod e gestisce il traffico.
- Container Network Interface (CNI) - Calico: permette la comunicazione tra i pod.
- MetalLB gestisce gli IP pubblici su cluster Kubernetes senza cloud provider.
- ModalitΓ Layer 2:
- MetalLB annuncia l'IP assegnato a un servizio LoadBalancer tramite ARP.
- I nodi del cluster rispondono direttamente alle richieste ricevute.
- Hai creato un Deployment Nginx con piΓΉ repliche.
- Il Service con
type: LoadBalancerha ricevuto un IP pubblico da MetalLB. - Il traffico verso lβIP pubblico viene distribuito ai pod di Nginx.
π Guida ufficiale: Istio Helm Installation
helm repo add istio https://istio-release.storage.googleapis.com/charts
>> Output atteso: "istio" has been added to your repositorieshelm repo update
>> Output atteso: Update Complete. Happy Helming!helm install istio-base istio/base -n istio-system --set defaultRevision=default --create-namespace
>> Output atteso:
- NAME: istio-base
- LAST DEPLOYED: Tue Feb 25 09:19:24 2025
- NAMESPACE: istio-system
- STATUS: deployed
- REVISION: 1
- TEST SUITE: None
- NOTES:
- Istio base successfully installed!helm status istio-base -n istio-system
helm get all istio-base -n istio-system
helm ls -n istio-systemhelm install istiod istio/istiod -n istio-system --waithelm ls -n istio-system
helm status istiod -n istio-systemkubectl get deployments -n istio-system --output wide
>> Output atteso:
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS SELECTOR
istiod 1/1 1 1 23m discovery istio=pilotkubectl create namespace istio-ingress
>> Output atteso: namespace/istio-ingress createdhelm install istio-ingress istio/gateway -n istio-ingress --waitkubectl get svc -A
>> Output atteso: Istio ha creato il suo LoadBalancer.kubectl get pods -n istio-ingress
>>OUTPUT atteso:
NAME READY STATUS
istio-ingress-<PodID> 1/1 Runningkubectl get svc -n istio-ingress
>> OUTPUT atteso:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
istio-ingress LoadBalancer x.x.x.x x.x.x.x 15021:30268/TCP,80:31240/TCP,443:32410/TCP- π Gateway VirtualService YAML --Modificare il controller col tuo
- π HelloPod YAML
- π Cartella esempi -- funzionante con l'ultima release di Istio: 17/03/25
mkdir istiohello
cd istiohello
nano gateway-virtualservice.yaml
nano podhello.yamlkubectl get pods -n istio-ingress --show-labels
>> Restituisce il nome del controller da inserire in gateway-virtualservice.yamlkubectl apply -f gateway-virtualservice.yaml
kubectl apply -f podhello.yamlkubectl get pods
kubectl get virtualservice
kubectl get gatewaycurl http://x.x.x.x/hello- Sito ufficiale
- Repository GitHub
- Guida all'installazione su GitHub
- Getting Started
- Documentazione per Compose.YAML
Kompose Γ¨ rilasciato tramite GitHub:
curl -L https://github.com/kubernetes/kompose/releases/download/v1.35.0/kompose-linux-amd64 -o kompose
chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose
kompose version
>> 1.35.0mkdir kompose-example
cd kompose-example
nano docker-compose.yaml # (vedi compose.yaml sopra)
kompose convert
>> INFO Kubernetes file "redis-leader-service.yaml" created
>> INFO Kubernetes file "redis-replica-service.yaml" created
>> INFO Kubernetes file "web-tcp-service.yaml" created
>> INFO Kubernetes file "redis-leader-deployment.yaml" created
>> INFO Kubernetes file "redis-replica-deployment.yaml" created
>> INFO Kubernetes file "web-deployment.yaml" createdkubectl apply -f web-tcp-service.yaml,redis-leader-service.yaml,redis-replica-service.yaml,web-deployment.yaml,redis-leader-deployment.yaml,redis-replica-deployment.yaml
>> service/web-tcp created
>> service/redis-leader created
>> service/redis-replica created
>> deployment.apps/web created
>> deployment.apps/redis-leader created
>> deployment.apps/redis-replica createdkubectl describe svc web-tcp
...
Type: LoadBalancer
LoadBalancer Ingress: x.x.x.x (VIP)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal IPAllocated 12s metallb-controller Assigned IP ["x.x.x.x"]
Normal nodeAssigned 9s metallb-speaker announcing from node "ubuntuworker" with protocol "layer2"curl http://x.x.x.x:8080
kubectl delete -f web-tcp-service.yaml,redis-leader-service.yaml,redis-replica-service.yaml,web-deployment.yaml,redis-leader-deployment.yaml,redis-replica-deployment.yamlQuesta guida descrive come clonare, configurare e avviare Stack4Things su Kubernetes.
- Cloniamo il repository ufficiale di Stack4Things:
git clone https://github.com/MDSLab/Stack4Things_k3s_deployment.git- Spostiamoci nella cartella del progetto:
cd Stack4Things_k3s_deployment- Applicare i file YAML al cluster Kubernetes:
cd yaml_file
kubectl apply -f .- Verificare che i Pod siano attivi:
kubectl get pods- Verificare i servizi disponibili:
kubectl get svcAll'interno della cartella troverai:
- ComposeDeployment
deployments/β Contiene i file YAML per la definizione dei Pod, Deployment e Service di S4T.storage/β Definizioni di PersistentVolumeClaim (PVC) per la gestione dei dati..envβ File con le variabili dβambiente necessarie per l'installazione.configmaps/β Configurazioni personalizzate per i servizi di S4T in Kubernetes.
- ConfigurazioneIstio
-
istio/β Configurazioni di Istio per il bilanciamento del traffico e il gateway di accesso.
-
- Estrarre la cartella ZIP sul proprio sistema.
- Accedere alla cartella
- Applicare i file YAML al cluster Kubernetes:
Kubectl apply -f .- Verificare che i Pod siano attivi:
kubectl get pods- Verificare i servizi disponibili:
kubectl get svc- π Definizione file yaml qui
Creiamo una cartella per i file di configurazione di Istio:
mkdir istioconfApriamo un nuovo file per definire il Gateway e il VirtualService:
nano gateway-virtualservice-istio.yaml
kubectl apply -f .Verifichiamo che le risorse siano state create correttamente:
kubectl describe virtualservice iotronic-uiVerifichiamo il servizio istio-ingress per ottenere l'IP pubblico del bilanciatore di carico:
kubectl get svc istio-ingress -n istio-ingressπ Esempio di output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingress LoadBalancer 10.x.x.x x.x.x.x 15021:30152/TCP,80:31152/TCP,443:30936/TCP 3d3hVerifichiamo la creazione del VirtualService:
kubectl get virtualserviceπ Esempio di output:
NAME GATEWAYS HOSTS AGE
iotronic-ui ["iotronic-ui-gateway"] ["*"] 11mControlliamo il Gateway:
kubectl get gatewayπ Esempio di output:
NAME AGE
iotronic-ui-gateway 12mUtilizziamo curl per testare l'accesso alla UI di Iotronic tramite l'IP di istio-ingress:
curl x.x.x.x/iotronic-uiπ Output atteso:
>> Apache Default Pageπ 5. Configurare il Port Forwarding --opzionale tramite tailscale Per esporre il servizio localmente:
kubectl port-forward --address 0.0.0.0 svc/istio-ingress 8100:80 -n istio-ingressOra possiamo accedere alla UI da un browser utilizzando gli indirizzi seguenti:
>> http://x.x.x.x:8100/iotronic-ui
>> http://x.x.x.x:8100/horizon/auth/login/?next=/horizon/