All Products
Search
Document Center

Apsara Devops:Build a Java application deliverable and publish it to ACK/K8S

Last Updated:Sep 15, 2023

Background

You can use this topic to assist your process automation under the following conditions:

  • Code development in Java

  • Delivery in the Docker Image format

  • Publish the related Docker Image on the ACK cluster or other K8s cluster

Customer requirements

Customers use K8S deployment in the following typical scenarios:

  • Perform some quality checks on the source code such as unit testing and code scanning.

  • Build source code into a deliverable such as a Docker Image.

  • Check the test environment.

  • Make online deployment using validated Docker Image.

  • All yaml files related to the applications are managed in the Infrastructure as Code. In this way, the yaml is completely handed over to the developer, which is more flexible and more conducive to the implementation of GitOps.

Apsara DevOps operations

The following example demonstrates how to build an application deliverable and useku bectl apply to publish it to ACK. A Java Spring Boot repository is used in the example.

Create a pipeline

1. Log on to Apsara DevOps Flow (https://flow.alibabacloud.com/).

2. Click Create Pipeline.

1

3. Select Java ยท Test, build Image, and deploy to Kubernetes cluster/ACK template and click Create.

2

Configure a code repository

1. Add a code source. Select General Git. Set Code Repository to https://github.com/yunxiao-devops/spring-boot-example.git and Default Branch to master. You can enter any personal certificate, because a public code repository is used in the example.

2. The address of the sample code library is: https://github.com/yunxiao-devops/spring-boot-example.git. Please check the relevant manifest file first, which contains a deployment, a service, and an ingress to expose this service to the public network:

3

Configure a build task

1. Create a repository on ACR(cr.console.aliyun.com) and select ACR Build step on the pipeline editing page.

46

2. The ${DATETIME} indicates that the timestamp when the pipeline is triggered. Enter "Dockerfile2" in the Dockerfile path.

7

3. After configuration, you can see the output variables of the task under the image build and push to Alibaba cloud ACR task. These variables can be referenced in subsequent tasks. Just focus on the second and third variables, where the second is the public network address of the image address and the third is the VPC address of the image address. It is recommended to put the image repository and ACK cluster in the same region, and then select the VPC address in subsequent deployment tasks to speed up the deployment process.

8

Configure a deployment task

1. Click Kubernetes Deployment task. Click Create Service Connection and enter the cluster name and related Kubeconfig information. After confirmation, the cluster information will be filled out automatically.

910

2. Configure the related variables. Enter the image address as the IMAGE. Then Flow will replace all the ${IMAGE} value with the image address in the YAML.

a

3. As shown in the example repository https://github.com/yunxiao-devops/spring-boot-example/blob/master/app-configs/manifest-app/app.yaml, all the ${IMAGE} will be replaced with the actual image address.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: spring-boot-sample
  name: spring-boot-sample
spec:
  replicas: 2
  selector:
    matchLabels:
      run: spring-boot-sample
  template:
    metadata:
      labels:
        run: spring-boot-sample
    spec:
      containers:
      - image: ${IMAGE}
        name: app

4. Enter the Testing Domain of ACK cluster as the HOST value.

dczcx

5. As shown in the example repository https://github.com/yunxiao-devops/spring-boot-example/blob/master/app-configs/manifest-app/ingress.yaml, the ${HOST} will be replaced with the actual testing domain.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: spring-boot-route
spec:
  rules:
    - host: www.${HOST}
      http:
        paths:
          - backend:
              serviceName: spring-boot-service
              servicePort: 8080
            path: /

Run a pipeline

1. After the configurations are complete, click Save and Run to trigger the pipeline.

c

2. Because the manifest includes ingress configuration, you can visit the domain.

$ curl http://www.${HOST}
Greetings from Spring Boot!