KUBERNETES - Note (Prof. Fischetti P.) host: sysadmin@pbmint:~$ hostnamectl Static hostname: pbmint Icon name: computer-laptop Chassis: laptop Machine ID: f3d85c83fd6e458aba76dbf56f683032 Boot ID: 88b253637ed045c58d2169cf469af5f6 Operating System: Linux Mint 20 Kernel: Linux 5.0.0-32-generic Architecture: x86-64 REF:https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ 1. Download the latest release with the command: $ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" 2. Validate the binary (optional): >>>$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256" >>>$ echo "$(cat kubectl.sha256) kubectl" | sha256sum --check 3. Install kubectl: sysadmin@pbmint:~$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl sysadmin@pbmint:~$ kubectl version Client Version: v1.30.2 Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 The connection to the server localhost:8080 was refused - did you specify the right host or port? sysadmin@pbmint:~$ kubectl version -o yaml clientVersion: buildDate: "2024-06-11T20:29:44Z" compiler: gc gitCommit: 39683505b630ff2121012f3c5b16215a1449d5ed gitTreeState: clean gitVersion: v1.30.2 goVersion: go1.22.4 major: "1" minor: "30" platform: linux/amd64 kustomizeVersion: v5.0.4-0.20230601165947-6ce0bf390ce3 Se appare il messaggio di errore: The connection to the server localhost:8080 was refused - did you specify the right host or port? Vedi anche: https://minikube.sigs.k8s.io/docs/start/?arch=%2Fwindows%2Fx86-64%2Fstable%2F.exe+download: $ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 $ sudo install minikube-linux-amd64 /usr/local/bin/minikube && rm minikube-linux-amd64 Aggiungere l'uente al gruppo docker sysadmin@pbmint:~$ sudo usermod -aG docker $USER && newgrp docker Avviare minikube: $ sudo minikube start --driver=docker Stop: $ minkube stop Cancellare: $ minikube delete in dettaglio avvio: $ minikube start --driver=docker πŸ˜„ minikube v1.33.1 on Linuxmint 20 ✨ Using the docker driver based on user configuration πŸ“Œ Using Docker driver with root privileges πŸ‘ Starting "minikube" primary control-plane node in "minikube" cluster 🚜 Pulling base image v0.0.44 ... πŸ”₯ Creating docker container (CPUs=2, Memory=2200MB) ... 🐳 Preparing Kubernetes v1.30.0 on Docker 26.1.1 ... β–ͺ Generating certificates and keys ... β–ͺ Booting up control plane ... β–ͺ Configuring RBAC rules ... πŸ”— Configuring bridge CNI (Container Networking Interface) ... πŸ”Ž Verifying Kubernetes components... β–ͺ Using image gcr.io/k8s-minikube/storage-provisioner:v5 🌟 Enabled addons: storage-provisioner, default-storageclass πŸ„ Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default $ kubectl version Client Version: v1.30.2 Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 Server Version: v1.30.0 sysadmin@pbmint:~$ kubectl cluster-info dump { "kind": "NodeList", "apiVersion": "v1", "metadata": { "resourceVersion": "546" }, "items": [ { "metadata": { "name": "minikube", "uid": "101a9b5a-f92f-4585-8264-2021b956de35", ......................................................................... "items": [] } { "kind": "PodList", "apiVersion": "v1", "metadata": { "resourceVersion": "546" }, "items": [] } Controlliamo i nodi del cluster: $ kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready control-plane 5m27s v1.30.0 l E' stato aggiunto in automatico un container: $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d229ad90fe3 gcr.io/k8s-minikube/kicbase:v0.0.44 "/usr/local/bin/entr…" 5 minutes ago Up 5 minutes 127.0.0.1:32787->22/tcp, 127.0.0.1:32786->2376/tcp, 127.0.0.1:32785->5000/tcp, 127.0.0.1:32784->8443/tcp, 127.0.0.1:32783->32443/tcp minikube Controolo dello stato dei componenti di Kubernetes: $ kubectl get componentstatus Warning: v1 ComponentStatus is deprecated in v1.19+ NAME STATUS MESSAGE ERROR controller-manager Healthy ok scheduler Healthy ok etcd-0 Healthy ok Controllo daemon kubernetes: $ kubectl get daemonSets --namespace=kube-system kube-proxy NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE kube-proxy 1 1 1 1 1 kubernetes.io/os=linux 26m $ kubectl get deployments --namespace=kube-system NAME READY UP-TO-DATE AVAILABLE AGE coredns 1/1 1 1 29m Controllo dei servizi kubernetes: $ kubectl get services --namespace=kube-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP,9153/TCP 29m lista dei contesti definiti sysadmin@pbmint:~$ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE * minikube minikube minikube default Creazione di una sepmlice immagine (un web server nginx): $ cd my_web_server ~/my_web_server$ ls Dockerfile index.html ~/my_web_server$ cat Dockerfile FROM nginx COPY index.html /usr/share/nginx/html/index.html ~/my_web_server$ cat index.html: Hello, ciao ~/my_web_server$ sudo docker build -t my-ws:1.0 . Controllo la lista delle immagini: $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE my-ws 1.0 ec358acd765f 36 seconds ago 233MB my-flask-mongodb-html 1.0.0 f759475ea76f 2 weeks ago 349MB gcr.io/k8s-minikube/kicbase v0.0.44 5a6e59a9bdc0 8 weeks ago 1.26GB Controlli i nodi Kubernetes: $ kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready control-plane 2d7h v1.30.0 Lista pods vuota: $ kubectl get pods No resources found in default namespace. Creiamo un pod: $ mkdir mykwebserver $ cd mykwebserver Creaiamo un file di testo: pod.yml: ~/mykwebserver$ ls pod.yml svc-local.yml ~/mykwebserver$ cat pod.yml apiVersion: v1 kind: Pod metadata: name: first-pod labels: project: my-nginx spec: containers: - name: my-nginx image: my_ws:1.0 imagePullPolicy: Never ports: - containerPort: 8080 N.B. imagePullPolicy: Never perche vogliamo utilizzare un'immagine locale e non fare pull dal docker hub $ kubectl apply -f pod.yml pod/first-pod created Controlliamo che il pod si sia avviato senza errori (STATU=Running) altrimenti (se STATUS= Err... ) controllare con: $ kubectl logs first-pod Molto probabile che l'errore sia dovuto all'immagine che K8s cerca su Docker hub (vedi parametro imagePullPolicy: IfNotPresent (Cerca su Hub)|Always (cerca su registry)| Never (In locale) vedi:https://kubernetes.io/docs/concepts/containers/images/) un'immagine che abbiamo creato in locale $ kubectl get pods NAME READY STATUS RESTARTS AGE first-pod 1/1 Running 0 16h Creiamo il nostro servizio che ci permettera' l'accesso al container nginx: cioe' un file per la creazione del servizio ~/mykwebserver$ cat svc-local.yml apiVersion: v1 kind: Service metadata: name: my-nginx spec: type: NodePort ports: - port: 8080 protocol: TCP targetPort: 80 nodePort: 31111 selector: project: my-nginx NB ho specificato: type: NodePort dato che vogliamo accedere all'applicazione nginx dall'esterno del pod La porta del pod:31111 e la porta interna e':80 sysadmin@pbmint:~/my-knode$ kubectl apply -f svc-local.yml service/svc-local created ~/mykwebserver$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 6d6h my-nginx NodePort 10.108.132.3 8080:31111/TCP 30h Se voglio info dettagliate sul pod: ~$ kubectl describe pod first-pod Name: first-pod Namespace: default Priority: 0 Service Account: default Node: minikube/192.168.49.2 Start Time: Sat, 06 Jul 2024 11:10:48 +0200 Labels: project=my-nginx Annotations: Status: Running IP: 10.244.0.26 IPs: IP: 10.244.0.26 Containers: my-nginx: Container ID: docker://dac53010fcda21a7736ddb42ebf5bbc64ae77862146554f911c4f537d6e0fd53 Image: my_ws:1.0 Image ID: docker://sha256:5fc802aae9e9a2542617208aa165be638f9dd9fcd9b9eb7d8f19a48ed0d371d3 Port: 8080/TCP Host Port: 0/TCP State: Running Started: Sat, 06 Jul 2024 11:10:54 +0200 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-gwqx4 (ro) Conditions: Type Status PodReadyToStartContainers True Initialized True Ready True ContainersReady True PodScheduled True Volumes: kube-api-access-gwqx4: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: DownwardAPI: true QoS Class: BestEffort Node-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: Che conferma la presenza del solo container: my-nginx Ora proviamo a chiedere la pagina index.html al server web nginx: prima devo cercare l'ip del nodo: ~$ minikube ip 192.168.49.2 Proviamo se la richiesta dell'url e' soddisfatta: sysadmin@pbmint:~$ curl 192.168.49.2:31111 Hello, Ciao OKOKOKOK Se voglio accedere al container my-nginx cioe' voglio una shell: :~$ kubectl exec -it first-pod -c my-nginx -- /bin/bash root@first-pod:/# root@first-pod:/# ls bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@first-pod:/# cd /usr/share/nginx/html root@first-pod:/usr/share/nginx/html# ls 50x.html index.html root@first-pod:/usr/share/nginx/html# cat index.html Hello, Ciao root@first-pod:/usr/share/nginx/html# uname -a Linux first-pod 5.0.0-32-generic #34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019 x86_64 GNU/Linux Questa versione non ha supporto di rete installato: root@first-pod:/usr/share/nginx/html# ifconfig bash: ifconfig: command not found root@first-pod:/usr/share/nginx/html# netstat bash: netstat: command not found root@first-pod:/usr/share/nginx/html# ss bash: ss: command not found Installiamolo: root@first-pod:/usr/share/nginx/html# apt-get update -y root@first-pod:/usr/share/nginx/html# apt-get install -y net-tools root@first-pod:/usr/share/nginx/html# netstat -tlpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro tcp6 0 0 :::80 :::* LISTEN 1/nginx: master pro root@first-pod:/usr/share/nginx/html# ifconfig eth0: flags=4163 mtu 1500 inet 10.244.0.26 netmask 255.255.0.0 broadcast 10.244.255.255 inet6 fe80::3428:dbff:fea1:73d9 prefixlen 64 scopeid 0x20 ether 36:28:db:a1:73:d9 txqueuelen 0 (Ethernet) RX packets 7184 bytes 9947762 (9.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1573 bytes 105678 (103.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 root@first-pod:/usr/share/nginx/html# curl localhost Hello, Ciao REPLICHE Facciamo pulizia del pod creato e del servizio: ~/mykwebserver$ kubectl get pods NAME READY STATUS RESTARTS AGE first-pod 1/1 Running 0 2d22h ~/mykwebserver$ kubectl delete pod first-pod pod "first-pod" deleted ~/mykwebserver$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 6d22h my-nginx NodePort 10.108.132.3 8080:31111/TCP 46h ~/mykwebserver$ kubectl delete svc my-nginx service "my-nginx" deleted ~/mykwebserver$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 6d22h Creiamo un file YAML per il deployment: ~/mykwebserver$ cat deploy.yml apiVersion: apps/v1 kind: Deployment metadata: name: my-deploy spec: replicas: 3 selector: matchLabels: project: my-nginx template: metadata: labels: project: my-nginx spec: containers: - name: my-nginx image: my_ws:1.0 imagePullPolicy: Never ports: - containerPort: 8080 ~/mykwebserver$ kubectl apply -f deploy.yml deployment.apps/my-deploy created Controllo: ~/mykwebserver$ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE my-deploy 0/3 3 0 13s Non ancora creato, aspettare qualche secondo ~/mykwebserver$ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE my-deploy 3/3 3 3 35s OK ~/mykwebserver$ kubectl get pods NAME READY STATUS RESTARTS AGE my-deploy-5945fff6f6-fp5qd 1/1 Running 0 29s my-deploy-5945fff6f6-mg2b4 1/1 Running 0 28s my-deploy-5945fff6f6-rccpt 1/1 Running 0 28s Ho 3 pod come spcificato nel file deploy.yml con replicas: 3 Provo ora a simulare un crash di un pod ad esempio: ~/mykwebserver$ kubectl delete pod my-deploy-5945fff6f6-rccpt pod "my-deploy-5945fff6f6-rccpt" deleted Controllo: ~/mykwebserver$ kubectl get pods NAME READY STATUS RESTARTS AGE my-deploy-5945fff6f6-5vn78 0/1 ContainerCreating 0 7s my-deploy-5945fff6f6-fp5qd 1/1 Running 0 8m57s my-deploy-5945fff6f6-mg2b4 1/1 Running 0 8m56s Kubernetes ha riportato a 3 il numero di istanze: ~/mykwebserver$ kubectl get pods NAME READY STATUS RESTARTS AGE my-deploy-5945fff6f6-5vn78 1/1 Running 0 14s my-deploy-5945fff6f6-fp5qd 1/1 Running 0 9m4s my-deploy-5945fff6f6-mg2b4 1/1 Running 0 9m3s RIFERIMENTI: https://kubernetes.io/ https://minikube.sigs.k8s.io/ https://awstip.com/creating-a-simple-web-server-with-docker-a-step-by-step-guide-to-running-your-web-server-as-a-2992ce2051e3