Servizi e Repliche

Esercizio

Sulla macchina host scaricare le immagini nginx:latest e nginx:1.16 e porle nel registry locale:

docker pull nginx
docker pull nginx:1.16
docker tag nginx:latest localhost:5000/nginx:latest
docker push localhost:5000/nginx:latest
docker tag nginx:1.16 localhost:5000/nginx:1.16
docker push localhost:5000/nginx:1.16

Entrare nel contenitore con Kind.

Scrivere il file di configurazione ~/scripts/nginx-depl.yml:

vim ~/scripts/nginx-depl.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: localhost:5000/nginx
        ports:
        - containerPort: 8080

Nginxdepl

Direttive globali:

  • apiVersion: apps/v1 - Versione API. Diversa per tipi diversi.
  • kind: Deployment - Tipo di oggetto da produrre

Seguono due sezioni:

  • metadata: - Dati globali sull’oggetto
  • spec: - Specifiche di costruzione dell’oggetto. E’ il desired state

Creare l’oggetto tramite il file:

kubectl apply -f ~/scripts/nginx-depl.yml

File interno di specifiche del deployment. Vederlo con:

kubectl get deployment nginx-deployment -o yaml

Piu complesso e completo di quello sottoposto

Include la terza sezione status:

...
status:                                             
  availableReplicas: 1                              
  conditions:                                       
  - lastTransitionTime: "2022-10-30T11:40:04Z"      
    lastUpdateTime: "2022-10-30T11:40:04Z"          
    message: Deployment has minimum availability.   
...                                                         
  observedGeneration: 1 
  readyReplicas: 1 
  replicas: 1   
  updatedReplicas: 1

Questo è il current state

Cambiare il numero di repliche

Stato corrente:

kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           4h48m
kubectl get replicasets
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5cbffcb86d   1         1         1       4h49m
kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5cbffcb86d-qwthn   1/1     Running   0          4h49m

Cambiamento: editare il file di specifiche ~/scripts/nginx-depl.yml

Cambiare

  replicas: 1

in

  replicas: 3
'''

Sottomettere i cambiamenti:

```bash
kubectl apply -f ~/scripts/nginx-depl.yml

Verificare il cambiamento con i comandi:

kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/3     3            2           4h57m
kubectl get replicasets
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5cbffcb86d   3         3         2       4h57m
kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5cbffcb86d-nkpgw   1/1     Running   0          17s
nginx-deployment-5cbffcb86d-qwthn   1/1     Running   0          4h57m
nginx-deployment-5cbffcb86d-vkvh5   1/1     Running   0          17s
kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           4h57m
kubectl get replicasets
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5cbffcb86d   3         3         3       4h57m

Il current state converge velocemente al nuovo desired state

Cambiare l'immagine

Editare il file di specifiche scripts/nginx-depl.yml

Cambiare

        image: localhost:5000/nginx

in

        image: localhost:5000/nginx:1.16

Sottomettere i cambiamenti:

kubectl apply -f ~/scripts/nginx-depl.yml

Verificare lo stato di aggiornamento:

kubectl get replicasets
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5cbffcb86d   3         3         3       5h6m
nginx-deployment-65bd9f5d9d   1         1         0       9s
kubectl get pods
NAME                                READY   STATUS              RESTARTS   AGE
nginx-deployment-5cbffcb86d-nkpgw   1/1     Terminating         0          9m11s
nginx-deployment-5cbffcb86d-qwthn   1/1     Running             0          5h6m
nginx-deployment-5cbffcb86d-vkvh5   1/1     Running             0          9m11s
nginx-deployment-65bd9f5d9d-9znxk   1/1     Running             0          17s
nginx-deployment-65bd9f5d9d-bms9t   0/1     ContainerCreating   0          6s

e presto converge completamente al nuovo stato

Creare un Servizio

File di specifiche ~/scripts/nginx-serv.yml:

vim ~/scripts/nginx-serv.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Nginxsvc

Creare con:

kubectl apply -f ~/scripts/nginx-serv.yml

Corrispondenza col Deployment

Ngincorr

Vedere pods e servizio:

kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
nginx-deployment-65bd9f5d9d-9znxk   1/1     Running   0          31m   10.244.2.3   std-worker2   <none>           <none>
nginx-deployment-65bd9f5d9d-bms9t   1/1     Running   0          30m   10.244.1.4   std-worker    <none>           <none>
nginx-deployment-65bd9f5d9d-tf4kp   1/1     Running   0          30m   10.244.2.4   std-worker2   <none>           <none>
kubectl describe service nginx-service
Name:              nginx-service
...
Type:              ClusterIP
...
IPs:               10.96.34.171
Port:              <unset>  80/TCP
TargetPort:        8080/TCP
Endpoints:         10.244.1.4:8080,10.244.2.3:8080,10.244.2.4:8080
kubectl get services
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP   6h34m
nginx-service   ClusterIP   10.96.34.171   <none>        80/TCP    8m54s

Cancellare Deployment e Servizio

Cancellare deployment direttamente:

kubectl delete deployment nginx-deployment
deployment.apps "nginx-deployment" deleted

Il servizio non viene cancellato ma se ispezionato con

kubectl describe service nginx-service

non vi sono più Endpoints

Cancellare il servizio tramite file di specifiche:

kubectl delete -f ~/scripts/nginx-serv.yml
service "nginx-service" deleted