Data Management Global

Data Management Global

A hub for collaboration, learning, networking, and cultural exchange, and contributing to positive global engagement

 View Only

Deploying DB2 .NET 7 application on Azure Kubernetes Service

By Archana Soni posted Wed June 21, 2023 04:37 AM

  

This blog will help you in deploying a DB2 .NET 7 application on Azure Kubernetes environment. To demonstrate, we will create MVC application, detailed steps are mentioned here.sddd

Section 1: Create MVC App 

  1. Create MVC application targeting .NET7 and install IBM.EntityFrameworkCore-lnx package for Linux.


  2. Add DbContext and Entity classes.


  3. Add Controller to the project.




  4. Add context as service in Program.cs file:
    builder.Services.AddDbContext<MyContext>();


  5. Compile the project to make sure it’s compiling.

Section 2: Build Docker image

  1. Add Docker support by right clicking on the project. In this example we will use Linux docker container.



  2. Add following instruction to your Dockerfile to install dependency and set LD_LIBRARY_PATH needed by DB2 .NET Provider.
    RUN set -e; \   
        apt-get update; \
        apt-get install -y libxml2-dev;
    ENV LD_LIBRARY_PATH="/app/clidriver/lib/"
    
    

    For reference here is Dockerfile:

    #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
    
    FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
    WORKDIR /src
    COPY ["MyK8sApp/MyK8sApp.csproj", "MyK8sApp/"]
    RUN dotnet restore "MyK8sApp/MyK8sApp.csproj"
    COPY . .
    WORKDIR "/src/MyK8sApp"
    RUN dotnet build "MyK8sApp.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "MyK8sApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
    
    FROM base AS final
    WORKDIR /app
    RUN set -e; \   
        apt-get update; \
        apt-get install -y libxml2-dev;
    COPY --from=publish /app/publish .
    ENV LD_LIBRARY_PATH="/app/clidriver/lib/"
    ENTRYPOINT ["dotnet", "MyK8sApp.dll"]
    


    Note: From .NET 8 on-wards User may require to provide Admin privileges in docker file to install libxml2-dev and capturing the traces.

  3. Open command prompt, navigate to your project directory and build the docker image using following command:
    docker build -t myk8sapp:v1 -f MyK8sApp\Dockerfile .

     

  4. Run the docker image using following command:
    docker run myk8sapp:v1 -ti -p 8080:80

Section 3: Push Docker image to Azure Container Registry (ACR) 

  1. Login to Azure portal and create the Azure Container Registry (ACR)


    Note: User may require to enable admin privilege of ACR.
  2. Login to ACR and tag docker image using below commands:
    az acr login --name myk8sappcontainerregistry
    docker tag myk8sapp:v1 myk8sappcontainerregistry.azurecr.io/myk8sapp:v1
  3. Push the docker image to azure container registry: 
    docker push myk8sappcontainerregistry.azurecr.io/myk8sapp:v1

Section 4: Deploy the app on Azure Kubernetes Service (AKS)

  1. Go back to Azure portal and create Kubernetes cluster.



    While creating give Container registry name under Integration tab:


     

  2. Now right click on the project in Solution explorer and add 2 yaml files named as deploy.yml and service.yml
    deploy.yml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myk8sapp-deployment
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: myk8sapp
      template:
        metadata:
          labels:
            app: myk8sapp
        spec:
          containers:
            - name: myk8sappcontainer
              image: myk8sappcontainerregistry.azurecr.io/myk8sapp:v1
              ports:
                - containerPort: 80          


    service.yml

    apiVersion: v1
    kind: Service
    metadata:
      name: myk8sapp-service
    spec:
      type: LoadBalancer
      selector:
        app: myk8sapp
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
    

  3. Go to Azure cluster we created and click on connect. Copy the following 2 commands and run from the command prompt.
    az account set --subscription <subscription number>
    az aks get-credentials --resource-group kubedemo --name myk8scluster



  4. Apply deploy.yml and service.yml file using kubectl command:
    kubectl apply -f MyK8sApp\deploy.yml,MyK8sApp\service.yml


  5. Run “kubectl get pod” and “kubectl describe pod <pod name>” commands to know the pod details


  6. Run “kubectl get svc” to know the external ip of the service. If it shows in pending state, please wait for some time as it may take about a minute.



  7. Open https://20.118.91.30/KUBETAB1 in browser to see the output.




0 comments
35 views

Permalink