在新建工程中,使用k8s的devops服务,自动化部署项目
1、在搭建好k8s的集群中,确认已开启devops服务;
2、新建Maven项目之后,创建dockerfile、deploy和Jenkins文件
例如:
Dockerfile
FROM bairong.k8s.master1/br-sjzl/tomcat:9.0-jdk8-temurin-focalENV TZ=Asia/ShanghaiRUN mkdir /usr/local/tomcat/webapps/userfilesCOPY bairong-web/target/*.war /usr/local/tomcat/webapps/
Deploy.yaml
apiVersion: apps/v1kind: Deploymentmetadata:name: czdataease-javanamespace: br-demo-projectannotations:deployment.kubernetes.io/revision: '1'kubesphere.io/creator: br-project-regularspec:replicas: 1selector:matchLabels:app: czdataease-javatemplate:metadata:labels:app: czdataease-javaspec:imagePullSecrets: # 如过使用harbor仓库得镜像,需要设置从secret处拉取镜像 查看 kubectl get secret- name: harbor-pullcontainers:- name: czdataease-javaimage: bairong.k8s.master1/br-sjzl/czdataease-java:latestimagePullPolicy: Alwaysports:- containerPort: 8080name: nginxvolumeMounts:- name: czdataease-nfs-volumemountPath: /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-volumepersistentVolumeClaim:claimName: br-czdataease-nfs-pvc ---apiVersion: v1kind: Servicemetadata:name: czdataease-java-servicenamespace: br-demo-projectannotations:deployment.kubernetes.io/revision: '1'kubesphere.io/creator: br-project-regularspec:selector:app: czdataease-javaports:- protocol: TCPname: nginxport: 8080targetPort: 8080nodePort: 30261type: NodePort
Jenkins
pipeline {agent {node {label 'nodejs'}}stages {stage('拉取') {agent nonesteps {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 nonesteps {container('nodejs') {sh 'npm install --registry=http://192.168.19.197:31081/repository/npm-group/'sh 'npm run build'}}}stage('打包') {agent nonesteps {container('nodejs') {sh 'docker build -t czdataease-vue:latest -f docker/Dockerfile .'}}}stage('推送') {agent nonesteps {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 nonesteps {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: '')}}