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