CICD: 简单例子
- TAGS: cicd
CICD-k8s
CI
- EKS: 1.30
- ECR: 私有镜像仓库
# tree business-server . ├── dockerfile │ ├── Dockerfile │ └── build.sh └── kubernetes ├── deployment.yaml └── service.yaml
Dockerfile
cat <<\EOF> dockerfile/Dockerfile FROM node:20.11-alpine LABEL maintainer ci@ ENV SERVICE_NAME=slots-game \ SERVICE_PATH=/app RUN ln -sf /usr/share/zoneinfo/Africa/Lagos /etc/localtime \ && echo 'Africa/Lagos' > /etc/timezone RUN apk add tzdata COPY slots_server/server /app/server COPY slots_server/node_modules /app/node_modules COPY slots_server/util /app/util COPY slots_server/CClass /app/CClass WORKDIR ${SERVICE_PATH}/server EXPOSE 13000 CMD [ "node","app" ] EOF
打镜像脚本
cat <<\EOF> dockerfile/build.sh #!/bin/bash # CI # Loading======================== set -eu tmpfile=$(mktemp -u ci.XXXX) tmp_path=/tmp/.${tmpfile} [ ! -e $tmp_path ] && mkdir -p $tmp_path cp -a * $tmp_path echo DIR: $tmp_path cd $tmp_path Name='slots-game-server' ECR_URL='87654321.dkr.ecr.af-south-1.amazonaws.com' TAG=`date +%Y%m%d%H%M` image_url=${ECR_URL}/slots/${Name}:${TAG} if [ ! -d "my-server" ] ; then git clone --depth=1 [email protected]:sa-slots/backend/my-server.git fi # Compiling======================== cd my-server/slots_server npm install cd $tmp_path mv my-server/slots_server ./slots_server # Build image======================== if ! aws ecr describe-repositories --repository-names slots/$Name &>/dev/null; then echo Create repository aws ecr create-repository --repository-name slots/$Name fi aws ecr get-login-password --region af-south-1 | docker login --username AWS --password-stdin $ECR_URL docker buildx build -t $image_url --platform=linux/arm64,linux/amd64 --push . # Clean up======================== cd; rm -fr $tmp_path echo -e "\nIMAGE: $image_url" EOF
CICD
kubernetes 中切换镜像脚本、服务状态脚本
cat <<\EOF> /usr/bin/k8s_set_tag_af-south-1 job_name=$1 #build_num=$2 repo_url=87654321.dkr.ecr.af-south-1.amazonaws.com/slots/ #img_url=$repo_url$job_name img_url=$2 kubectl --kubeconfig /root/.kube/config_af-south-1 -n slots-prod set image deploy ${job_name} ${job_name}=$img_url EOF cat <<\EOF> /usr/bin/k8s_log_af-south-1 job_name=$1 config="--kubeconfig /root/.kube/config_af-south-1 -n slots-prod" pod_name=$(kubectl ${config} get pod --sort-by=.metadata.creationTimestamp -l app=${job_name}|tail -1|awk '{print $1}') # 检测pod启动后再获取log while true; do kubectl ${config} -n slots-prod get pod $pod_name |grep -q Running if [ $? -eq 0 ];then echo $pod_name "is running" break fi echo $pod_name "waiting to running" sleep 1 done kubectl ${config} logs -f $pod_name & #jenkins无需自己解决子进程问题 #pid=$! while true; do restarts=$(kubectl $config get pod $pod_name|grep -v NAME|awk '{print $4}') if [ $restarts -gt 0 ];then #kill $pid exit 1 fi kubectl $config rollout status deployment $job_name --watch=false|grep -q successfully if [ $? -eq 0 ];then #kill $pid exit 0 fi sleep 1 done EOF
部署脚本
cat <<\EOF> ~/bin/rc-deploy #!/bin/bash # set -eu server_list=( slots-web slots-game-server ) if [ $# -eq 0 ];then echo "Usage: `basename $0` one of [ $(echo -e "${server_list[*]}" |tr ' ' '\n') ]" exit fi out_arg=0 if [ $# -ge 2 ];then out_arg=1 fi JOB_BASE_NAME=$1 flag=0 for i in $(seq 0 $[${#server_list[*]}-1]); do if [ "${server_list[$i]}" == "${JOB_BASE_NAME}" ];then flag=1 break fi done [ $flag -eq 0 ] && { echo "Please use: $(echo -e "${server_list[*]}" |tr ' ' '\n')" ; exit 1; } || : base_dir=/root/.jasper/ build_dir=${base_dir}/eks-yaml/slots/slots-prod/${JOB_BASE_NAME}/dockerfile echo "Build Image" cd $build_dir [ ${out_arg} -eq 0 ] && { img_url=$(bash build.sh 2>/dev/null |awk '/IMAGE: /{print $2}') } || { img_url=$(bash build.sh |awk '/IMAGE: /{print $2}') } echo "IMAGE: $img_url" echo 'Deploy Trigger' k8s_set_tag_af-south-1 ${JOB_BASE_NAME} "${img_url}" #echo "Deploy Status" #k8s_log_af-south-1 ${JOB_BASE_NAME} EOF