kubesphere devops部署springboot项目
一:使用流水线devops部署springboot项目的流程:
二、本次项目结构:
其中Dockerfile:
FROM openjdk:8-jdk LABEL maintainer=leifengyang #docker run -e PARAMS="--server.port 9090" ENV PARAMS="--server.port=8080" 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: demo-mybatis name: demo-mybatis namespace: terp #一定要写名称空间 spec: progressDeadlineSeconds: 600 replicas: 1 selector: matchLabels: app: demo-mybatis strategy: rollingUpdate: maxSurge: 50% maxUnavailable: 50% type: RollingUpdate template: metadata: labels: app: demo-mybatis spec: imagePullSecrets: - name: aliyuncs-docker-hub #提前在项目下配置访问阿里云的账号密码 containers: - image: $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER # readinessProbe: # httpGet: # path: /actuator/health # port: 8080 # timeoutSeconds: 10 # failureThreshold: 30 # periodSeconds: 5 imagePullPolicy: Always name: app ports: - containerPort: 8080 protocol: TCP resources: limits: cpu: 300m memory: 600Mi terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always terminationGracePeriodSeconds: 30 --- apiVersion: v1 kind: Service metadata: labels: app: demo-mybatis name: demo-mybatis namespace: terp spec: ports: - name: http port: 8080 protocol: TCP targetPort: 8080 nodePort: 32607 selector: app: demo-mybatis sessionAffinity: None type: NodePort
上面的部署文件中,我们的镜像需要从阿里云镜像仓库拉取,这里拉取的密钥是imagePullSecrets是aliyuncs-docker-hub,因此需要事先在kubeSphere中配置好这个镜像仓库:
三、在kubeSphere中创建devops流水线
1.创建一个devops工程:名叫shop
2.进入工程中,创建流水线:
其他都默认
3.进入流水线中,选择编辑流水线
选择其中一个模板:
如果进不去流水线这一步,并报错了,可以参考: 如果后续安装devops报错,参考:https://blog.51cto.com/wutengfei/3818273
4.编辑流水线拉取代码这一步:因为这一步需要用到git拉取代码,因此需要先创建git账户密码
回到流水线编辑处:
在嵌套步骤中选择git
继续添加创建步骤:执行shell命令 ls -al 列出拉取回来的代码结构
最终效果如上图:
保存所有步骤后,先运行看看效果:
5. 代码编译:mvn clean package -Dmaven.test.skip=true
6. 打包成镜像 docker build -t demo_mybatis . (后面有一个点)这样会将jar包打成demo_mybatis:latest镜像
7.将镜像推送到阿里云镜像仓库:这里是最复杂的:
先创建一个阿里云镜像仓库账户:
之后直接将Jenkins文件拷贝出来,修改JenkinsFile
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('clone code') {
agent none
steps {
container('maven') {
git(credentialsId: 'gitee', branch: 'master', url: 'https://gitee.com/yangxioahui/demo_mybatis.git', changelog: true, poll: false)
sh 'ls -al'
}
}
}
stage('代码编译') {
agent none
steps {
container('maven') {
sh 'mvn clean package -Dmaven.test.skip=true'
}
}
}
stage('打包成镜像') {
steps {
container('maven') {
sh 'docker build -t demo_mybatis .'
}
}
}
stage('push images') {
agent {
node {
label 'maven'
}
}
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-docker-hub' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker tag demo_mybatis:latest $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('deploy to production') {
steps {
input(id: 'deploy-to-production', message: 'deploy to production?')
kubernetesDeploy(configs: 'deploy.yml', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'aliyun-docker-hub' #这里是前面配置的阿里云账户
REGISTRY = 'registry.cn-hangzhou.aliyuncs.com' #阿里云镜像仓库地址
DOCKERHUB_NAMESPACE = 'terp' #命名空间
GITHUB_ACCOUNT = 'kubesphere'
APP_NAME = 'demo-mybatis' #自己的项目名称
}
parameters {
string(name: 'TAG_NAME', defaultValue: '', description: '')
}
}
将jenkinsFile保存后,继续回到流水线进行编辑;
这里用到了2个变量:DOCKER_PASSWORD和 DOCKER_USERNAME 选择嵌套步骤,选择凭证
最后一步,因为要在其他节点使用kubectl apply -f deploy.yml命令:需要做些配置:
接着回到流水线,编辑最后一步
最后完整的JenkinsFile如下:
pipeline { agent { node { label 'maven' } } stages { stage('clone code') { agent none steps { container('maven') { git(credentialsId: 'gitee', branch: 'master', url: 'https://gitee.com/yangxioahui/demo_mybatis.git', changelog: true, poll: false) sh 'ls -al' } } } stage('代码编译') { agent none steps { container('maven') { sh 'mvn clean package -Dmaven.test.skip=true' } } } stage('打包成镜像') { steps { container('maven') { sh 'docker build -t demo_mybatis .' } } } stage('push images') { agent { node { label 'maven' } } steps { container('maven') { withCredentials([usernamePassword(credentialsId : 'aliyun-docker-hub' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) { sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin' sh 'docker tag demo_mybatis:latest $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER' sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BUILD_NUMBER' } } } } stage('deploy to production') { agent none steps { input(id: 'deploy-to-production', message: 'deploy to production?') kubernetesDeploy(configs: 'deploy.yml', enableConfigSubstitution: true, kubeconfigId: 'kubectl-config') } } } environment { DOCKER_CREDENTIAL_ID = 'aliyun-docker-hub' REGISTRY = 'registry.cn-hangzhou.aliyuncs.com' DOCKERHUB_NAMESPACE = 'terp' GITHUB_ACCOUNT = 'kubesphere' APP_NAME = 'demo-mybatis' } parameters { string(name: 'TAG_NAME', defaultValue: '', description: '') } }
流水线执行称成功后,可以看到对应的服务