k8s 项目最新jenkins部署shell
Posted fengjian2016
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s 项目最新jenkins部署shell相关的知识,希望对你有一定的参考价值。
# 定义变量 SERVERNAME=`echo $JOB_NAME | awk -F ‘.‘ ‘{print $1}‘` LSERVERNAME=`echo "$SERVERNAME" | sed ‘s/-//g‘ | tr ‘[A-Z]‘ ‘[a-z]‘` GET_RESULT=`echo $JOB_NAME | awk -F ‘.‘ ‘{print $2}‘` TAG_NUMBER=`echo $(date "+%Y-%m-%d-%H-%M-%S")| sed ‘s/-//g‘` mkdir -p /data/deployment/$SERVERNAME #调用的证书 k8S_APICURL="curl --cacert /data/deployment_git/ca.pem --cert /data/deployment_git/admin.pem --key /data/deployment_git/admin-key.pem" replace_Dockerfile() { if [ "$GET_RESULT" == "jar" ] ;then /bin/cp /data/deployment/Docker-Template/$GET_RESULT/start.sh /data/deployment/$LSERVERNAME/ mv $LSERVERNAME.$GET_RESULT /data/deployment/$LSERVERNAME/ sed "s/jenkins/$LSERVERNAME/g" /data/deployment/Docker-Template/$GET_RESULT/Dockerfile > /data/deployment/$LSERVERNAME/Dockerfile sed -i "s#IMAGE#$javabaseimage#g" /data/deployment/$LSERVERNAME/Dockerfile cp -ar /data/deployment/Docker-Template/agent /data/deployment/$LSERVERNAME/ elif [ "$GET_RESULT" == "js" ] ; then cd /data/deployment/$LSERVERNAME unzip $LSERVERNAME.zip -d ./ cp /data/deployment/Docker-Template/$GET_RESULT/nginx.k8s.conf /data/deployment/$LSERVERNAME/nginx.conf sed "s/jenkins/dist/g" /data/deployment/Docker-Template/$GET_RESULT/Dockerfile > /data/deployment/$LSERVERNAME/Dockerfile sed -i "s#IMAGE#$nginxbaseimage#g" /data/deployment/$LSERVERNAME/Dockerfile elif [ "$GET_RESULT" == "war" ] ; then name=`ls -l /data/deployment/$LSERVERNAME/*.$GET_RESULT | awk -F ‘/‘ ‘{print $5}‘` #如果上传的war与任务名称不同,修改 if [[ $name != $JOB_NAME ]]; then mv /data/deployment/$LSERVERNAME/*.$GET_RESULT /data/deployment/$LSERVERNAME/$LSERVERNAME.$GET_RESULT fi #解压缩war包 unzip /data/deployment/$LSERVERNAME/$LSERVERNAME.$GET_RESULT -d /data/deployment/$LSERVERNAME/$LSERVERNAME/ #替换Dockerfile项目名字 sed "s/jenkins/$LSERVERNAME/g" /data/deployment/Docker-Template/$GET_RESULT/Dockerfile > /data/deployment/$LSERVERNAME/Dockerfile #替换java的基础镜像 sed -i "s#IMAGE#$javabaseimage#g" /data/deployment/$LSERVERNAME/Dockerfile #替换tomcat server.xml文件端口 sed "s#containerport#$containerport#g" /data/deployment/Docker-Template/$GET_RESULT/server.xml > /data/deployment/$LSERVERNAME/server.xml # 修改catalina.sh 日志路径 if `ls -l /data/deployment/Docker-Template/$GET_RESULT/*.sh | grep $LLSERVERNAME >/dev/null`; then cp /data/deployment/Docker-Template/$GET_RESULT/catalina$LLSERVERNAME.sh /data/deployment/$LSERVERNAME/catalina.sh fi fi #替换端口 sed -i "s#containerport#$containerport#g" /data/deployment/$LSERVERNAME/Dockerfile if [ "$?" != 0 ]; then exit 1 fi } push_image() { #转换成小写 docker build -t $REGISTRY/${namespace_name}/$LSERVERNAME:${TAG_NUMBER} /data/deployment/$LSERVERNAME/ docker push $REGISTRY/${namespace_name}/$LSERVERNAME:${TAG_NUMBER} if docker images | grep $LSERVERNAME; then docker images | grep $LSERVERNAME | awk ‘{print $3}‘ | xargs docker rmi fi rm -rf /data/deployment/$LSERVERNAME/* } replace_deployment() { sed "s/im-web/$LSERVERNAME/g" /data/deployment/Docker-Template/${GET_RESULT}/im-web-deployment.yaml >/data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml # 启动pod的个数 sed -i "s/numberpod/${numberpod}/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml #限制pod cpu使用数量 1000m表示一核 sed -i "s/LIMITCPU/${limitcpu}/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml # 修改容器端口 sed -i "s#containerport#$containerport#g" /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml sed -i "s/192.168.200.10/senyint/$LSERVERNAME:latest/$REGISTRY/senyint/$LSERVERNAME:${TAG_NUMBER}/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml if `$k8S_APICURL --insecure -X GET $K8S_URL/api/v1/namespaces/senyint/configmaps | grep senyintconfimap >/dev/null`; then >/data/deployment/$LSERVERNAME/$LSERVERNAME-configmap.yaml mkdir /data/deployment/configmap/ -p sed "s/testihospital/${namespace_name}/g" /data/deployment/Docker-Template/configmap/test_get_configmap.py > /data/deployment/configmap/senyint_get_configmap.py sed -i "s#https://192.168.200.11:6443#$K8S_URL#g" /data/deployment/configmap/senyint_get_configmap.py /usr/bin/python /data/deployment/configmap/senyint_get_configmap.py| while read line; do if grep -v "#env" /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml;then sed -i ‘s/#env/env/g‘ /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml fi name=`echo $line | awk -F ‘#‘ ‘{print $1}‘` value=`echo $line | awk -F ‘#‘ ‘{print $2}‘` confimapvalue=`echo $line | awk -F ‘#‘ ‘{print $3}‘` sed "s/testenv/$confimapvalue/g" /data/deployment/Docker-Template/configmap/configmap-env.yaml >> /data/deployment/$LSERVERNAME/$LSERVERNAME-configmap.yaml sed -i "s/mysql_server/$name/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-configmap.yaml done fi #添加configmap文件 sed -i "/env/r /data/deployment/$LSERVERNAME/$LSERVERNAME-configmap.yaml" /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml } replace_ingress() { sed -i "s/default/${namespace_name}/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml sed -i "s/nginx.cinyi.com/${DNS_NAME}/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml sed -i "s/servicename/$LSERVERNAME/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml sed -i "s#80#$containerport#g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml } replace_ingress_ssl() { sed -i "s/default/${namespace_name}/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml sed -i "s/nginx.cinyi.com/${DNS_NAME}/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml sed -i "s/servicename/$LSERVERNAME/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml sed -i "s#80#$containerport#g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml } add_ingress() { # 假如使用http方式访问 if [ $https -eq 1 ] ; then if ! `$k8S_APICURL --insecure -X GET $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses | grep -E "${DNS_NAME}|"name": "$LSERVERNAME"" >/dev/null`; then if [ $accessmode -eq 0 ] ; then cat /data/deploymentt/Docker-Template/ingress/ingress-domain-template.yaml > /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml replace_ingress $k8S_APICURL --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml)" else cat /data/deployment/Docker-Template/ingress/ingress-domain-project-template.yaml > /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml replace_ingress sed -i "s/projectname/$LSERVERNAME/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml $k8S_APICURL --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml)" fi else #假如有DNS_NAME.cinyi.com域名,那么更新 if [ $accessmode -eq 0 ] ; then cat /data/deployment/Docker-Template/ingress/ingress-domain-template.yaml > /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml replace_ingress #更新 $k8S_APICURL --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/$LSERVERNAME -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml)" else cat /data/deployment/Docker-Template/ingress/ingress-domain-project-template.yaml > /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml replace_ingress sed -i "s/projectname/$LSERVERNAME/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml #重新创建 $k8S_APICURL --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/$LSERVERNAME -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress.yaml)" fi fi else if ! `$k8s_apicurl --insecure -X GET $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses | grep -E "${DNS_NAME}|"name": "$LSERVERNAME"">/dev/null`; then if [ $accessmode -eq 0 ] ; then cat /data/deployment/Docker-Template/ingress/ingress-domain-template-ssl.yaml > /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml replace_ingress_ssl $k8S_APICURL --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml)" else cat /data/deployment/Docker-Template/ingress/ingress-domain-project-template—ssl.yaml > /data/deployment/$SERVERNAME/$LSERVERNAME-ingress-ssl.yaml replace_ingress_ssl sed -i "s/projectname/$LSERVERNAME/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml $k8S_APICURL --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml)" fi else #假如有DNS_NAME.cinyi.com域名,那么更新 if [ $accessmode -eq 0 ] ; then cat /data/deployment/Docker-Template/ingressingress-domain-template-ssl.yaml > /data/deployment/$SERVERNAME/$LSERVERNAME-ingress-ssl.yaml replace_ingress_ssl #更新 $k8S_APICURL --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/$LSERVERNAME -d "$(cat /data/deployment/$SERVERNAME/$LSERVERNAME-ingress-ssl.yaml)" else cat /data/deployment/Docker-Template/ingress/ingress-domain-project-template-ssl.yaml > /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml replace_ingress_ssl sed -i "s/projectname/$LSERVERNAME/g" /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml #重新创建 $k8S_APICURL --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/$LSERVERNAME -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-ingress-ssl.yaml)" fi fi fi } replace_service() { sed "s/im-web/$LSERVERNAME/g" /data/deployment/Docker-Template/${GET_RESULT}/im-web-service.yaml >/data/deployment/$LSERVERNAME/$LSERVERNAME-service.yaml # 修改容器端口 sed -i "s#containerport#$containerport#g" /data/deployment/$LSERVERNAME/$LSERVERNAME-service.yaml } update_service() { if ! `$k8S_APICURL --insecure -X GET $K8S_URL/api/v1/namespaces/senyint/services | grep "$LSERVERNAME" >/dev/null` ; then $k8S_APICURL --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/api/v1/namespaces/senyint/services -d "$(cat //data/deployment/$LSERVERNAME/$LSERVERNAME-service.yaml)" fi if [ "$?" != 0 ]; then exit 1 fi } update_deployment() { if `$k8S_APICURL --insecure -X GET $K8S_URL/apis/apps/v1/namespaces/senyint/deployments/ | grep ""$LSERVERNAME"" >/dev/null` ; then #滚动升级 $k8S_APICURL --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/apps/v1/namespaces/senyint/deployments/$LSERVERNAME -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml)" else $k8S_APICURL --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/apps/v1/namespaces/senyint/deployments/ -d "$(cat /data/deployment/$LSERVERNAME/$LSERVERNAME-deployment.yaml)" fi if [ "$?" != 0 ]; then exit 1 fi check_pod } check_pod() { sleep 60 sed "s/testhospital/${namespace_name}/g" /data/deployment/Docker-Template/configmap/check.py > /data/deployment/$LSERVERNAME/check.py sed -i "s#https://192.168.200.11:6443#$K8S_URL#g" /data/deployment/$LSERVERNAME/check.py sed -i "s#edirestserver#$LSERVERNAME#g" /data/deployment/$LSERVERNAME/check.py /usr/bin/python /data/deployment/$LSERVERNAME/check.py | while read line; do if [ $line != "Running" ]; then echo "error" exit 1 fi done } if [ 0"$DNS_NAME" = "0" ]; then DNS_NAME="$LSERVERNAME.cinyi.com" fi replace_Dockerfile push_image replace_deployment replace_service update_service add_ingress update_deployment
#####################################################
#镜像打包上传 image_push() { #处理-问题 docker build -t $REGISTRY/${NAMESPACE_NAME}/$javaname:${TAG_NUMBER} /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/ docker push $REGISTRY/${NAMESPACE_NAME}/$javaname:${TAG_NUMBER} #删除以前的镜像,节省本地空间 if docker images | grep $javaname ; then docker images | grep $javaname | awk ‘{print $3}‘ | xargs docker rmi -f fi } replace_k8s_yaml() { #替换模版中namespace sed "s/test-namespace/$NAMESPACE_NAME/g" /data/deployment_git/k8s-template/${GET_RESULT}/namespaces.yaml >/data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-namespaces.yaml #替换模版中 deployment文件 sed "s/im-web/$javaname/g" /data/deployment_git/k8s-template/${GET_RESULT}/im-web-deployment.yaml >/data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml sed -i "s/test-namespace/$NAMESPACE_NAME/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml #启动pod的数量 sed -i "s/numberpod/${numberpod}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml #限制pod cpu使用数量 1000m表示一核 sed -i "s/LIMITCPU/${limitcpu}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml #修改容器端口 sed -i "s#containerport#$containerport#g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml #修改image sed -i "s/192.168.200.10/${NAMESPACE_NAME}/$javaname:latest/$REGISTRY/${NAMESPACE_NAME}/$javaname:${TAG_NUMBER}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml #判断 namespace命名空间是否存在,没有创建 if ! `$k8s_apicurl --insecure -X GET $K8S_URL/api/v1/namespaces | grep ${NAMESPACE_NAME} >/dev/null`; then $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/api/v1/namespaces/ -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/${javadir}/${javaname}-namespaces.yaml)" fi # 获取namespace下configmap if `$k8s_apicurl --insecure -X GET $K8S_URL/api/v1/namespaces/${NAMESPACE_NAME}/configmaps | grep ${NAMESPACE_NAME}configmap >/dev/null`; then #rm -rf /data/deployment_git/docker-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml mkdir /data/deployment_git/k8s-project-${GET_RESULT}/configmap/ -p sed "s/testihospital/${NAMESPACE_NAME}/g" /data/deployment_git/k8s-template/configmap/test_get_configmap.py > /data/deployment_git/k8s-project-${GET_RESULT}/configmap/${NAMESPACE_NAME}_get_configmap.py sed -i "s#https://192.168.200.11:6443#$K8S_URL#g" /data/deployment_git/k8s-project-${GET_RESULT}/configmap/${NAMESPACE_NAME}_get_configmap.py /usr/bin/python /data/deployment_git/k8s-project-${GET_RESULT}/configmap/${NAMESPACE_NAME}_get_configmap.py | while read line; do if grep -v "#env" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml;then sed -i ‘s/#env/env/g‘ /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml fi name=`echo $line | awk -F ‘#‘ ‘{print $1}‘` value=`echo $line | awk -F ‘#‘ ‘{print $2}‘` confimapvalue=`echo $line | awk -F ‘#‘ ‘{print $3}‘` sed "s/testenv/$confimapvalue/g" /data/deployment_git/k8s-template/configmap/configmap-env.yaml >> /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml #sed "s/testenv/configmap${NAMESPACE_NAME}/g" /data/deployment_git/Docker-Template/configmap/configmap-env.yaml >> /data/deployment_git/docker-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml sed -i "s/mysql_server/$name/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml done fi #添加configmap文件 sed -i "/env/r /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-configmap.yaml" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml #替换模版中server yaml文件 sed "s/im-web/$javaname/g" /data/deployment_git/k8s-template/${GET_RESULT}/im-web-service.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-service.yaml sed -i "s/test-namespace/$NAMESPACE_NAME/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-service.yaml # 修改容器端口 sed -i "s#containerport#$containerport#g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-service.yaml #替换镜像的版本号(时间为版本号) #sed -i "s/latest/${BUILD_NUMBER}/g" /data/deployment_git/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml # 启动pod的个数 #sed -i "s/numberpod/${numberpod}/g" /data/deployment_git/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml } create_update_k8s() { #定义namespace 为test: #update service if ! `$k8s_apicurl --insecure -X GET $K8S_URL/api/v1/namespaces/${NAMESPACE_NAME}/services | grep $javaname >/dev/null` ;then $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/api/v1/namespaces/${NAMESPACE_NAME}/services -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/${javadir}/${javaname}-service.yaml)" fi if [ "$?" != 0 ]; then exit 1 fi ##创建service #if `$k8s_apicurl --insecure -X GET $K8S_URL/api/v1/namespaces/${NAMESPACE_NAME}/services | grep "${javaname}" >/dev/null` ; then # $k8s_apicurl --insecure -H "content-Type: application/yaml" -X DELETE $k8s_url/api/v1/namespaces/${NAMESPACE_NAME}/services/${javaname} #fi #$k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces/${NAMESPACE_NAME}/services -d "$(cat /data/deployment_git/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml)" #创建Deployment if `$k8s_apicurl --insecure -X GET $K8S_URL/apis/apps/v1/namespaces/${NAMESPACE_NAME}/deployments/ | grep "${javaname}" >/dev/null` ; then #滚动升级 $k8s_apicurl --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/apps/v1/namespaces/${NAMESPACE_NAME}/deployments/${javaname} -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml)" else $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/apps/v1/namespaces/${NAMESPACE_NAME}/deployments/ -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml)" fi if [ "$?" != 0 ]; then exit 1 fi } replace_ingress() { sed -i "s/default/${NAMESPACE_NAME}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/nginx.cinyi.com/${DNS_NAME}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/servicename/${javaname}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s#80#$containerport#g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml } replace_ingress_ssl() { sed -i "s/default/${NAMESPACE_NAME}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml sed -i "s/nginx.cinyi.com/${DNS_NAME}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml sed -i "s/servicename/${javaname}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml sed -i "s#80#$containerport#g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml } add_ingress() { # 假如使用http方式访问 if [ $https -eq 1 ] ; then if ! `$k8s_apicurl --insecure -X GET $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses | grep -E "${DNS_NAME}|"name": "${javaname}"" >/dev/null`; then if [ $accessmode -eq 0 ] ; then cat /data/deployment_git/k8s-template/ingress/ingress-domain-template.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml replace_ingress $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" else cat /data/deployment_git/k8s-template/ingress/ingress-domain-project-template.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml replace_ingress #projectname=`echo ${javaname} | tr ‘a-z‘ ‘A-Z‘` sed -i "s/projectname/${javaname}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" fi else #假如有DNS_NAME.cinyi.com域名,那么更新 if [ $accessmode -eq 0 ] ; then cat /data/deployment_git/k8s-template/ingress/ingress-domain-template.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml replace_ingress #更新 $k8s_apicurl --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses/${javaname} -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" else cat /data/deployment_git/k8s-template/ingress/ingress-domain-project-template.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml replace_ingress #projectname=`echo ${javaname} | tr ‘a-z‘ ‘A-Z‘` sed -i "s/projectname/${javaname}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml #重新创建 $k8s_apicurl --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses/${javaname} -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" fi fi else if ! `$k8s_apicurl --insecure -X GET $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses | grep -E "${DNS_NAME}|"name": "${javaname}"">/dev/null`; then if [ $accessmode -eq 0 ] ; then cat /data/deployment_git/k8s-template/ingress/ingress-domain-template-ssl.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml replace_ingress_ssl $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml)" else cat /data/deployment_git/k8s-template/ingress/ingress-domain-project-template—ssl.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml replace_ingress_ssl #projectname=`echo ${javaname} | tr ‘a-z‘ ‘A-Z‘` sed -i "s/projectname/${javaname}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml)" fi else #假如有DNS_NAME.cinyi.com域名,那么更新 if [ $accessmode -eq 0 ] ; then cat /data/deployment_git/k8s-template/ingress/ingress-domain-template-ssl.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml replace_ingress_ssl #更新 $k8s_apicurl --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses/${javaname} -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml)" else cat /data/deployment_git/k8s-template/ingress/ingress-domain-project-template-ssl.yaml > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml replace_ingress_ssl #projectname=`echo ${javaname} | tr ‘a-z‘ ‘A-Z‘` sed -i "s/projectname/${javaname}/g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml #重新创建 $k8s_apicurl --insecure -H "content-Type: application/yaml" -X PUT $K8S_URL/apis/extensions/v1beta1/namespaces/${NAMESPACE_NAME}/ingresses/${javaname} -d "$(cat /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/${javaname}-ingress-ssl.yaml)" fi fi fi } check_pod() { sleep 120 sed "s/testhospital/${NAMESPACE_NAME}/g" /data/deployment_git/k8s-template/configmap/check.py > /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/check.py sed -i "s#https://192.168.200.11:6443#$K8S_URL#g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/check.py sed -i "s#edirestserver#$javaname#g" /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/check.py /usr/bin/python /data/deployment_git/k8s-project-${GET_RESULT}/$javadir/check.py | while read line; do if [ $line != "Running" ]; then echo "error" exit 1 fi done } replace_content() { #创建/data/deployment_git/docker-project-gz/js/jar/war目录 #if [ -f /data/deployment_git/docker-project-$GET_RESULT/$javadir/ ] ;then # rm -rf /data/deployment_git/docker-project-$GET_RESULT/$javadir/* # else #mkdir -p /data/deployment_git/docker-project-$GET_RESULT/$javadir/ #fi #删除原来的,创建新的目录 rm -rf /data/deployment_git/k8s-project-$GET_RESULT/$javadir mkdir -p /data/deployment_git/k8s-project-$GET_RESULT/$javadir #将编译后的war包拷贝到/data/deployment_git/k8s-project-war/$javadir目录下 mv $javaname_path /data/deployment_git/k8s-project-$GET_RESULT/$javadir/$javaname.${GET_RESULT} if [ "$1" == "war" ] ;then #在/data/deployment_git/Docker-Template 目录下有一个dockerfile模版,根据war包的名字替换成新的dockerfile if [ $accessmode -eq 0 ] ; then #替换Dockerfile中的war 名字 sed "s/jenkins/$javaname/g" /data/deployment_git/k8s-template/$GET_RESULT/Dockerfile_webserver >/data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile else sed "s/jenkins/$javaname/g" /data/deployment_git/k8s-template/$GET_RESULT/Dockerfile >/data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile sed -i ‘s/^#ADD server.xml/Add server.xml/g‘ /data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile fi #替换基础镜像 sed -i "s#IMAGE#$javabaseimage#g" /data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile #替换server.xml sed "s#containerport#$containerport#g" /data/deployment_git/k8s-template/$GET_RESULT/server.xml > /data/deployment_git/k8s-project-$GET_RESULT/$javadir/server.xml elif [ "$1" == "jar" ] ;then #替换Dockerfile中的war 名字 sed "s/jenkins/$javaname/g" /data/deployment_git/k8s-template/$GET_RESULT/Dockerfile >/data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile /usr/bin/cp /data/deployment_git/k8s-template/$GET_RESULT/start.sh /data/deployment_git/k8s-project-$GET_RESULT/$javadir/ sed -i "s#IMAGE#$javabaseimage#g" /data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile elif [ "$1" == "js" ] ;then #将编译后的war包拷贝到/data/deployment_git/docker-project-js/$javadir目录下 cd $WORKSPACE #cnpm install --registry=http://192.168.20.71:8081/repository/npm-repo-proxy/ cnpm install --registry=https://registry.npm.taobao.org cnpm install sass-loader node-sass -D cnpm install cnpm run build mv $WORKSPACE/dist /data/deployment_git/k8s-project-$GET_RESULT/$javadir/ sed "s/jenkins/$javaname/g" /data/deployment_git/k8s-template/$GET_RESULT/Dockerfile >/data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile sed -i "s#IMAGE#$javabaseimage#g" /data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile cp /data/deployment_git/k8s-template/$GET_RESULT/$javaname.conf /data/deployment_git/k8s-project-$GET_RESULT/$javadir/nginx.conf #mv $WORKSPACE/config/nginx.conf /data/deployment_git/k8s-project-$GET_RESULT/$javadir/ #导入Dockerfile模板 #cp /data/deployment_git/k8s-template/$GET_RESULT/Dockerfile /data/deployment_git/k8s-project-$GET_RESULT/$javadir/ #sed -i "s/172.16.201.50:9001/empirest/g" /data/deployment_git/docker-project-js/$javadir/nginx.conf #sed -i "s/172.16.201.50:7001/webadmin/g" /data/deployment_git/docker-project-js/$javadir/nginx.conf #sed -i "s/172.16.201.50:6001/authserver/g" /data/deployment_git/docker-project-js/$javadir/nginx.conf fi #替换端口 sed -i "s#containerport#$containerport#g" /data/deployment_git/k8s-project-$GET_RESULT/$javadir/Dockerfile if [ "$?" != 0 ]; then exit 1 fi #delete_old_images image_push replace_k8s_yaml create_update_k8s add_ingress check_pod } #镜像仓库地址 #REGISTRY="172.16.230.84" #调用的证书 k8s_apicurl="curl --cacert /data/deployment_git/ca.pem --cert /data/deployment_git/admin.pem --key /data/deployment_git/admin-key.pem" #调用地址 #k8s_url="https://192.168.200.11:6443" #取出项目目录subject-bj-prod-serverapi-gz javadir=`echo $WORKSPACE | awk -F‘/‘ ‘{print $5}‘` #获取jar名称 11 #javaname=`echo $javadir| awk -F‘-‘ ‘{print $4}‘` javaname=`echo $javadir| awk -F‘-‘ ‘{print $4"-"$5}‘` #判断结尾名称格式war|jar|gz RESULT=`echo $javadir | awk -F‘-‘ ‘{print $NF}‘` GET_RESULT=`echo "$RESULT" | tr ‘[A-Z]‘ ‘[a-z]‘` #java的路径 /root/.jenkins/jobs/subject-bj-test-imserver/workspace/target/11.war ## 注意:如果存在分支情况,javaname_path 路径需要具体查看jenkins再做修改 #有分支,教育需要修改此处 #javaname_path=`find $WORKSPACE/$javaname-war/target/ -maxdepth 1 -type f | egrep ".${GET_RESULT}$"` #无分支 javaname_path=`find $WORKSPACE/target/ -maxdepth 1 -type f | egrep ".${GET_RESULT}$"` #获取jar名称 11.war javaname_all=`echo $javaname_path | awk -F‘/‘ ‘{print $NF}‘` #获取jar名称 11 javaname=`echo $javadir| awk -F‘-‘ ‘{print $4}‘` #取出环境(生产或者测试)prod environment=`echo $javadir | awk -F‘-‘ ‘{print $3}‘` #获取namespace #NAMESPACE_NAME=$environment`echo $javadir | awk -F‘-‘ ‘{print $1}‘` TAG_NUMBER=`echo $(date "+%Y-%m-%d-%H-%M-%S")| sed ‘s/-//g‘` #dns域名 #DNS_NAME="xkim" #DNS_NAME="$environment$javaname.cinyi.com" if [ 0"$DNS_NAME" = "0" ]; then DNS_NAME="$environment$javaname.cinyi.com" fi case "$GET_RESULT" in war|jar|gz|js) replace_content $GET_RESULT ;; *) echo "未知" exit 1 ;; esac
########################################################
configmap
replace_k8s_yaml() { #替换模版中namespace sed "s/test-namespace/$namespace_name/g" /data/deployment/Docker-Template/jar/namespaces.yaml > /data/deployment/configmap/$namespace_name/$namespace_name-namespaces.yaml #替换模版中configmap sed ‘$a ‘"$ORACLE_SERVER" /data/deployment/Docker-Template/jar/configmap.yaml > /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml sed -i ‘$a ‘"$ORACLE_SID" /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml sed -i ‘$a ‘"$NACOS_SERVER" /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml sed -i ‘$a ‘"$NACOS_PORT" /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml sed -i ‘$a ‘"$EMDI_SSO" /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml sed -i ‘$a ‘"$JAVA_OPT" /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml sed -i "s/testihospital/$namespace_name/g" /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml sed -i "s/testenv/$javaname/g" /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml } create_update_k8s() { #定义namespace 为test: k8s_apicurl="curl --cacert /data/deployment_git/ca.pem --cert /data/deployment_git/admin.pem --key /data/deployment_git/admin-key.pem" #k8s_url="https://192.168.200.11:6443" #创建namespaces if ! `$k8s_apicurl --insecure -X GET $k8s_url/api/v1/namespaces | grep ${namespace_name} >/dev/null` ;then $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces -d "$(cat /data/deployment/configmap/$namespace_name/$namespace_name-namespaces.yaml)" fi #创建confmap if `$k8s_apicurl --insecure -X GET $k8s_url/api/v1/namespaces/${namespace_name}/configmaps | grep "$javaname" >/dev/null` ; then $k8s_apicurl --insecure -H "content-Type: application/yaml" -X DELETE $k8s_url/api/v1/namespaces/${namespace_name}/configmaps/${javaname} fi $k8s_apicurl --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces/${namespace_name}/configmaps -d "$(cat /data/deployment/configmap/$namespace_name/$namespace_name-configmap.yaml)" } #取出项目目录subject-bj-prod-serverapi-gz #javadir=`echo $WORKSPACE | awk -F‘/‘ ‘{print $5}‘` # serverapi #javaname=`echo $javadir| awk -F‘-‘ ‘{print $4}‘` javaname="senyintconfimap" #取出环境(生产或者测试)prod #environment=`echo $javadir | awk -F‘-‘ ‘{print $3}‘` #获取namespace namespace_name="senyint" mkdir /data/deployment/configmap/$namespace_name -p replace_k8s_yaml create_update_k8s
以上是关于k8s 项目最新jenkins部署shell的主要内容,如果未能解决你的问题,请参考以下文章
使用Jenkins实现重构项目并部署项目到阿里k8s环境运行
使用Jenkins实现重构项目并部署项目到阿里k8s环境运行
在k8s+jenkins+github+dockerhub环境中用pipeline部署应用