Java project and setting up a CI CD pipeline in Azure DevOps with Docker and Kubernetes (AKS) - kondareddypp/azure-network-fundamentals GitHub Wiki

Creating a simple Java project and setting up a CI/CD pipeline in Azure DevOps with Docker and Kubernetes (AKS) involves several steps. Here's an overview:


1. Java Project Setup

  1. Create a Simple Java Application:

    • Use any IDE (like IntelliJ IDEA or Eclipse) to create a Maven-based Java application.
    • Example: A simple HelloWorld application.
  2. Add a Dockerfile:

    • Place a Dockerfile in the root directory of your project:
      FROM openjdk:11-jre-slim
      WORKDIR /app
      COPY target/*.jar app.jar
      ENTRYPOINT ["java", "-jar", "app.jar"]
      
    • Build the project with mvn clean package to generate the .jar file.
  3. Push to Azure Repos or GitHub:

    • Initialize Git, commit your code, and push it to a repository.

2. Create an Azure Kubernetes Service (AKS) Cluster

  1. Set Up AKS:

    • Navigate to the Azure portal.
    • Create a Kubernetes cluster by selecting Azure Kubernetes Service (AKS).
    • Configure node count, networking, and resource group.
  2. Install Azure CLI & kubectl:

    • Install the Azure CLI and enable kubectl for cluster management.
    • Connect your AKS cluster:
      az aks get-credentials --resource-group <your-resource-group> --name <your-cluster-name>
      
  3. Prepare Kubernetes YAML Files:

    • Deployment YAML:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: java-app
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: java-app
        template:
          metadata:
            labels:
              app: java-app
          spec:
            containers:
            - name: java-container
              image: <your-docker-image>
              ports:
              - containerPort: 8080
      
    • Service YAML:
      apiVersion: v1
      kind: Service
      metadata:
        name: java-service
      spec:
        selector:
          app: java-app
        ports:
        - protocol: TCP
          port: 80
          targetPort: 8080
        type: LoadBalancer
      

3. Set Up Azure Pipelines (YAML)

Create an azure-pipelines.yml file in your repository root:

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  imageName: '<your-docker-image>'

steps:
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'package'

- task: Docker@2
  inputs:
    containerRegistry: '<your-azure-container-registry-service-connection>'
    repository: '$(imageName)'
    command: 'buildAndPush'
    Dockerfile: '**/Dockerfile'
    tags: |
      $(Build.BuildId)

- script: |
    kubectl apply -f deployment.yaml
    kubectl apply -f service.yaml
  displayName: 'Deploy to AKS'

4. Configure Azure DevOps

  1. Create a New Pipeline:

    • Navigate to Pipelines in Azure DevOps.
    • Select your repository and point to the azure-pipelines.yml file.
  2. Set Up Service Connections:

    • Create service connections for Azure Container Registry and AKS.
  3. Run the Pipeline:

    • Queue the pipeline to build, push the Docker image, and deploy to AKS.

This setup will build your Java project, containerize it using Docker, push it to Azure Container Registry, and deploy it to AKS via your Azure pipeline.