Jenkins+GitLab+docker+springboot 实现自动化部署

38 篇文章 0 订阅
5 篇文章 0 订阅
本文介绍如何利用Jenkins配合GitLab、Docker及Kubernetes实现SpringBoot项目的自动化部署流程,包括配置Jenkins任务、定义Kubernetes部署文件、编写Ansible脚本等关键步骤。
摘要由CSDN通过智能技术生成

Jenkins+GitLab+docker+springboot 实现自动化部署

jenkins设置

k8s - 新建ltem - 构建一个maven项目
k8s - 新建ltem
在这里插入图片描述
构建一个maven项目
在这里插入图片描述
这是设置的GitLab
在这里插入图片描述
设置你的源码仓库,一下为git
在这里插入图片描述
构建触发器设置
在这里插入图片描述
构建环境设置
在这里插入图片描述
脚本启动java项目
在这里插入图片描述

Post Steps运行脚本

ansible 192.168.1.191 -m shell -a 'nohup /bin/sh /home/workspace/vcollege-twenty-uims-core/springboot-docker.sh vcollege-twenty-uims-core’

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: springboot
  name: springboot
spec:
  replicas: 2
  selector:
    matchLabels:
      app: springboot
  strategy: # 策略
    rollingUpdate: # 滚动更新
      maxSurge: 30% # 最大额外可以存在的副本数,可以为百分比,也可以为整数
      maxUnavailable: 30% # 示在更新过程中能够进入不可用状态的 Pod 的最大值,可以为百分比,也可以为整数
    type: RollingUpdate # 滚动更新策略
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: springboot
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/xinhuixu/public:springboot
        name: public
        imagePullPolicy: Always
        resources: # 资源管理
          limits: # 最大使用
            cpu: 700m # CPU,1核心 = 1000m
            memory: 2000Mi # 内存,1G = 1024Mi
          requests:  # 容器运行时,最低资源需求,也就是说最少需要多少资源容器才能正常运行
            cpu: 700m
            memory: 1500Mi
        ports:
          - name: http
            containerPort: 0000
            protocol: TCP
        env:
        - name: TZ
          value: Asia/Shanghai
        lifecycle:
          preStop:
            exec:
              command: ["sleep","5"]
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: springboot
  name: springboot
spec:
  ports:
  - port: 0000
    protocol: TCP
    targetPort: 0000
    nodePort: 31111
  selector:
    app: springboot
  type:  NodePort #ClusterIP 
status:
  loadBalancer: {}

ansible 192.168.1.191 -m shell -a 'nohup /bin/sh /home/k8s-jenkins/jenkins191.sh vcollege-twenty-uims-core 8041’

#/bin/bash
while [ -n "$1" ]  
do
  case "$1" in
    vcollege-twenty-register-server)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
#        cd /home/k8s-jenkins/ && cp k8s-springboot.yaml k8s-${1}.yaml && sed -i 's/springboot/'$1'/g' k8s-${1}.yaml && sed -i 's/0000/'$2'/g' k8s-${1}.yaml
#	sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' /home/k8s-jenkins/springboot.yaml > /home/k8s-jenkins/k8s-${1}.yaml
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/1111/'$3'/g' springboot.yaml > k8s-${1}.yaml
        kubectl apply -f /home/k8s-jenkins/k8s-${1}.yaml
#        rm -f k8s-${1}.yaml
	;;
    vcollege-twenty-uims-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
#        cd /home/k8s-jenkins/ && cp k8s-springboot.yaml k8s-${1}.yaml && sed -i 's/springboot/'$1'/g' k8s-${1}.yaml && sed -i 's/0000/'${2}'/g' k8s-${1}.yaml
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
	sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-common-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-file-server)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-file-server-pre)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/1111/'$3'/g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-leave-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
#        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-mq-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-pay-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-school-pre)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/1111/'$3'/g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-sign-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-student-pre)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/1111/'$3'/g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-survey-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-sysadmin-pre)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/1111/'$3'/g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-task-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-teacher-pre)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/1111/'$3'/g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    vcollege-twenty-welcomenew-core)
        kubectl get svc,deployment|grep $1|awk '{print "kubectl delete " $1}'|bash
        cd /home/k8s-jenkins/ && sed -e 's/springboot/'$1'/g' -e 's/0000/'$2'/g' -e 's/    nodePort: 31111//g' springboot.yaml > k8s-${1}.yaml
        sleep 1
        kubectl apply -f k8s-${1}.yaml
        rm -f k8s-${1}.yaml
        ;;
    *)
        echo "$1 is not an option"  
        ;;
  esac
  shift
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

主主主主公

你的鼓励将大动力作的最是我创

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值