Search This Blog

2021-04-17

My Azure Study Note 07- Container, Kubernetes, Docker

 Containers

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

Advantage of using Containers

  • Predictability -  The same package is deployed from the dev machine to test to prod
  • Performance - Containers goes up in seconds vs minutes in VM
  • Density - One server can run thousands of containers vs dozens of VMs
Disadvantage
 Isolation- Containers share the same of, so isolation is lighter than VM

Build and Deploy image to ACR using VS Code

Step 1. Install Docker for windows
Step 2. Install Azure CLI
Step 3. Open project in VS code and Add extension "Docker"
Step 4. Open Explorer in VS code, right click on Dockerfile and Build Image

Content of docker file is as below

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 5004
ENV ASPNETCORE_URLS=http://+:5004

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["cart.csproj", "./"]
RUN dotnet restore "cart.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "cart.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "cart.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "cart.dll"]

Step 5. In VS code Docker extension check the Images list
Step 6. Right click the "latest" image and run
Step 7. In VS code Docker extension check the Containers list
Step 8. Right click the target container and "Open in Browser"
Step 9. It will run the application in browser from container
Step 10. Click connect registry -> Azure
Step 11. Right click Subscription -> Create registry-> Type a name like "myacr"-> Hit Enter
Step 12. Select a SKU = Basic, Select RG , select Location
This will throw exception The subscription is not registered to use namespace 'Microsoft.ContainerRegistry'. See https://aka.ms/rps-not-found for how to register subscriptions."
Step 13. Go to the azure portal-> Subscription-> Resource Providers-> Select "Microsoft.ContainerRegistry" and Register
Step 14. In cloud shell check the registration status by following command
az provider show --namespace Microsoft.ContainerRegistry -o table
Step 13. Perform step 11 and 12 once again
Step 14. Check in azure portal-> Container registry
Step 15. In VS Code ->Images->Latest-> Push image
Step 16. In Azure portal-> Container Registry -> Repository and check the image

Create AKS Cluster

Step 1. In VS Code Terminal run below command to create aks cluster
az aks create --resource-group readit-app-rg --name cart-aks --node-count 1 --generate-ssh-keys --attach-acr readitacr2 --node-vm-size Standard_DS1_v2

Step 2. Install CLI of AKS to run kubctl command to perform various option
az aks install-cli

Set path for Kubectl command
set PATH=%PATH%;"C:\Users\ManabRanjan\.azure-kubectl"

Step 3. Get credential to access aks
az aks get-credentials --resource-group readit-app-rg --name cart-aks

If you have multiple subscriptions, check the subscription id
az login
az account list --output table

Set the subscription id
az account set --subscription 0da7d86b-a610-4f8a-97e8-6c44db344baa

Step 4. To check everything is fine, run below command
kubectl get nodes

Output as below i.e one node is running. Node is a actual VM where kubernet is setup 
NAME                                STATUS   ROLES   AGE     VERSION
aks-nodepool1-40111762-vmss000000   Ready    agent   3m27s   v1.18.14

Step 5. Go to the portal and type aks
 In Cart-aks, go to the node pool, it will show a node pool with Node count = 1

Step 6.
Deploy the app in that AKS
in VS Code, open deployment.yaml

apiVersionapps/v1
kindDeployment
metadata:
  namereadit-cart
spec:
  selector:
    matchLabels:
      appreadit-cart
  template:
    metadata:
      labels:
        appreadit-cart
    spec:
      containers:
      - namereadit-cart
        imagereaditacr2.azurecr.io/cart:latest
        resources:
          limits:
            memory"128Mi"
            cpu"500m"
        ports:
         - containerPort80
---
apiVersionv1
kindService
metadata:
  namereadit-cart
spec:
  typeLoadBalancer
  ports:
  - port80
    targetPort5004
  selector:
    appreadit-cart      


Run the below command as below for the actual deployment

kubectl apply -f deployment.yaml

Step 7. In the azure portal, go to the aks cart-aks, go to the "Services and ingresses". 

The first 3 has been created by default but the actual service is readit-cart

Go to the workloads where the actual image can be seen found under.

Step 8. Go to the "Services and ingresses", click on readit-cart, check the Pod is running and then click on the external IP. It will open the application in the local browser from the container.



No comments: