- 已经安装kubesphere的devops组件
- 安装教程可参考官方文档:https://v3-1.docs.kubesphere.io/zh/docs/pluggable-components/devops/
1 我们编写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: '')
}
}
打包镜像文件
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}"]
应用部署文件
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 #提前在项目下配置访问阿里云的账号密码