devops部署SpringCloud项目
- 环境说明
- 部署流程
- 创建DevOps工程
- 填写流水线信息
- 创建流水线
- jenkinsfile
- Dockerfile
- deploy.yaml
环境说明
- 已经安装kubesphere的devops组件
- 安装教程可参考官方文档:https://v3-1.docs.kubesphere.io/zh/docs/pluggable-components/devops/
部署流程
创建DevOps工程
填写流水线信息
创建流水线
1 我们编写JenkinsFile
流水线的部署流程如下图
jenkinsfile
因为所有服务都写出来实在太长也不方便观看,故以
gateway,admin
两个模块为例,增加其他模块也是亦是相同,复制粘贴即可
pipeline {agent {node {label 'jdk11'}}stages {stage('拉取代码') {agent nonesteps {container('maven') {git(url: '你的代码地址', credentialsId: 'git-code-auth', branch: '代码分支', changelog: true, poll: false)sh 'ls'}}}stage('编译代码') {agent nonesteps {container('maven') {sh '''ls
pwd
java -version'''sh 'mvn clean install -Dmaven.test.skip=true'}}}stage('default-2') {parallel {stage('构建镜像gateway') {agent nonesteps {container('maven') {sh 'ls $GATEWAY/target'sh 'docker build -t $GATEWAY:latest -f $GATEWAY/Dockerfile ./$GATEWAY/'}}}stage('构建镜像tingyuan-cloud-api-admin') {agent nonesteps {container('maven') {sh 'ls $BASE_PATH/$ADMIN/target'sh 'docker build -t $ADMIN:latest -f $BASE_PATH/$ADMIN/Dockerfile ./$BASE_PATH/$ADMIN/'}}}}}stage('default-3') {parallel {stage('推送镜像gateway') {agent nonesteps {container('maven') {withCredentials([usernamePassword(credentialsId: 'aliyun-docker', passwordVariable: 'DOCKER_PASSWORD_VAR', usernameVariable: 'DOCKER_USER_VAR',)]) {sh 'echo "$DOCKER_PASSWORD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'sh 'docker tag $GATEWAY:latest $REGISTRY/$DOCKERHUB_NAMESPACE/$GATEWAY:SNAPSHOT-$BUILD_NUMBER'sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$GATEWAY:SNAPSHOT-$BUILD_NUMBER'}}}}stage('推送镜像admin') {agent nonesteps {container('maven') {withCredentials([usernamePassword(credentialsId: 'aliyun-docker', passwordVariable: 'DOCKER_PASSWORD_VAR', usernameVariable: 'DOCKER_USER_VAR',)]) {sh 'echo "$DOCKER_PASSWORD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'sh 'docker tag $ADMIN:latest $REGISTRY/$DOCKERHUB_NAMESPACE/$ADMIN:SNAPSHOT-$BUILD_NUMBER'sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$ADMIN:SNAPSHOT-$BUILD_NUMBER'}}}}}}stage('default-4') {parallel {stage('deploy gateway to pro') {agent nonesteps {kubernetesDeploy(configs: '文件所在路径/deploy/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")}}stage('deploy admin to pro') {agent nonesteps {kubernetesDeploy(configs: '文件所在路径/deploy/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")}}}}}environment {DOCKER_CREDENTIAL_ID = 'dockerhub-id'GITHUB_CREDENTIAL_ID = 'github-id'KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'REGISTRY = '镜像仓库地址'DOCKERHUB_NAMESPACE = '镜像创库nameSpace'GITHUB_ACCOUNT = 'kubesphere'BASE_PATH = 'api'GATEWAY = 'gateway'APP = 'app'ADMIN = 'admin'GUILD = 'guild'LANDPAGE = 'landpage'SCHEDULE = 'schedule'WS = 'ws'DB = 'db'SERVICE = 'service'FILE = 'file'}parameters {string(name: 'TAG_NAME', defaultValue: '', description: '')}
}
Dockerfile
打包镜像文件
FROM openjdk:11
LABEL maintainer=hrd
ENV PARAMS="--server.port=8080 --spring.profiles.active=k8sProd"
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY target/*.jar /app.jar
EXPOSE 8080
ENTRYPOINT ["/bin/sh","-c","java -Dfile.encoding=utf8 -Djava.security.egd=file:/dev/./urandom -jar app.jar ${PARAMS}"]
deploy.yaml
应用部署文件
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: 容器名name: 容器名namespace: 名称空间 #一定要写名称空间
spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: 容器名strategy:rollingUpdate:maxSurge: 50%maxUnavailable: 50%type: RollingUpdatetemplate:metadata:labels:app: 容器名spec:imagePullSecrets:- name: aliyun-docker #提前在项目下配置访问阿里云的账号密码 containers:- image: 推送到镜像仓库的地址readinessProbe:httpGet:path: /actuator/healthport: 8080timeoutSeconds: 10failureThreshold: 30periodSeconds: 5imagePullPolicy: Alwaysname: appports:- name: http-8080containerPort: 8080protocol: TCPresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: AlwaysterminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:labels:app: 容器名name: 容器名namespace: 名称空间
spec:ports:- name: httpport: 8080protocol: TCPtargetPort: 8080selector:app: 容器名sessionAffinity: Nonetype: ClusterIP
如何设置
imagePullSecrets:
- name: aliyun-docker #提前在项目下配置访问阿里云的账号密码