⚙️

Azure CLI & Basics

Cài đặt Azure CLI

# macOS
brew install azure-cli

# Ubuntu/Debian
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

# Windows (PowerShell)
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi

# Login
az login

# Set subscription
az account set --subscription "My Subscription"

# List resource groups
az group list --output table

Tạo Resource Group & VM

# Tạo Resource Group
az group create \
    --name myResourceGroup \
    --location southeastasia

# Tạo Virtual Machine
az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image Ubuntu2204 \
    --size Standard_B2s \
    --admin-username azureuser \
    --generate-ssh-keys

# Mở port 80 cho web
az vm open-port \
    --resource-group myResourceGroup \
    --name myVM \
    --port 80

# SSH vào VM
ssh azureuser@<public-ip>

Azure Functions

📘 Serverless trên Azure

Azure Functions tương tự AWS Lambda. Hỗ trợ nhiều ngôn ngữ: C#, JavaScript, Python, Java, PowerShell.

HTTP Trigger Function (Python)

# function_app.py
import azure.functions as func
import json

app = func.FunctionApp()

@app.function_name(name="HttpTrigger")
@app.route(route="hello", methods=["GET", "POST"])
def hello(req: func.HttpRequest) -> func.HttpResponse:
    """HTTP triggered function"""
    
    name = req.params.get('name')
    
    if not name:
        try:
            req_body = req.get_json()
            name = req_body.get('name')
        except ValueError:
            name = 'World'
    
    if name:
        return func.HttpResponse(
            json.dumps({
                "message": f"Xin chào, {name}!",
                "status": "success"
            }),
            mimetype="application/json",
            status_code=200
        )
    else:
        return func.HttpResponse(
            "Vui lòng truyền name parameter",
            status_code=400
        )

Deploy Function

# Cài đặt Azure Functions Core Tools
npm install -g azure-functions-core-tools@4

# Tạo function app trên Azure
az functionapp create \
    --resource-group myResourceGroup \
    --consumption-plan-location southeastasia \
    --runtime python \
    --runtime-version 3.11 \
    --functions-version 4 \
    --name myFunctionApp \
    --storage-account mystorageaccount

# Deploy
func azure functionapp publish myFunctionApp
🔄

Azure DevOps

Azure Pipelines (YAML)

Azure DevOps cung cấp CI/CD pipelines mạnh mẽ với YAML configuration.

# azure-pipelines.yml
trigger:
  - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  pythonVersion: '3.11'

stages:
  - stage: Build
    jobs:
      - job: BuildJob
        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
          
          - script: |
              python -m pip install --upgrade pip
              pip install -r requirements.txt
            displayName: 'Install dependencies'
          
          - script: |
              pip install pytest
              pytest tests/ --junitxml=test-results.xml
            displayName: 'Run tests'
          
          - task: PublishTestResults@2
            inputs:
              testResultsFiles: 'test-results.xml'

  - stage: Deploy
    dependsOn: Build
    condition: succeeded()
    jobs:
      - deployment: DeployToProduction
        environment: 'production'
        strategy:
          runOnce:
            deploy:
              steps:
                - script: echo "Deploying to production..."
                  displayName: 'Deploy'
💡 Azure DevOps vs GitHub Actions:
• Azure DevOps: Tích hợp sâu với Azure, enterprise features
• GitHub Actions: Đơn giản hơn, cộng đồng marketplace lớn

Azure Kubernetes Service (AKS)

Tạo AKS Cluster

# Tạo AKS cluster
az aks create \
    --resource-group myResourceGroup \
    --name myAKSCluster \
    --node-count 2 \
    --node-vm-size Standard_B2s \
    --enable-managed-identity \
    --generate-ssh-keys

# Lấy credentials
az aks get-credentials \
    --resource-group myResourceGroup \
    --name myAKSCluster

# Verify connection
kubectl get nodes

# Deploy app
kubectl apply -f deployment.yaml