K8S发版脚本
2021-04-28 本文已影响0人
鸡蛋挂面
# tree
.
├── generateyaml.sh
├── Jenkinsfiletemplate
├── kubernetes
│ ├── Dockerfile_template
│ ├── start
│ ├── template-deployment-addnew.yaml
│ └── template-ingress.yaml
└── shell
└── template.sh
3 directories, 6 files
template.sh
#!/bin/bash
#定义变量
shell_version=shell_dev
config_version=config_dev
Dockerfile_version=Dockerfiledev
JOB_NAME=
service_name=
pod_name=
# ingresshost=
# secret_name=
# 定义数组
port_state=(
)
service_state=(
)
# 更新时间:2021年5月18日18点19分
# 优化:将容器启动命令放置yaml文件
# 优化:修改yaml文件的镜像
# 最新更新:2021年5月19日18点22分
# 修改:添加了config_version
# ==========================脚本开始===================================
# ====================================================================
#清理环境
mkdir -p ${shell_version}
# 备份
cp -a ./kubernetes/Dockerfile_template ./kubernetes/${Dockerfile_version}
cp -a Jenkinsfiletemplate Jenkinsfile
sed -i s/config_version/${config_version}/g ./kubernetes/${Dockerfile_version}
# # ==================================ingress=================================================
# for i in ${service_state[*]}
# do
# ##### ==================================ingress.yaml====================================
# cp ./kubernetes/template-ingress.yaml ./kubernetes/${JOB_NAME}_$i-ingress.yaml
# sed -i s/pod-name/${pod_name}-$i/g ./kubernetes/${JOB_NAME}_$i-ingress.yaml
# sed -i s/ingresshost/${ingresshost}/g ./kubernetes/${JOB_NAME}_$i-ingress.yaml
# sed -i s/secret-name/${secret_name}/g ./kubernetes/${JOB_NAME}_$i-ingress.yaml
# done
# for j in ${!service_state[*]}
# do
# # ingress端口替换
# sed -i s/PORTnumber/${port_state[j]}/g ./kubernetes/${JOB_NAME}_${service_state[j]}-ingress.yaml
# done
# ##### ==================================================================================
# ##### ==================================ingress.sh======================================
# for i in ${service_state[*]}
# do
# echo "setsid script -c 'sudo kubectl apply -f ./kubernetes/${JOB_NAME}_$i-ingress.yaml --record'">>./${shell_version}/ingress.sh
# done
# # ==============================================================================================
# 批量生成不同服务的yaml文件
for i in ${service_state[*]}
do
cp ./kubernetes/template-deployment-addnew.yaml ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml
sed -i s/pod-name/${pod_name}-$i/g ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml
sed -i s/images_name/${JOB_NAME}/g ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml
sed -i s/service_state/$i/g ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml
sed -i s/service_name/${service_name}/g ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml
sed -i s/config_version/${config_version}/g ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml
done
for j in ${!service_state[*]}
do
# deployment端口替换
sed -i s/PORTnumber/${port_state[j]}/g ./kubernetes/${JOB_NAME}_${service_state[j]}-deployment-addnew.yaml
done
# ====================================================================
# ====================buildrsync.sh===================================
cat > ./${shell_version}/build.sh << EOF
#!/bin/bash
export GO111MODULE=on
export GOROOT=/usr/local/go16
export GOPROXY=https://goproxy.cn,direct
git config --global url."ssh://git@gitlab.heywoods.cn/".insteadOf "https://gitlab.heywoods.cn/"
export GOPRIVATE=gitlab.heywoods.cn
export GONOPROXY=gitlab.heywoods.cn
export GONOSUMDB=gitlab.heywoods.cn
/usr/local/go16/bin/go build -mod=mod -o ./${service_name} /opt/applications/go/src/${JOB_NAME}/${service_name}.go
if [ $? -ne 0 ]; then
exit 1
fi
cp ${service_name} kubernetes/start/${service_name}
EOF
# ====================================================================
# ====================buildrsync.sh===================================
cat > ./${shell_version}/buildrsync.sh << EOF
/usr/bin/rsync -avz ./* /opt/applications/go/src/${JOB_NAME}/
/usr/bin/rsync -avz --exclude={kubernetes,Jenkinsfile_dev,shell_version,${shell_version}} ./* ./kubernetes/start/
EOF
# ====================================================================
# =======================buildocker.sh================================
cat > ./${shell_version}/buildocker.sh << EOF
build_tag=\$1
docker build -t heywoods/${JOB_NAME}:\${build_tag} -f ./kubernetes/${Dockerfile_version} .
docker tag heywoods/${JOB_NAME}:\${build_tag} harboral.heywoods.cn/app/${JOB_NAME}:\${build_tag}
EOF
# ====================================================================
# ========================pushdocker.sh===============================
cat > ./${shell_version}/pushdocker.sh << EOF
harborUser=\$1
harborPassword=\$2
build_tag=\$3
docker login -u \${harborUser} -p \${harborPassword} harboral.heywoods.cn
docker push harboral.heywoods.cn/app/${JOB_NAME}:\${build_tag}
EOF
# ====================================================================
# ===================cleandocker.sh===================================
cat > ./${shell_version}/cleandocker.sh << EOF
build_tag=\$1
docker rmi heywoods/${JOB_NAME}:\${build_tag}
docker rmi harboral.heywoods.cn/app/${JOB_NAME}:\${build_tag}
EOF
# ==================deployment.sh=====================================
for i in ${service_state[*]}
do
echo setsid script -c \"sudo kubectl apply -f ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml --record\">>./${shell_version}/deployment.sh
done
# ===================Jenkinsfile======================================
sed -i s/shell_version/${shell_version}/g Jenkinsfile
for i in ${service_state[*]}
do
echo sh \"sed -i "'"s/\<BUILD_TAG\>/\$\{build_tag\}/"'" ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml\">>Jenkinsfile
echo sh \"sed -i "'"s/\<BRANCH_NAME\>/\$\{env.BRANCH_NAME\}/"'" ./kubernetes/${JOB_NAME}_$i-deployment-addnew.yaml\">>Jenkinsfile
done
cat >>Jenkinsfile <<EOF
sh "./shell/deployment.sh"
sh "./shell/ingress.sh"
}
}
EOF
# ====================================================================
# ==========================脚本结束===================================
Dockerfile_template
FROM centos:7.5.1804
MAINTAINER heywoods
ENV PROJECT_DIR=/usr/src
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN yum install -y glibc-devel gcc c++ net-tools psmisc
COPY ./kubernetes/start /usr/src/start
# 2021年5月18日18点48分:删除了复制二进制包的步骤
COPY config_version.yml /usr/src/start/config_version.yml
WORKDIR $PROJECT_DIR/start
template-deployment-addnew.yaml
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: pod-name-deployment
namespace: heywoods-app
spec:
strategy:
rollingUpdate:
maxSurge: 100%
maxUnavailable: 0%
replicas: 1
template:
metadata:
labels:
app: pod-name
spec:
containers:
- name: heywoods-golang
image: "harboral.heywoods.cn/app/images_name:<BUILD_TAG>"
command: ["/bin/sh"]
args: ["-c","./service_name service_state --config config_version.yml"]
imagePullPolicy: IfNotPresent
env:
- name: branch
value: <BRANCH_NAME>
- name: JAEGER_AGENT_HOST
valueFrom:
fieldRef:
fieldPath: status.hostIP
resources:
limits:
cpu: 400m
memory: 512Mi
requests:
cpu: 200m
memory: 200Mi
ports:
- containerPort: PORTnumber
imagePullSecrets:
- name: harbor-secret
---
kind: Service
apiVersion: v1
metadata:
name: pod-name-service
namespace: heywoods-app
labels:
app: pod-name
spec:
ports:
- name: http
protocol: TCP
port: PORTnumber
targetPort: PORTnumber
selector:
app: pod-name
# Minikube needs NodePort to work.
type: ClusterIP
#type: NodePort
sessionAffinity: None
template-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: pod-name-ingress
namespace: heywoods-app
spec:
tls:
- hosts:
- ingresshost
secretName: secret-name
rules:
- host: ingresshost
http:
paths:
- path: /
backend:
serviceName: pod-name-service
servicePort: PORTnumber
generateyaml.sh
#!/bin/bash
# 清理环境
find . -type f '!' -name 'Dockerfile_template' '!' -name 'template*' '!' -name 'generateyaml.sh' '!' -name 'Jenkinsfiletemplate' |xargs rm -rf
# 字符转化
dos2unix shell/template.sh
# 执行yaml文件自动生成脚本
sh -x shell/template.sh
Jenkinsfiletemplate
#!/usr/bin/env groovy
node {
stage('获取源码-master') {
echo "1.Prepare Stage"
checkout([$class: 'GitSCM',
branches: [[name: "${params.tag}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
gitTool: 'Default',
submoduleCfg: [],
userRemoteConfigs: [[url: '']]
])
build_tag = "${params.tag}"
}
stage('编译') {
echo "2.Build Stage"
sh "chmod u+x ./shell_version/*.sh"
sh "./shell_version/buildrsync.sh"
sh "./shell_version/build.sh"
}
stage('构建镜像') {
echo "3.Build Docker Image Stage"
sh "./shell_version/buildocker.sh ${build_tag}"
sh "sleep 2"
}
stage('上传镜像') {
echo "4.Push Docker Image Stage"
withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPassword', usernameVariable: 'harborUser')]) {
sh "./shell_version/pushdocker.sh ${harborUser} ${harborPassword} ${build_tag}"
}
}
stage('清理本地镜像') {
echo "4.Clean Local Docker Image"
sh "./shell_version/cleandocker.sh ${build_tag}"
}
stage('K8S构建项目') {
echo "5. Deploy Stage"