Friday, February 14, 2025

AKS docker Persistent Volume to decouple .net code deploy without rebuilding image and container

 Here’s how you can implement some of these methods to decouple your .NET web app’s source code deployment from the AKS container image:


1. Use Persistent Volume (PV) and Persistent Volume Claim (PVC)

This method mounts an external Azure Storage account into the AKS pod, allowing your app to read the latest source code without rebuilding the image.

Steps to Implement:

  1. Create an Azure File Share:

    az storage account create --name mystorageaccount --resource-group myResourceGroup --location eastus --sku Standard_LRS
    az storage share create --name myfileshare --account-name mystorageaccount
    
  2. Create a Kubernetes Secret for Storage Credentials:

    kubectl create secret generic azure-secret \
      --from-literal=azurestorageaccountname=mystorageaccount \
      --from-literal=azurestorageaccountkey=$(az storage account keys list --resource-group myResourceGroup --account-name mystorageaccount --query '[0].value' --output tsv)
    
  3. Define a Persistent Volume (PV) and Persistent Volume Claim (PVC):

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: azurefile-pv
    spec:
      capacity:
        storage: 5Gi
      accessModes:
        - ReadWriteMany
      azureFile:
        secretName: azure-secret
        shareName: myfileshare
        readOnly: false
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: azurefile-pvc
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 5Gi
    
  4. Mount the Storage in Your Deployment:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-dotnet-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: my-dotnet-app
      template:
        metadata:
          labels:
            app: my-dotnet-app
        spec:
          containers:
          - name: my-dotnet-app
            image: myacr.azurecr.io/mydotnetapp:latest
            volumeMounts:
            - name: azurefile
              mountPath: /app
          volumes:
          - name: azurefile
            persistentVolumeClaim:
              claimName: azurefile-pvc
    
  5. Deploy the Updated App to AKS:

    kubectl apply -f deployment.yaml
    

Now, your .NET app will dynamically read source code from the Azure File Share without rebuilding the container.


2. Use Sidecar Pattern with a Shared Volume

This method runs a second container inside the same pod to fetch and update the source code.

Steps to Implement:

  1. Modify the Deployment YAML to Add a Sidecar:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-dotnet-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: my-dotnet-app
      template:
        metadata:
          labels:
            app: my-dotnet-app
        spec:
          volumes:
          - name: shared-volume
            emptyDir: {}
          containers:
          - name: my-dotnet-app
            image: myacr.azurecr.io/mydotnetapp:latest
            volumeMounts:
            - name: shared-volume
              mountPath: /app
          - name: sidecar-git-sync
            image: alpine/git
            command: ["sh", "-c", "while true; do git pull origin main; sleep 60; done"]
            volumeMounts:
            - name: shared-volume
              mountPath: /app
    
  2. Ensure the Sidecar Has Access to the Repo:

    • Store SSH keys or tokens in Kubernetes secrets.
    • Modify the Git sync command to fetch from your repository.
  3. Deploy to AKS:

    kubectl apply -f deployment.yaml
    

Now, the sidecar container will fetch code updates every 60 seconds, and your app container will read from the shared volume.


3. Use .NET Hot Reload with Volume Mounting

This method allows live updates to your .NET web app inside an AKS pod.

Steps to Implement:

  1. Modify Your .NET Dockerfile to Enable Hot Reload:

    FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
    WORKDIR /app
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
    WORKDIR /src
    COPY . .
    RUN dotnet restore
    RUN dotnet publish -c Release -o /app
    
    FROM base AS final
    WORKDIR /app
    COPY --from=build /app .
    CMD ["dotnet", "watch", "run", "--urls", "http://+:80"]
    
  2. Mount the Source Code Volume in Deployment YAML:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-dotnet-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: my-dotnet-app
      template:
        metadata:
          labels:
            app: my-dotnet-app
        spec:
          containers:
          - name: my-dotnet-app
            image: myacr.azurecr.io/mydotnetapp:latest
            volumeMounts:
            - name: source-code
              mountPath: /app
          volumes:
          - name: source-code
            persistentVolumeClaim:
              claimName: azurefile-pvc
    
  3. Deploy to AKS and Start Hot Reload:

    kubectl apply -f deployment.yaml
    

Now, when you update your source code, the changes will reflect in your .NET app without restarting the container.


4. Use Azure DevOps Pipelines to Deploy Just Source Code

Instead of rebuilding the entire container, update only the source code.

Steps to Implement:

  1. Set Up an Azure DevOps Pipeline:

    • Use Azure DevOps Pipelines or GitHub Actions to deploy only source code updates.
    • Configure a build step to sync source code to a persistent volume.
    • Restart only the application process, not the container.
  2. Use Helm to Update the Deployment Without Rebuilding the Image:

    helm upgrade myapp ./helm-chart --set app.sourceVersion=$(git rev-parse HEAD)
    

This will ensure the latest source code is available without triggering a new Docker image build.


Conclusion

The best approach depends on your use case: ✅ For Persistent Source Code Storage: Use Azure File Share with PV/PVC.
For Continuous Sync from Git: Use a sidecar pattern.
For Live Updates During Development: Use .NET Hot Reload.
For Automated Updates in Production: Use Azure DevOps with Helm.

Hope you enjoy please share and comment.🚀

No comments :