gitlab-jenkins-shell-helm-chart-k8s自动化部署微服务

1.准备好编译环境的容器,所有容器的镜像制作在gemdale-dockerfile这个代码库里面,也可以直接拉取官方镜像部署

bash 复制代码
docker run --name node1420-patternx -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t node:14.20.0

docker run --name node1615-patternx  -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t node:16.15.1

docker run --name nodejs1417-patternx -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t jelastic/nodejs:14.17.5-forever

FROM node:16.20.1-bullseye
docker run --name nodejs16 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/nodejs16:v1.1.0.0

FROM golang:1.19.6-buster
docker run --name golang -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ -v /data/etc/cron.d/:/etc/cron.d/ -v /data/etc/supervisord.d/:/etc/supervisord.d/ -v /data/var/log/supervisor/:/var/log/supervisor/ --net host --restart always -d -t golang:1.19.6-bullseye

FROM node:14.20.0-buster
docker run --name nodejs14 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/nodejs14:v1.2.0.0

FROM python:3.9.17-bullseye
docker run --name python39-testing -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/python39-testing:v1.0.0.1

FROM maven:3.8.6-openjdk-11
docker run --name maven3-openjdk11 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/maven3-openjdk11:v1.2.0.0

FROM python:3.10.13-bullseye
docker run --name ansible --privileged -v /data/root/.ssh/:/root/.ssh/ -v /data/root/.tiup/:/root/.tiup/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ -v /data/ansible/:/data/ -v /data/etc/ansible/:/etc/ansible/ --restart always -d -t linice/ansible:v1.2.0.0

FROM maven:3.8.6-openjdk-8
docker run --name maven3-openjdk8 -v /data/var/www/:/data/var/www/ -v /var/jenkins_home/:/var/jenkins_home/ -v /mnt/hgfs/:/mnt/hgfs/ --net host --restart always -d -t linice/maven3-openjdk8:v1.1.0.0

2.发布jenkins的shell执行脚本代码在仓库gemdale_jenkins

gemdale_jenkins

This project is parameterized

Choice Parameter 参数的名称这些参数作为环境变量公开用于构建

这个是定义项目脚本里面的变量

dr 默认是deploy部署脚本 可以选rollback回滚脚本

project ienjoys 项目变量

env test prod 环境变量

部署到服务器

实际部署的服务service是服务名称变量,gemdale_jenkins是实际服务名

代码分支

选择变量false,true:是否更新helm chart release. 默认为false, 即不更新.第一次执行要选true

回滚设置



bash /data/var/www/service/gemdale_jenkins/sourcecode/sourcecode.sh

bash 复制代码
#!/bin/bash
set -e #有报错就直接退出
######## define variables -- start ########
# current shell script file's directory
workdir=$(dirname $(readlink -f $0)) #当前文件所在的目录
######## define variables -- end ########
# arg
dr=${dr:-deploy}  #-deploy等于jenkins设置的默认变量deploy
# comm_var
shell=$workdir/default/sourcecode-$dr.sh  #设置一个变量
# exec
#执行变量脚本/data/var/www/service/gemdale_jenkins/default/sourcecode-$dr.sh
source $shell     

source sourcecode-deploy.sh

bash 复制代码
#!/bin/bash

echo 'renew my_tgt:'
rm -rf my_tgt
mkdir my_tgt

######## define variables -- start ########
keep=20
######## define variables -- end ########

# comm
#可无权限执行脚本/data/var/www/service/gemdale_jenkins/default/sourcecode-comm.sh
source $workdir/default/sourcecode-comm.sh

# build_env
service_ws=$WORKSPACE
service_tgt_path=$service_ws

echo 'compress service:'
cd $service_tgt_path
tgz_file=$service-${BUILD_ID}.tar.gz
tgz_file_path=$WORKSPACE/my_tgt/$tgz_file
tar --exclude=my_tgt -czf $tgz_file_path .

# run
dest_tgz_file_path=$dest_d_dir/$service/$tgz_file
dest_d_path=$dest_d_dir/$service/$service-$BUILD_ID

source $workdir/default/sourcecode-run.sh

echo "on destination server: delete old tgz files and service directories, to keep last $keep services. -- start"
del_files=''
for ((i=$keep; i < $(($keep + 100)); i++)); do
  if (($BUILD_ID - $i <= 0)); then
    break;
  fi
  del_files="$del_files $service-$(($BUILD_ID - $i)) $service-$(($BUILD_ID - $i)).tar.gz"
done

docker exec ansible ansible $rs -m shell -a "cd $dest_d_dir/$service && rm -rf $del_files"
echo "on destination server: delete old tgz files and service directories, to keep last $keep services. -- end"

source sourcecode-comm.sh

bash 复制代码
#!/bin/bash

# comm_var
dest_dir=/data/var/www/service
dest_d_dir=/data/var/www/service.d
dest_path=$dest_dir/$service

# env_service_var
case $env in
  test | uat )
    case $service in
      patternx-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
      carpark-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdimg-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdnb-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdr-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gdyk-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215}
        ;;
      gemdale_charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
      gemdale_jenkins )
        rs=${rs:-10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
      gemdale-playbook )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.36.21.224}
        echo ${rs}
        ;;
      pmc-charts )
        rs=${rs:-10.0.103.211,10.0.103.213,10.0.103.215,10.0.103.245,10.0.103.247,10.0.103.249}
        ;;
    esac
    ;;
  prod )
    case $service in
      patternx-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      ats-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      carpark-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdimg-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdnb-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdr-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gdyk-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216}
        ;;
      gemdale_charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216,10.0.103.246,10.0.103.250}
        ;;
      gemdale_jenkins )
        rs=${rs:-10.0.103.246,10.0.103.250}
        ;;
      gemdale-playbook )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216,10.36.14.174}
        ;;
      pmc-charts )
        rs=${rs:-10.0.103.212,10.0.103.214,10.0.103.216,10.0.103.246,10.0.103.250}
        ;;
    esac
    ;;
esac

source sourcecode-run.sh

bash 复制代码
#!/bin/bash

echo 'on destination server, create directories:'
docker exec ansible ansible $rs -m file -a "path=$dest_dir state=directory"
docker exec ansible ansible $rs -m file -a "path=$dest_d_path state=directory"

echo 'scp tgz file to destination server:'
docker exec ansible ansible $rs -m copy -a "src=$tgz_file_path dest=$dest_d_dir/$service/"

echo 'on destination server, decompress tgz file:'
docker exec ansible ansible $rs -m shell -a "tar -xzf $dest_tgz_file_path -C $dest_d_path/"

echo 'on destination server, link to dest_path:'
docker exec ansible ansible $rs -m shell -a "ln -snf $dest_d_path $dest_path"
相关推荐
萨格拉斯救世主32 分钟前
jenkins使用slave节点进行node打包报错问题处理
运维·jenkins
昌sit!7 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
A ?Charis9 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
北漂IT民工_程序员_ZG10 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes
秋说17 小时前
开源代码管理平台Gitlab如何本地化部署并实现公网环境远程访问私有仓库
gitlab·源代码管理
2301_8061313617 小时前
Kubernetes的基本构建块和最小可调度单元pod-0
云原生·容器·kubernetes
SilentCodeY18 小时前
containerd配置私有仓库registry
容器·kubernetes·containerd·镜像·crictl
极小狐20 小时前
驭码上新,AI Code Review、基于代码库的知识问答,让研发起飞
gitlab·devsecops·devops·极狐gitlab·安全合规
Narutolxy21 小时前
精准优化Elasticsearch:磁盘空间管理与性能提升技巧20241106
大数据·elasticsearch·jenkins
binqian1 天前
【k8s】ClusterIP能http访问,但是不能ping 的原因
http·容器·kubernetes