改DevOps主要就是 监控git 在有版本更新的时候可以自动拉取代码打包发布docker images,更新k8s的deployment
1. 自制应用参考官方文档配置( 自制应用可以用的KubeSphere的灰度发布功能)
Jenkins的pipeline如下 使用kubernetesDeploy的时候 kubeconfig在KubeSphere3.0中有问题,解决方案看帖子:https://kubesphere.com.cn/forum/d/2591-spring-boot-received-fatal-alert-bad-certificate/8
关键点如下:
kubectl -n kubesphere-controls-system delete cm kubeconfig-[用户名]
kubectl -n kubesphere-system rollout restart deployment ks-controller-manager ks-apiserver
def riqi=''
import java.text.SimpleDateFormat
pipeline {
agent {
node {
label 'maven'
}
}
environment {
DATETAG=""
}
stages {
stage('定义日期') {
steps {
script {
def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
def date = new Date()
riqi=dateFormat.format(date)
env.riqistr = riqi
}
}
}
stage('获取代码') {
steps {
git(credentialsId: 'gitee-ssh', url: 'git@gitee.com:xxxx', branch: 'dev', changelog: true, poll: false)
}
}
stage('Maven 构建') {
steps {
container('maven') {
sh 'mvn -f xxxx/pom.xml -Dmaven.test.skip=true -U clean install'
}
}
}
stage('打包Docker') {
steps {
container('maven') {
echo 'start build image'
dir('xxxxx') {
sh 'docker build --build-arg DEPLOY_ENV_SELECT=dev -t xxx-back-dev:$riqistr .'
sh 'docker login -u admin -p xxx xxx.io'
sh 'docker tag xxxx:1.0 fqidocker.io/jeecg-boot-back-dev:$riqistr'
sh 'docker push fqidocker.io/jeecg-boot-back-dev:$riqistr'
}
}
}
}
stage('部署到K8S') {
steps {
container('maven') {
sh 'ls'
kubernetesDeploy(enableConfigSubstitution: true, deleteResource: false, kubeconfigId: 'kubeconfig', configs: 'xxxx/k8s_deployment.yaml')
}
}
}
}
}
K8S的deployment在 KubeSphere工作负载中Copy出来,这里有几个改动需要注意,idea下载k8s的插件,检查下yaml中是否有灰色的标签,如果有需要删除否则会报错
ERROR: ERROR: Cannot create property=spec for JavaBean=class V1Deployment {
kind: Deployment
apiVersion: apps/v1
metadata:
name: xxxxback-dev-v1
namespace: demo-project
labels:
build_number: ${BUILD_NUMBER} #这里是让k8s观察到deployment发生变化
app: xxxxback-dev
app.kubernetes.io/name: xxxx-improve
app.kubernetes.io/version: v1
version: v1
annotations:
deployment.kubernetes.io/revision: '1'
kubesphere.io/creator: project-admin
servicemesh.kubesphere.io/enabled: 'true'
spec:
replicas: 2
selector:
matchLabels:
app: jeecg-boot-back-dev
app.kubernetes.io/name: xxx-improve
app.kubernetes.io/version: v1
version: v1
template:
metadata:
labels:
app: jeecg-boot-back-dev
app.kubernetes.io/name: xxx-improve
app.kubernetes.io/version: v1
version: v1
annotations:
kubesphere.io/containerSecrets: '{"container-xxx-back":"docker-sign"}'
sidecar.istio.io/inject: 'true'
spec:
volumes:
- name: host-time
hostPath:
path: /etc/localtime
type: ''
containers:
- name: container-xxxx-back
image: 'fqidocker.io/jeecg-boot-back-dev:${riqistr}'
ports:
- name: http-api
containerPort: 8080
protocol: TCP
resources: {}
volumeMounts:
- name: host-time
readOnly: true
mountPath: /etc/localtime
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
serviceAccountName: default
securityContext: {}
imagePullSecrets:
- name: docker-sign
affinity: {}
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 10
progressDeadlineSeconds: 600