使用kubesphere的devops部署SpringCloud项目

发布时间:2024年01月09日

环境说明

部署流程

创建DevOps工程

在这里插入图片描述
在这里插入图片描述

填写流水线信息

在这里插入图片描述
在这里插入图片描述

创建流水线


1 我们编写JenkinsFile
在这里插入图片描述

流水线的部署流程如下图

拉取代码
编译代码
打包镜像
推送镜像
部署应用

jenkinsfile

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

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

打包镜像文件

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: 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 #提前在项目下配置访问阿里云的账号密码
在这里插入图片描述
在这里插入图片描述

文章来源:https://blog.csdn.net/qq_39017153/article/details/135471604
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。