Romer Alvarez
← Tutti gli articoli

Deploy di applicazioni di base su Azure Kubernetes Service (AKS)

Deploy di applicazioni su Azure Kubernetes Service (AKS): introduzione di base.

Introduzione

In questo articolo vediamo come fare il deploy di applicazioni con Azure Kubernetes Service (AKS). Andremo dalla creazione di un Azure Container Registry al deploy di immagini container su AKS, con esempi pratici e diagrammi.

Relazione tra ACR e AKS

Faremo l’esercizio proposto da Microsoft.

Si compone di 4 esercizi:

Esercizio 1: provisioning di Azure Container Registry (ACR) e Azure Kubernetes Service (AKS)

Passo 1: creare un Azure Container Registry (ACR)

  1. Accedi al portale di Azure.
  2. Cerca Container Registries nella barra di ricerca. Fai clic su + Create e compila i seguenti dati:
Impostazione Valore
Subscription La tua sottoscrizione Azure
Resource Group acr-01-RG
Registry name Deve essere un nome univoco; il nostro è romeralvarezme
Region Europe West
Availability zones Nessuna
SKU Basic

Passo 2: creare una Azure Virtual Network e un cluster AKS

Creazione di una rete virtuale e di un cluster AKS.

  1. Cerca Virtual Network nella barra di ricerca. Fai clic su + Create e compila i seguenti dati:
Impostazione Valore
Subscription La tua sottoscrizione Azure
Resource Group aks-01-RG
Virtual network name vnet-01
Region [La stessa regione di prima]
  1. Cerca Kubernetes Service nella barra di ricerca. Fai clic su + Create e compila i seguenti dati:
Impostazione Valore
Subscription La tua sottoscrizione Azure
Resource Group aks-01-RG
Cluster preset configuration Dev/Test
Kubernetes cluster name aks-01
Region [La stessa regione di prima]
Availability zones Nessuna
AKS pricing tier Free
Kubernetes version [Valore predefinito]
Automatic upgrade Disabled
Node size Standard B4ms
Scale method Manual
Node count 2
Impostazione Valore
Cluster subnet aks-subnet (10.0.0.0/20)
Kubernetes service address range 172.16.0.0/22
Kubernetes DNS service IP address 172.16.3.254
DNS name prefix aks-01-dns
Enable private cluster Disabled
Set authorized IP ranges Disabled
Network policy None
  1. Ora creiamo il node pool Windows:
Impostazione Valore
Node pool name w1pool
Mode User
OS type Windows
Availability zone Nessuna
Enable Azure spot instances Disabled
Node size Standard B4s_v2
Scale method Manual
Node count 2
Max pods per node 30
Enable public IP per node Disabled
  1. Nella scheda Integrations selezioniamo il registry creato prima, ci assicuriamo di disattivare Azure Monitor e Azure Policy e selezioniamo Review + Create e poi Create.

Ora aspettiamo che le risorse vengano create, e l’esercizio 1 è concluso. Avremo:

Il risultato sarà qualcosa del genere: ACR e AKS

Esercizio 2: creare immagini container Linux e Windows e salvarle in Azure Container Registry

In questo esercizio creeremo immagini container Linux e Windows e le salveremo in Azure Container Registry (ACR).

Passo 1: preparare l’ambiente

mkdir ~/image-l01
cd ~/image-l01
vim server.js

Passo 2: creare i file del progetto Linux

const http = require('http')
const port = 80
const server = http.createServer((request, response) => {
  response.writeHead(200, {'Content-Type': 'text/plain'})
  response.write('Hello World from Node\n')
  response.end('Version: ' + process.env.NODE_VERSION + '\n')
})
server.listen(port)
console.log(`Server running at http://localhost: ${port}`)
{
  "name": "helloworld",
  "version": "1.0.0",
  "description": "Sample app for ACR Build",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "license": "MIT"
}
FROM node:20.2-alpine
COPY . /src
RUN cd /src && npm install
EXPOSE 80
CMD ["node", "/src/server.js"]
ACR_RG='acr-01-RG'
ACR_NAME=$(az acr list --resource-group $ACR_RGNAME --query "[].name" --output tsv)
az acr build --registry $ACR_NAME --image hellofromnode:v1.0 .

Passo 3: creare i file del progetto Windows

mkdir ~/image-w01
cd ~/image-w01
git clone https://github.com/Azure-Samples/dotnetcore-docs-hello-world.git
cd ~/image-w01/dotnetcore-docs-hello-world
az acr build --registry $ACR_NAME --image hellofromdotnet:v1.0 --platform windows --file Dockerfile.windows .

Andando nel nostro registry, vedremo che ci sono entrambe le immagini:

Repository ACR

Esercizio 3: deploy dei container su Azure Kubernetes Service

In questa attività creeremo due namespace nel nostro cluster AKS e faremo il deploy delle immagini create nell’esercizio precedente.

Passo 1: creare i namespace

  1. Nel portale di Azure, cerchiamo Kubernetes Services e selezioniamo il nostro cluster AKS.
  2. Nella scheda Kubernetes Resources selezioniamo Namespaces e ne creiamo uno chiamato dev-node e un altro chiamato dev-dotnet.

Passo 2: creare i manifest per il deploy delle immagini Linux e Windows

  1. Nella Cloud Shell creiamo la seguente cartella:
mkdir ~/aks-l01
cd ~/aks-l01
  1. Creiamo il seguente manifest Kubernetes, aks-deployment-l01.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellofromnode-deployment
  labels:
    environment: dev
    app: hellofromnode
spec:
  replicas: 1
  template:
    metadata:
      name: hellofromnode
      labels:
        app: hellofromnode
    spec:
      nodeSelector:
        "kubernetes.io/os": linux
      containers:
      - name: hellofromnode
        image: ACR_NAME.azurecr.io/hellofromnode:v1.0
        resources:
          limits:
            cpu: 1
            memory: 800M
        ports:
          - containerPort: 80
  selector:
    matchLabels:
      app: hellofromnode
---
apiVersion: v1
kind: Service
metadata:
  name: hellofromnode-service
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
  selector:
    app: hellofromnode
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellofromdotnet-deployment
  labels:
    environment: dev
    app: hellofromdotnet
spec:
  replicas: 1
  template:
    metadata:
      name: hellofromdotnet
      labels:
        app: hellofromdotnet
    spec:
      nodeSelector:
        "kubernetes.io/os": windows
      containers:
      - name: hellofromdotnet
        image: ACR_NAME.azurecr.io/hellofromdotnet:v1.0
        resources:
          limits:
            cpu: 1
            memory: 800M
        ports:
          - containerPort: 80
  selector:
    matchLabels:
      app: hellofromdotnet
---
apiVersion: v1
kind: Service
metadata:
  name: hellofromdotnet-service
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
  selector:
    app: hellofromdotnet
  1. Sostituiamo ACR_NAME con il nome del nostro Azure Container Registry (ACR) ed eseguiamo il seguente comando per il deploy dell’immagine Linux:
sed -i "s/ACR_NAME/$ACR_NAME/" ./aks-deployment-w01.yaml
kubectl apply -f ./aks-deployment-w01.yaml -n dev-node

Passo 3: deploy dei manifest nei rispettivi namespace:

  1. Nella sessione bash, ci colleghiamo al cluster con i seguenti comandi:
AKSRG='aks-01-RG'
AKSNAME='aks-01'
az aks get-credentials --resource-group $AKSRG --name $AKSNAME
  1. Verifichiamo che la connessione funzioni:
kubectl get nodes
  1. Facciamo il deploy dei manifest nei rispettivi namespace:
kubectl apply -f aks-l01/aks-deployment-l01.yaml -n dev-node
kubectl apply -f aks-w01/aks-deployment-w01.yaml -n dev-dotnet
  1. Verifichiamo che i pod siano stati creati:
kubectl get pods -n dev-node
kubectl get pods -n dev-dotnet
  1. Verifichiamo che i servizi siano stati creati:
kubectl get services -n dev-node
kubectl get services -n dev-dotnet

E infine vediamo che pod e servizi sono stati creati correttamente:

romer [ ~ ]$ kubectl get pods -n dev-node
NAME                                READY   STATUS    RESTARTS   AGE
hellofromnode-deployment-65bb76d95f-8wp4c   1/1     Running   0          4m41s

romer [ ~ ]$ kubectl get pods -n dev-dotnet
NAME                                 READY   STATUS    RESTARTS   AGE
hellofromdotnet-deployment-66445d8f4-jwtv7   1/1     Running   0          4m39s

romer [ ~ ]$ kubectl get services -n dev-dotnet
NAME                           TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)          AGE
hellofromdotnet-service        LoadBalancer   172.16.0.112    4.175.193.225     80:30614/TCP     4m44s

romer [ ~ ]$ kubectl get services -n dev-node
NAME                         TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)          AGE
hellofromnode-service        LoadBalancer   172.16.3.220    4.175.193.210     80:32568/TCP     4m53s

Esercizio 4: eliminare tutte le risorse

È fondamentale eliminare tutte le risorse create per non sostenere costi inutili. Possiamo farlo così, nella stessa sessione bash:

az group delete --resource-group $ACR_RG --yes --no-wait
az group delete --resource-group $AKSRG --yes --no-wait