1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| echo -e "===prepare workspace===" if [ ! -d "workspace" ]; then echo "create new workspace" mkdir workspace fi cd workspace
echo -e "===goto current space===" version=$[$(ls | sort -n | tail -n 1)+1] mkdir $version cd $version echo "Version: $version" echo "Space: $(pwd)"
echo -e "===set parmas===" gitPath=xxxx girBranch=xxxx
mavenMirror=xxxx mavenCacheVolume=maven-repo
mavenImage=maven:3.6.3-jdk-8 mavenPackageTarget=xxx-start/target/*.jar
jdkImage=openjdk:8-jdk javaApp=xxxx
echo -e "===get code===" docker run -i --rm \ -v ${HOME}:/root \ -v $(pwd)/src:/git \ alpine/git \ clone $gitPath . pwd echo $girBranch docker run -i --rm \ -v ${HOME}:/root \ -v $(pwd)/src:/git \ alpine/git \ checkout $girBranch
echo -e "===build target===" mkdir .m2 cat>.m2/settings.xml<<EOF <settings> <mirrors> <mirror> <id>proxy</id> <mirrorOf>central</mirrorOf> <name>proxy maven</name> <url>$mavenMirror</url> </mirror> </mirrors> </settings> EOF docker volume create --name $mavenCacheVolume docker run -i --rm \ -v $(pwd)/src:/usr/src/mymaven \ -v $mavenCacheVolume:/root/.m2/repository \ -v $(pwd)/.m2/settings.xml:/root/.m2/settings.xml \ -w /usr/src/mymaven \ $mavenImage \ mvn package -Dmaven.test.skip=true
echo -e "===move jar===" mkdir image mv src/$mavenPackageTarget image/main.jar
echo -e "===build image===" cd image cat>Dockerfile<<EOF FROM $jdkImage COPY main.jar /main.jar COPY entrypoint.sh /entrypoint.sh CMD ["sh","entrypoint.sh"] EOF cat>entrypoint.sh<<EOF java -jar -Xmx250m -Xms200m -Dserver.port=80 /main.jar --logger.print-parmas.enable=true EOF docker build -t $javaApp:$version . cd ..
echo -e "===deploy to k8s===" mkdir deploy cd deploy cat>${javaApp}-deployment.yaml<<EOF apiVersion: apps/v1 kind: Deployment metadata: name: ${javaApp}-deployment labels: app: $javaApp spec: replicas: 1 selector: matchLabels: app: $javaApp template: metadata: labels: app: $javaApp spec: containers: - name: $javaApp image: $javaApp:$version imagePullPolicy: IfNotPresent env: - name: ENV value: "env" ports: - containerPort: 80 resources: limits: cpu: 0.3 memory: 400Mi requests: cpu: 0.3 memory: 300Mi livenessProbe: httpGet: path: /swagger-ui/ port: 80 initialDelaySeconds: 100 periodSeconds: 3 strategy: # 策略 type: RollingUpdate # 也可以是Recreate rollingUpdate: maxUnavailable: 50% # 滚动更新的时候的最大不可用pod数量, 可以是绝对数字或者比例10% maxSurge: 50% # 动更新的时候的溢出的pod数量,也可以是绝对数字 progressDeadlineSeconds: 150 # 进度期限秒数,不懂是什么 minReadySeconds: 100 # 最短就绪时间, 容器创建多久以后被视为就绪 revisionHistoryLimit: 3 # 历史修订限制, 保留的rs的数量,这个数量会消耗etcd资源,rs删除了就不能回滚刀那个版本的Deployment了 EOF
cat>${javaApp}-service.yaml<<EOF apiVersion: v1 kind: Service metadata: name: ${javaApp}-service spec: type: NodePort selector: app: $javaApp ports: - port: 80 targetPort: 80 nodePort: 10010 EOF
kubectl apply -f ${javaApp}-deployment.yaml kubectl apply -f ${javaApp}-service.yaml cd ..
|