KubeSphere环境准备
新建企业空间
新建项目
创建harbor镜像服务信息
新建DevOps 项目
创建git,harbor,kubeconfig凭证
中间件部署
- MySQL8部署
- redis6部署
后端部署
- 修改pom.xml
<!-- pom.xml添加内容:-->
<profiles>
<!-- 本地环境 -->
<profile>
<id>local</id>
<properties>
<profile.active>local</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
<!-- 生产环境-->
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
</profiles>
<!-- buil标签添加内容 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
- 修改application.yaml
# spring.profiles.active 动态取值
spring:
profiles:
active: @profile.active@
- 修改Dockerfile
FROM hcd1129/jdk11 as builder
MAINTAINER cc cc@qq.com
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
COPY target/yudao-server.jar /app.jar
EXPOSE 8080
ENV JAVA_OPTS="-Xms512m -Xmx1024m -Xss1024k -Djava.security.egd=file:/dev/./urandom"
CMD java -jar /app.jar $JAVA_OPTS
- 修改Jenkinsfile
#!groovy
pipeline {
agent {
node {
label 'maven && jdk11'
}
}
environment {
// DockerHub 凭证 ID(登录您的 DockerHub)
DOCKER_CREDENTIAL_ID = 'ruoyi-harbor'
// GitHub 凭证 ID (推送 tag 到 GitHub 仓库)
GITHUB_CREDENTIAL_ID = 'ruoyi-git'
// kubeconfig 凭证 ID (访问接入正在运行的 Kubernetes 集群)
KUBECONFIG_CREDENTIAL_ID = 'ruoyi-kubeconfig'
// 镜像的推送
HARBOR_REGISTRY = '192.168.1.106:81'
// DockerHub 账号名
DOCKERHUB_NAMESPACE = 'ruoyi'
// GitHub 账号名
KUBE_NAMESPACE = 'source'
GIT_URL = 'http://192.168.1.50/ry/ruoyi-vue-pro.git'
}
stages {
stage('代码克隆') {
agent none
steps {
container('maven') {
git(url: "$GIT_URL", credentialsId: "$GITHUB_CREDENTIAL_ID", branch: "$env.BRANCH_NAME", changelog: true, poll: false)
sh 'ls -l'
}
}
}
stage('项目编译develop') {
agent none
when {
branch 'develop'
}
steps {
container('maven') {
sh 'mvn clean package -Dmaven.test.skip=true -Pdev'
}
}
}
stage('项目编译master') {
agent none
when {
branch 'master'
}
steps {
container('maven') {
sh 'mvn clean package -Dmaven.test.skip=true -Pprod'
}
}
}
stage('构建镜像') {
parallel {
stage('构建ruoyi-vue-pro-server镜像') {
agent none
steps {
container('maven') {
sh 'docker build -t ruoyi-vue-pro-server:latest -f yudao-server/Dockerfile yudao-server/'
}
}
}
}
}
stage('推送镜像到仓库') {
parallel {
stage('推送ruoyi-vue-pro-server镜像到harbor') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : "$DOCKER_CREDENTIAL_ID" ,passwordVariable : 'HARBOR_PWD_VAR' ,usernameVariable : 'HARBOR_USER_VAR' ,)]) {
sh 'echo "$HARBOR_PWD_VAR" | docker login -u $HARBOR_USER_VAR --password-stdin $HARBOR_REGISTRY'
sh 'docker tag ruoyi-vue-pro-server:latest $HARBOR_REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-vue-pro-server:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $HARBOR_REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-vue-pro-server:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
}
}
stage('推送K8S环境') {
parallel {
stage('发到ruoyi-vue-pro-server到K8S环境') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigContent(credentialsId : "$KUBECONFIG_CREDENTIAL_ID" ,variable : 'KUBECONFIG_CONFIG' ,)]) {
sh 'mkdir -p ~/.kube/'
sh 'echo "$KUBECONFIG_CONFIG" > ~/.kube/config'
sh 'envsubst < yudao-server/deploy/deploy.yml | kubectl apply -f -'
}
}
}
}
}
}
}
}
- 新建deploy/deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ruoyi-vue-pro-server
name: ruoyi-vue-pro-server
namespace: $KUBE_NAMESPACE #一定要写名称空间
spec:
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: ruoyi-vue-pro-server
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: ruoyi-vue-pro-server
spec:
volumes:
- name: host-time
hostPath:
path: /etc/localtime
type: ''
imagePullSecrets:
- name: harbor #提前在项目下配置访问阿里云的账号密码
containers:
- image: $HARBOR_REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-vue-pro-server:SNAPSHOT-$BUILD_NUMBER
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
timeoutSeconds: 1
periodSeconds: 30
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 1
periodSeconds: 60
successThreshold: 1
failureThreshold: 3
imagePullPolicy: Always
name: app
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 2000m
memory: 2048Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
labels:
app: ruoyi-vue-pro-server
name: ruoyi-vue-pro-server
namespace: $KUBE_NAMESPACE
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: ruoyi-vue-pro-server
sessionAffinity: None
type: ClusterIP
- 新建流水线
选择代码仓库
指定扫描分支过滤,Jenkinsfile路径
正则过滤可使用 分支A|分支B|分支C 格式来指定扫描分支
分支扫描完成的状态,至此后端部署完毕
前端部署
- 修改.env.dev,api调用地址
- 修改Jenkinsfile
pipeline {
agent {
kubernetes {
inheritFrom 'nodejs base'
containerTemplate {
name 'nodejs'
image 'node:14.19.0'
}
}
}
environment {
// DockerHub 凭证 ID(登录您的 DockerHub)
DOCKER_CREDENTIAL_ID = 'ruoyi-harbor'
// GitHub 凭证 ID (推送 tag 到 GitHub 仓库)
GITHUB_CREDENTIAL_ID = 'ruoyi-git'
// kubeconfig 凭证 ID (访问接入正在运行的 Kubernetes 集群)
KUBECONFIG_CREDENTIAL_ID = 'ruoyi-kubeconfig'
// 镜像的推送
HARBOR_REGISTRY = '192.168.1.106:81'
// DockerHub 账号名
DOCKERHUB_NAMESPACE = 'ruoyi'
// GitHub 账号名
KUBE_NAMESPACE = 'source'
GIT_URL = 'http://192.168.1.50/ry/ruoyi-admin-ui.git'
}
stages {
stage('拉取代码') {
steps {
container('nodejs') {
git(url: "$GIT_URL", credentialsId: "$GITHUB_CREDENTIAL_ID", branch: "$env.BRANCH_NAME", changelog: true, poll: false)
sh 'ls -al'
}
}
}
stage('项目编译') {
agent none
steps {
container('nodejs') {
sh 'npm install -g yarn --force'
sh 'yarn config get registry'
sh 'yarn config set registry https://registry.npmmirror.com/'
sh 'yarn config get registry'
sh 'yarn install'
}
}
}
stage('项目构建dev') {
agent none
when {
branch 'develop'
}
steps {
container('nodejs') {
sh 'yarn build:dev'
sh 'ls -a'
}
}
}
stage('项目构建master') {
agent none
when {
branch 'master'
}
steps {
container('nodejs') {
sh 'yarn build:prod'
sh 'ls -a'
}
}
}
stage('构建ruoyi-admin-ui镜像') {
agent none
steps {
container('base') {
sh 'ls -a'
sh 'docker build -t ruoyi-admin-ui:latest -f Dockerfile ./'
}
}
}
stage('推送ruoyi-admin-ui镜像到harbor') {
agent none
steps {
container('base') {
withCredentials([usernamePassword(credentialsId: "$DOCKER_CREDENTIAL_ID", passwordVariable: 'HARBOR_PWD_VAR', usernameVariable: 'HARBOR_USER_VAR',)]) {
sh 'echo "$HARBOR_PWD_VAR" | docker login -u $HARBOR_USER_VAR --password-stdin $HARBOR_REGISTRY'
sh 'docker tag ruoyi-admin-ui:latest $HARBOR_REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-admin-ui:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $HARBOR_REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-admin-ui:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('发到ruoyi-admin-ui到K8S环境') {
agent none
steps {
container('base') {
withCredentials([kubeconfigContent(credentialsId: "$KUBECONFIG_CREDENTIAL_ID", variable: 'KUBECONFIG_CONFIG',)]) {
sh 'mkdir -p ~/.kube/'
sh 'echo "$KUBECONFIG_CONFIG" > ~/.kube/config'
sh 'envsubst < deploy.yml | kubectl apply -f -'
}
}
}
}
}
}
- 新建deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ruoyi-admin-ui
name: ruoyi-admin-ui
namespace: $KUBE_NAMESPACE #一定要写名称空间
spec:
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: ruoyi-admin-ui
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: ruoyi-admin-ui
spec:
volumes:
- name: host-time
hostPath:
path: /etc/localtime
type: ''
imagePullSecrets:
- name: harbor #提前在项目下配置访问阿里云的账号密码
containers:
- image: $HARBOR_REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-admin-ui:SNAPSHOT-$BUILD_NUMBER
imagePullPolicy: Always
name: app
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: 2000m
memory: 1024Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
labels:
app: ruoyi-admin-ui
name: ruoyi-admin-ui
namespace: $KUBE_NAMESPACE
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: ruoyi-admin-ui
sessionAffinity: None
type: ClusterIP
- 修改Dockerfile
FROM nginx
COPY ./dist/ /usr/share/nginx/html/
EXPOSE 80
- 新建流水线
- 选择代码仓库
- 指定扫描分支过滤,Jenkinsfile路径
- 分支扫描完成的状态,至此后端部署完毕
gitLab配置自动打包
- 复制Webhook地址
- 添加到gitlab,不要勾选SSL证书验证