使用kubesphere的devops部署SpringCloud项目

devops部署SpringCloud项目

环境说明

部署流程

创建DevOps工程


填写流水线信息


创建流水线


1 我们编写JenkinsFile

流水线的部署流程如下图
拉取代码 编译代码 打包镜像 推送镜像 部署应用

jenkinsfile

因为所有服务都写出来实在太长也不方便观看,故以gateway,admin 两个模块为例,增加其他模块也是亦是相同,复制粘贴即可

sh 复制代码
pipeline {
    agent {
        node {
            label 'jdk11'
        }

    }
    stages {
        stage('拉取代码') {
            agent none
            steps {
                container('maven') {
                    git(url: '你的代码地址', credentialsId: 'git-code-auth', branch: '代码分支', changelog: true, poll: false)
                    sh 'ls'
                }

            }
        }

        stage('编译代码') {
            agent none
            steps {
                container('maven') {
                    sh '''ls
pwd
java -version'''
                    sh 'mvn clean install -Dmaven.test.skip=true'
                }

            }
        }

        stage('default-2') {
            parallel {
                stage('构建镜像gateway') {
                    agent none
                    steps {
                        container('maven') {
                            sh 'ls $GATEWAY/target'
                            sh 'docker build -t $GATEWAY:latest -f $GATEWAY/Dockerfile ./$GATEWAY/'
                        }

                    }
                }

                stage('构建镜像tingyuan-cloud-api-admin') {
                    agent none
                    steps {
                        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 none
                    steps {
                        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 none
                    steps {
                        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 none
                    steps {
                        kubernetesDeploy(configs: '文件所在路径/deploy/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
                    }
                }

                stage('deploy admin to pro') {
                    agent none
                    steps {
                        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

打包镜像文件

sh 复制代码
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

应用部署文件

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: 容器名
  name: 容器名
  namespace: 名称空间   #一定要写名称空间
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: 容器名
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: 容器名
    spec:
      imagePullSecrets:
        - name: aliyun-docker  #提前在项目下配置访问阿里云的账号密码 
      containers:
        - image: 推送到镜像仓库的地址
          readinessProbe:
            httpGet:
              path: /actuator/health
              port: 8080
            timeoutSeconds: 10
            failureThreshold: 30
            periodSeconds: 5
          imagePullPolicy: Always
          name: app
          ports:
            - name: http-8080
              containerPort: 8080
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: 容器名
  name: 容器名
  namespace: 名称空间
spec:
  ports:
    - name: http
      port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    app: 容器名
  sessionAffinity: None
  type: ClusterIP

如何设置

imagePullSecrets:

  • name: aliyun-docker #提前在项目下配置访问阿里云的账号密码

相关推荐
一元咖啡3 小时前
SpringCloud Gateway转发请求到同一个服务的不同端口
spring·spring cloud·gateway
天天扭码3 小时前
五天SpringCloud计划——DAY2之单体架构和微服务架构的选择和转换原则
java·spring cloud·微服务·架构
跳跳的向阳花4 小时前
03-03、SpringCloud第三章,负载均衡Ribbon和Feign
spring cloud·ribbon·负载均衡
天天扭码19 小时前
五天SpringCloud计划——DAY1之mybatis-plus的使用
java·spring cloud·mybatis
luckywuxn1 天前
Spring Cloud Alibaba、Spring Cloud 与 Spring Boot各版本的对应关系
spring boot·spring·spring cloud
wclass-zhengge2 天前
SpringCloud篇(服务网关 - GateWay)
spring boot·spring cloud·gateway
荆州克莱2 天前
Redis | Redis常用命令及示例总结(API)
spring boot·spring·spring cloud·css3·技术
RainbowSea2 天前
5. Spring Cloud OpenFeign 声明式 WebService 客户端的超详细使用
java·spring·spring cloud
听潮阁2 天前
【SpringCloud详细教程】-02-微服务环境搭建
spring·spring cloud·微服务