更新於 2024/05/28閱讀時間約 12 分鐘

[Java][Spring Cloud]DevOps

這篇的DevOps發佈系統是以Spring Cloud微服務(微服務)為背景;由GitLab、Harbor與Kubernetes組成。


環境建置


建立專案

spring:
application:
name: xxx
profiles:
active: debug
cloud:
consul:
discovery:
preferIpAddress: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}:@project.version@
healthCheckPath: /actuator/health
server:
port: xxx​


  • 在pom.xml增加Maven設定
  • 建立Spring Cloud微服務main,如下範例:
package xxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class DevopsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(DevopsDemoApplication.class, args);
}
}


打包設定

  • 於pom.xml設定打包Docker image
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--指定Dockerfile檔案位置-->
<dockerfile>docker/Dockerfile</dockerfile>
<repository>${docker.repository}/springcloud-action/${app.name}</repository>
<!--<tag>${project.version}</tag>-->
<buildArgs>
<!--傳遞參數給Dockerfile-->
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>


  • 建立Dockerfile
FROM openjdk:8u191-jre-alpine3.9
ENTRYPOINT ["/usr/bin/java", "-jar", "/app.jar"]
ARG JAR_FILE
ADD ${JAR_FILE} /app.jar
EXPOSE 8080


  • 於pom.xml設定存放Docker image
<properties>
<docker.repository>xxx​</docker.repository>
<app.name>devops-demo</app.name>
</properties>


部署設定

  • 在GitLab CI/CD server安裝kubectl
snap install kubectl -classic


  • 將Kubernetes Master設定檔複製到GitLab CI/CD server
scp root@網址:$HOME/.kube/config​ $HOME/.kube​


  • 建立檔案deploy.yaml,用於發佈
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: __APP_NAME__
spec:
replicas: __REPLICAS__
selector:
matchLabels:
app: __APP_NAME__
strategy:
type: RollingUpdate
template:
metadata:
labels:
app: __APP_NAME__
spec:
imagePullSecrets:
- name: xxx
containers:
- name: __APP_NAME__
image: __IMAGE__
resources:
requests:
memory: "1000M"
limits:
memory: "1000M"
volumeMounts:
- name: time-zone
mountPath: /etc/localtime
- name: java-logs
mountPath: /opt/logs
ports:
- containerPort: __PORT__
env:
- name: SPRING_PROFILES_ACTIVE
value: __PROFILE__
- name: JAVA_OPTS
value: -Xms1G -Xmx1G -Dapp.home=/opt/
volumes:
- name: time-zone
hostPath:
path: /etc/localtime
- name: java-logs
hostPath:
path: /data/app/deployment/logs


  • 建立.gitlab-ci.yml,設定流程
#環境資訊
variables:
#Docker image倉庫地址和帳號密碼訊息
DOCKER_REPO_URL: "xxx"
DOCKER_REPO_USERNAME: admin
DOCKER_REPO_PASSWORD: admin
#Kubernetes配置
K8S_NAMESPACE: "xxx"
PORT: "xxx"

#定義CI/CD
stages:
- test
- build
- push
- deploy

#執行單元測試
maven-test:
stage: test
script:
- mvn clean test

#程式碼編譯打包image
maven-build:
stage: build
script:
- mvn clean package -DskipTests

#將打包好的Docker image上傳至私有image倉庫
docker-push:
stage: push
script:
#對打包的image進行tag
- docker tag $DOCKER_REPO_URL/$CI_PROJECT_PATH $DOCKER_REPO_URL/$CI_PROJECT_PATH/$CI_BUILD_REF_NAME:${CI_COMMIT_SHA:0:8}
#登入私有image倉庫
- docker login $DOCKER_REPO_URL -u $DOCKER_REPO_USERNAME -p $DOCKER_REPO_PASSWORD
#上傳應用程式image至image倉庫
- docker push $DOCKER_REPO_URL/$CI_PROJECT_PATH/$CI_BUILD_REF_NAME:${CI_COMMIT_SHA:0:8}
- docker rmi $DOCKER_REPO_URL/$CI_PROJECT_PATH/$CI_BUILD_REF_NAME:${CI_COMMIT_SHA:0:8}
- docker rmi $DOCKER_REPO_URL/$CI_PROJECT_PATH

#將應用程式發佈至Kubernetes測試叢集
deploy-test:
stage: deploy
when: manual
script:
- kubectl config use-context kubernetes-admin@kubernetes
- sed -e "s/__REPLICAS__/1/; s/__PORT__/$PORT/; s/__APP_NAME__/$CI_PROJECT_NAME/; s/__PROFILE__/test/; s/__IMAGE__/$DOCKER_REPO_URL\/${CI_PROJECT_PPf/ATH/ /\\/}\/${CI_BUILD_REF_NAME//\//\\/}:${CI_COMMIT_SHA:0:8}/" kubernetes/deploy.yaml | kubectl -n ${K8S_NAMESPACE} apply -f -


  • 部署Consul服務
docker run --name consul -p xxxx:xxxx -v /tmp/consul/conf/:/consul/conf/ -v /tmp/consul/data/:/consul/data/ -d consul


分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.