jenkins 调用 k8s api
delete_old_images() { #删除以前的镜像,节省本地空间 if docker images | grep $javaname ; then docker rmi -f `docker images | grep $javaname | awk '{print $3}'` fi } #镜像打包上传 image_push() { #处理-问题 docker build -t 192.168.200.10/$namespace_name/$javaname:${BUILD_NUMBER} /export/docker-project-${GET_RESULT}/$javadir/ docker push 192.168.200.10/$namespace_name/$javaname:${BUILD_NUMBER} } replace_k8s_yaml() { #替换模版中namespace sed "s/test-namespace/$namespace_name/g" /export/Docker-Template/${GET_RESULT}/namespaces.yaml >/export/docker-project-${GET_RESULT}/$javadir/${javaname}-namespaces.yaml #替换模版中 deployment文件 sed "s/im-web/$javaname/g" /export/Docker-Template/${GET_RESULT}/im-web-deployment.yaml >/export/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml sed -i "s/test-namespace/$namespace_name/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml #替换模版中server yaml文件 sed "s/im-web/$javaname/g" /export/Docker-Template/${GET_RESULT}/im-web-service.yaml >/export/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml sed -i "s/test-namespace/$namespace_name/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml #替换镜像的版本号(时间为版本号) sed -i "s/latest/${BUILD_NUMBER}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml # 启动pod的个数 sed -i "s/numberpod/${numberpod}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml } create_update_k8s() { #定义namespace 为test: k8s_apicurl="curl --cacert /root/ca.pem" k8s_url="https://192.168.200.11:6443" #创建namespaces if ! `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/api/v1/namespaces | grep ${namespace_name} >/dev/null` ;then $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces -d "$(cat /export/docker-project-${GET_RESULT}/${javadir}/${javaname}-namespaces.yaml)" fi #创建service #if `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/api/v1/namespaces/${namespace_name}/services | grep "${javaname}" >/dev/null` ; then # $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X PUT $k8s_url/api/v1/namespaces/${namespace_name}/services/${javaname} -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml)" #else # $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces/${namespace_name}/services -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml)" #fi #创建service if `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/api/v1/namespaces/${namespace_name}/services | grep "${javaname}" >/dev/null` ; then $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X DELETE $k8s_url/api/v1/namespaces/${namespace_name}/services/${javaname} fi $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/api/v1/namespaces/${namespace_name}/services -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-service.yaml)" #创建Deployment if `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/apis/apps/v1beta2/namespaces/${namespace_name}/deployments/ | grep "${javaname}" >/dev/null` ; then #滚动升级 $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X PUT $k8s_url/apis/apps/v1beta2/namespaces/${namespace_name}/deployments/${javaname} -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml)" else $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/apis/apps/v1beta2/namespaces/${namespace_name}/deployments/ -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-deployment.yaml)" fi } add_ingress() { # 假如使用域名方式访问 if ! `$k8s_apicurl -basic -u fengjian:fengjian --insecure -X GET $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses | grep "${dnsname}.cinyi.com" >/dev/null`; then if [ $accessmode -eq 0 ] ; then cat /export/Docker-Template/ingress/ingress-domain-template.yaml > /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/default/${namespace_name}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/nginx/${dnsname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/servicename/${javaname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" else cat /export/Docker-Template/ingress/ingress-domain-project-template.yaml > /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/default/${namespace_name}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/nginx/${dnsname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/servicename/${javaname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml projectname=`echo ${javaname} | tr 'a-z' 'A-Z'` sed -i "s/projectname/${projectname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" fi else #假如有dnsname.cinyi.com域名,那么更新 if [ $accessmode -eq 0 ] ; then cat /export/Docker-Template/ingress/ingress-domain-template.yaml > /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/default/${namespace_name}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/nginx/${dnsname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/servicename/${javaname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml #curl --cacert /root/ca.pem -basic -u fengjian:fengjian --insecure -X DELETE https://192.168.200.11:6443/apis/extensions/v1beta1/namespaces/testsy/ingresses/adminserver #先删除 $k8s_apicurl -basic -u fengjian:fengjian --insecure -X DELETE $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/${javaname} #从新再创建 $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" else cat /export/Docker-Template/ingress/ingress-domain-project-template.yaml > /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/default/${namespace_name}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/nginx/${dnsname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml sed -i "s/servicename/${javaname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml projectname=`echo ${javaname} | tr 'a-z' 'A-Z'` sed -i "s/projectname/${projectname}/g" /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml #先删除 $k8s_apicurl -basic -u fengjian:fengjian --insecure -X DELETE $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses/${javaname} #重新创建 $k8s_apicurl -basic -u fengjian:fengjian --insecure -H "content-Type: application/yaml" -X POST $k8s_url/apis/extensions/v1beta1/namespaces/${namespace_name}/ingresses -d "$(cat /export/docker-project-${GET_RESULT}/$javadir/${javaname}-ingress.yaml)" fi fi } replace_content() { if [ "$1" == "gz" ] ;then #创建/export/docker-project-gz mkdir -p /export/docker-project-gz/$javadir #删除已经 if [ -f /export/docker-project-gz/$javadir/$javaname.tar.${GET_RESULT} ] ;then rm -rf /export/docker-project-gz/$javadir/$javaname.tar.${GET_RESULT} fi #将编译后的war包拷贝到/export/docker-project-gz/$javadir目录下 mv $javaname_path /export/docker-project-gz/$javadir/$javaname.tar.${GET_RESULT} #在/export/Docker-Template/gz 目录下有一个dockerfile模版,根据war包的名字替换成新的dockerfile #替换Dockerfile中的tar 名字 sed "s/jenkins/$javaname/g" /export/Docker-Template/gz/Dockerfile >/export/docker-project-gz/$javadir/Dockerfile elif [ "$1" == "war" ] ;then #创建/export/docker-project mkdir -p /export/docker-project-war/$javadir #删除已经 if [ -f /export/docker-profile-war/$javadir/$javaname.${GET_RESULT} ] ;then rm -rf /export/docker-profile-war/$javadir/$javaname.${GET_RESULT} fi #将编译后的war包拷贝到/export/docker-project-war/$javadir目录下 mv $javaname_path /export/docker-project-war/$javadir/$javaname.${GET_RESULT} #在/export/Docker-Template 目录下有一个dockerfile模版,根据war包的名字替换成新的dockerfile #替换Dockerfile中的war 名字 sed "s/jenkins/$javaname/g" /export/Docker-Template/war/Dockerfile >/export/docker-project-war/$javadir/Dockerfile elif [ "$1" == "jar" ] ;then #创建/export/docker-project-jar mkdir -p /export/docker-project-jar/$javadir #删除已经 if [ -f /export/docker-project-jar/$javadir/$javaname.${GET_RESULT} ] ;then rm -rf /export/docker-project-jar/$javadir/$javaname.${GET_RESULT} fi #将编译后的war包拷贝到/export/docker-project-jar/$javadir目录下 mv $javaname_path /export/docker-project-jar/$javadir/$javaname.${GET_RESULT} #在/export/Docker-Template/jar 目录下有一个dockerfile模版,根据war包的名字替换成新的dockerfile #替换Dockerfile中的war 名字 sed "s/jenkins/$javaname/g" /export/Docker-Template/jar/Dockerfile >/export/docker-project-jar/$javadir/Dockerfile /bin/cp /export/Docker-Template/jar/start.sh /export/docker-project-jar/$javadir/ elif [ "$1" == "js" ] ;then #将编译后的war包拷贝到/export/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-empi #创建/export/docker-project-jar mkdir -p /export/docker-project-js/$javadir #删除已经 #if [ -f /export/docker-project-nodejs/$javadir/ ] ; then rm -rf /export/docker-project-js/$javadir/* #fi mv $WORKSPACE/dist/empi /export/docker-project-js/$javadir/ mv $WORKSPACE/config/nginx.conf /export/docker-project-js/$javadir/ #导入Dockerfile模板 cp /export/Docker-Template/js/Dockerfile /export/docker-project-js/$javadir/ sed -i "s/172.16.201.50:9001/empirest/g" /export/docker-project-js/$javadir/nginx.conf sed -i "s/172.16.201.50:7001/webadmin/g" /export/docker-project-js/$javadir/nginx.conf sed -i "s/172.16.201.50:6001/authserver/g" /export/docker-project-js/$javadir/nginx.conf fi delete_old_images image_push replace_k8s_yaml create_update_k8s add_ingress } #镜像仓库地址 registry="192.168.200.10" #取出项目目录subject-bj-prod-serverapi-gz javadir=`echo $WORKSPACE | awk -F'/' '{print $5}'` #判断结尾名称格式war|jar|gz GET_RESULT=`echo $javadir | awk -F'-' '{print $NF}'` #java的路径 /root/.jenkins/jobs/subject-bj-test-imserver/workspace/target/11.war ## 注意:如果存在分支情况,javaname_path 路径需要具体查看jenkins再做修改 #有分支 #javaname_path=`find $WORKSPACE/xxx/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}'` #dns域名 #dnsname="xkim" dnsname=$environment$javaname case "$GET_RESULT" in war|jar|gz|js) replace_content $GET_RESULT ;; *) echo "未知" ;; esac