在新建工程中,使用k8s的devops服务,自动化部署项目
1、在搭建好k8s的集群中,确认已开启devops服务;
2、新建Maven项目之后,创建dockerfile、deploy和Jenkins文件
例如:
Dockerfile
FROM bairong.k8s.master1/br-sjzl/tomcat:9.0-jdk8-temurin-focal
ENV TZ=Asia/Shanghai
RUN mkdir /usr/local/tomcat/webapps/userfiles
COPY bairong-web/target/*.war /usr/local/tomcat/webapps/
Deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: czdataease-java
namespace: br-demo-project
annotations:
deployment.kubernetes.io/revision: '1'
kubesphere.io/creator: br-project-regular
spec:
replicas: 1
selector:
matchLabels:
app: czdataease-java
template:
metadata:
labels:
app: czdataease-java
spec:
imagePullSecrets: # 如过使用harbor仓库得镜像,需要设置从secret处拉取镜像 查看 kubectl get secret
- name: harbor-pull
containers:
- name: czdataease-java
image: bairong.k8s.master1/br-sjzl/czdataease-java:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
name: nginx
volumeMounts:
- name: czdataease-nfs-volume
mountPath: /usr/local/tomcat/webapps/userfiles
# command: ["sh", "-c"]
# args:
# - |
# cp -R /usr/local/tomcat/webapps/1 /usr/local/tomcat/webapps/userfiles/
volumes:
- name: czdataease-nfs-volume
persistentVolumeClaim:
claimName: br-czdataease-nfs-pvc
---
apiVersion: v1
kind: Service
metadata:
name: czdataease-java-service
namespace: br-demo-project
annotations:
deployment.kubernetes.io/revision: '1'
kubesphere.io/creator: br-project-regular
spec:
selector:
app: czdataease-java
ports:
- protocol: TCP
name: nginx
port: 8080
targetPort: 8080
nodePort: 30261
type: NodePort
Jenkins
pipeline {
agent {
node {
label 'nodejs'
}
}
stages {
stage('拉取') {
agent none
steps {
container('nodejs') {
git(url: 'http://192.168.19.199:12589/org_dataapplication/czDataease_vue.git', credentialsId: 'zyq', branch: 'master', changelog: true, poll: false)
}
}
}
stage('编译') {
agent none
steps {
container('nodejs') {
sh 'npm install --registry=http://192.168.19.197:31081/repository/npm-group/'
sh 'npm run build'
}
}
}
stage('打包') {
agent none
steps {
container('nodejs') {
sh 'docker build -t czdataease-vue:latest -f docker/Dockerfile .'
}
}
}
stage('推送') {
agent none
steps {
container('nodejs') {
withCredentials([usernamePassword(credentialsId : 'br-dockerhub-id' ,usernameVariable : 'DOCKER_USER_VAR' ,passwordVariable : 'DOCKER_PWD_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $BR_REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag czdataease-vue:latest $BR_REGISTRY/$BR_DOCKERHUB_NAMESPACE/czdataease-vue:latest'
sh 'docker push $BR_REGISTRY/$BR_DOCKERHUB_NAMESPACE/czdataease-vue:latest'
}
}
}
}
stage('镜像部署') {
agent none
steps {
container('nodejs') {
withCredentials([kubeconfigFile(credentialsId: env.KUBECONFIG_CREDENTIAL_ID, variable: 'KUBECONFIG')]) {
sh '''envsubst < deploy/deploy.yaml | kubectl apply -f -
kubectl rollout restart deploy/br-czdataease-vue -n br-demo-project'''
}
}
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub-id'
GITHUB_CREDENTIAL_ID = 'github-id'
KUBECONFIG_CREDENTIAL_ID = 'kubeconfig-demo'
REGISTRY = 'docker.io'
BR_REGISTRY = 'bairong.k8s.master1'
DOCKERHUB_NAMESPACE = 'bwylove'
BR_DOCKERHUB_NAMESPACE = 'br-sjzl'
GITHUB_ACCOUNT = 'bwylove'
APP_NAME = 'dataease-vue'
}
parameters {
string(name: 'TAG_NAME', defaultValue: '', description: '')
}
}