公司项目有springmvc和springboot两种类型,springmvc项目主要通过tomcat启动,springboot项目主要通过java [option] -jar *.jar的方式启动。我们准备把springmvc项目和springboot项目先做成镜像先在灰度环境跑,如果没有问题我们再把镜像push到生产镜像仓库,最后选择在灰度验证通过的镜像运行在k8s上从而实现springmvc迁移。
contentName="$project"
currentTime=`date "+%Y%m%d%H%M"`
echo $currentTime
export PATH="/opt/apache-maven-3.3.3/bin:$PATH"
projectVals=(${projectVals//,/ })
projectName=${projectVals[0]}
containerArray=(${projectName//-/ })
container=${containerArray[1]}
project=${projectVals[1]}
finalName=${projectVals[2]}
if [ "$finalName" == "" ]; then
finalName=$project
fi
tag=$finalName-${gitVersion##*/}-$currentTime
TestEnvs=(${TestEnv//,/ })
serverName=${TestEnvs[0]}
serverHost=${TestEnvs[1]}
if [ "$gitVersion" == "" ]; then
echo "gitVersion不能为空"
exit
fi
cd /plk/source
if [ "true" != "$isSpringBoot" ] ; then
rm -rf $finalName
sshpass -p $remotePasswd scp -r -P 3721 $serverHost:/programs/$project/target/$finalName .
cd $finalName/WEB-INF/classes
pwd
ls
if [ -f "$finalName-mq.properties" ] ; then
sed -i 's|.mg.addr|-mg-addr|g' $finalName-mq.properties
sed -i 's|mq-mg-addr|mq1-mg-addr:9876;mq2-mg-addr:9876;mq3-mg-addr|g' $finalName-mq.properties
fi
echo "set prod zookeeper"
sed -i 's|{dubbo.registry.address}|{dubbo.registry.cluster.address}|g' $finalName-*.xml
if [ ! -z "`grep 'jdbc.properties' $finalName-spring-context-server.xml`" ]; then
echo "set prod mysql config"
sed -i 's|jdbc.mg.addr:3306/moni|rm-***.mysql.rds.aliyuncs.com:3306/moni_b|g' *jdbc.properties
sed -i 's|jdbc.mg.addr:3306/game|rm-***.mysql.rds.aliyuncs.com:3306/game_b|g' *jdbc.properties
sed -i 's|.username=***|.username=***|g' *jdbc.properties
sed -i 's|.password=***|.password=***|g' *jdbc.properties
fi
if [ ! -z "`grep 'mongo.properties' $finalName-spring-context-server.xml`" ]; then
echo "set prod mongo config"
sed -i 's|=mongo.mg.addr:27017|=dds-***.mongodb.rds.aliyuncs.com:3717,dds-***.mongodb.rds.aliyuncs.com:3717|g' *-mongo.properties
sed -i 's|mongo.mg.addr|dds-***.mongodb.rds.aliyuncs.com|g' *-mongo.properties
sed -i 's|mongo.readonly.mg.addr|dds-***.mongodb.rds.aliyuncs.com|g' *-mongo.properties
sed -i 's|database=statistics|database=statistics_b|g' *-mongo.properties
sed -i 's|database=everydaykline|database=everydaykline_b|g' *-mongo.properties
sed -i 's|database=evaluation|database=evaluation_b|g' *-mongo.properties
sed -i 's|database=safedata|database=safedata|g' *-mongo.properties
sed -i 's|credentials=evaluation:***@evaluation|credentials=***:***@evaluation_b|g' *-mongo.properties
sed -i 's|credentials=statistics:***@statistics|credentials=***:***@statistics_b|g' *-mongo.properties
sed -i 's|credentials=everydaykline:***@everydaykline|credentials=***:***@everydaykline_b|g' *-mongo.properties
sed -i 's|credentials=safedata:***@safedata|credentials=***:***@safedata|g' *-mongo.properties
sed -i 's|27017|3717|g' *-mongo.properties
fi
if [ ! -z "`grep 'redis' $finalName-spring-context-server.xml`" ]; then
echo "set prod redis config"
sed -i 's|redis-cluster|redis|g' $finalName-spring-context-server.xml
sed -i 's|=redis.mg.addr|=mgsc-***.redis.rds.aliyuncs.com|g' $finalName-*redis.properties
sed -i 's|=Mogu07550831..|=Mogu07550831**|g' $finalName-*redis.properties
fi
cd /plk/source
cp -f /plk/source/Dockerfile.mvc Dockerfile/Dockerfile.${finalName}
sed -i "s|{project}|${finalName}|g" Dockerfile/Dockerfile.${finalName}
else
//次数逻辑为springboot项目逻辑,下篇文章详细展开
fi
sudo docker build -f /plk/source/Dockerfile/Dockerfile.${finalName} -t registry-vpc.cn-shenzhen.aliyuncs.com/moguyun-pre/$dockerStore:$tag .
sudo docker login --username $dockerHubName --password $dockerHubPasswd registry-vpc.cn-shenzhen.aliyuncs.com
sudo docker push registry-vpc.cn-shenzhen.aliyuncs.com/moguyun-pre/$dockerStore:$tag
if [ "true" == "$isPreUpdate" ] ; then
sshpass -p Mogupro0601sz ssh root@$preEnvHost "cd /docker/k8s; sh -x ${projectName}.sh $tag 2>&1 &"
fi
if [ "true" == "$isIncUpdate" ] ; then
sshpass -p Mogupro0601sz ssh root@$preEnvHost "cd /opt/apps; rm -rf $finalName && docker cp $container:/opt/tomcat/webapps/$finalName ."
fi
] # vim Dockerfile.mvc
FROM registry.moguyun.com/tomcat:9.26
MAINTAINER lijun moguyun.com
ENV SOURCEPATH {project}
ENV TARGETPATH /opt/tomcat/webapps
ENV RUN_OPTION "-Xms1g -Xmx4g -Xmn256m -Dfile.encoding=UTF-8"
ENV PNAME {project}
ENV RUNPORT 0
ADD $SOURCEPATH $TARGETPATH/$SOURCEPATH
EXPOSE 8080
CMD ["sh", "-c", "/bin/sysinit.sh \"$RUN_OPTION\" \"$PNAME\" \"$RUNPORT\" "]
] # vim commom.sh
#!/bin/sh
project=${1}
pro=${project//_/-}
pname=${2}
RUN_PORT=${4}
DEBUG_PORT=${5}
version="registry-vpc.cn-shenzhen.aliyuncs.com/moguyun-pre/env-b:${3}"
JVM_OPTION="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=${DEBUG_PORT}
-Duser.timezone=GMT+08 -Dfile.encoding=UTF-8 -server -Xms256m -Xmx1024m "
echo "/docker/tomcat/logs/$project"
echo "JVM_OPTION=$JVM_OPTION"
echo "a1=$1"
echo "a2=$2"
echo "a3=$3"
echo "a4=$4"
echo "a5=$5"
docker stop $pname && docker rm $pname
docker run -d --name $pname \
-v /opt/image_data:/image_data:rw \
-v /opt/image_data:/opt/image_data:rw \
-v /etc/hosts:/etc/hosts:ro \
-v /docker/tomcat/logs/$project:/opt/tomcat/logs:rw \
-p $RUN_PORT:8080 \
-p $DEBUG_PORT:$DEBUG_PORT \
-e RUN_OPTION="$JVM_OPTION" \
-e PNAME="$pname" \
$version
sleep 35
docker logs --tail 400 $pname
] # vim financial.sh
/docker/k8s/common.run financial-shop-server financial $1 6207 9909
开发有时需要增量更新一个类,而不是全量,因为这个服务可能多个开发同时在开发,如果在上一个自由风格项目勾选了【isPreUpdate】,就会把容器里面的代码拷贝一份出来到 /opt/apps目录,然后通过winscp连接到灰度【1.226】机器,把增量类替换/opt/apps目录下的服务,然后docker cp拷贝到容器里面,重启容器。
通过执行restartFinancial就可以重新启动了,我们把restartFinancial定义成一个别名,这个别名执行restartServer.sh脚本
] # vim restartServer.sh
#!/bin/bash
docker cp /opt/apps/$1 $2:/opt/tomcat/webapps/ && docker restart $2 ; docker logs $2 -f --tail 400
] # vim ~/.bashrc
alias restartFinancial='sh -x /opt/apps/restartServer.sh financial-shop-server financial'
推送镜像到远程仓库主要实现过程就是docker commit容器成镜像,然后docker push到生产镜像仓库。
currentTime=`date "+%Y%m%d%H%M"`
echo $currentTime
projectVals=(${projectVals//,/ })
containerName=${projectVals[0]}
projectName=${projectVals[1]}
echo "cd /opt/apps"
cd /opt/apps
echo "tag=${projectName}_${currentTime}"
tag=${projectName}_${currentTime}
echo "docker commit -m "$containerName is commited at $currentTime" $containerName registry-vpc.cn-***.com/moguyun-prod/env-b:$tag"
docker commit -m "$containerName is commited at $currentTime" $containerName registry-vpc.cn-***.com/moguyun-prod/env-b:$tag
echo "docker login --username $dockerHubName --password $dockerHubPasswd registry-vpc.cn-shenzhen.aliyuncs.com"
docker login --username $dockerHubName --password $dockerHubPasswd registry-vpc.cn-shenzhen.aliyuncs.com
echo "docker push registry-vpc.cn-shenzhen.aliyuncs.com/moguyun-prod/$repository:$tag"
docker push registry-vpc.cn-shenzhen.aliyuncs.com/moguyun-prod/$repository:$tag
至此:已经完成了SpringMVC应用迁移到生产K8S集群的所有步骤