Jenkins自动化部署阿里云K8s
Jenkins上执行脚本内容#!/bin/bashserviceName=$1code_dir=/var/lib/jenkins/workspace/Dice_$serviceNameexport GOROOT=/micro/goexport PATH=$PATH:$GOROOT/binexport GOPATH=/opt/code/goexport PATH=/usr/local/bin/:$P
·
Kubernetes连接
在Jenkins服务器上安装 kubectl 客户端
#用以下命令下载最新发行版
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
#安装 kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
#将 kubectl 安装到目录 ~/.local/bin
chmod +x kubectl
mkdir -p ~/.local/bin/kubectl
mv ./kubectl ~/.local/bin/kubectl
#执行测试,以保障你安装的版本是最新的
kubectl version --client
通过 kubectl 来连接阿里云的 Kubernetes 集群
mkdir ~/.kube/
cd ~/.kube/
#创建并编辑config,将阿里云上的集群凭证配置复制进去,保存退出便可以访问阿里云的k8S了
vim config
kubectl get node,pod,svc
Jenkins脚本
Jenkins上执行脚本内容
#!/bin/bash
serviceName=$1
code_dir=/var/lib/jenkins/workspace/Dice_$serviceName
export GOROOT=/micro/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/opt/code/go
export PATH=/usr/local/bin/:$PATH
docker login --username=zzt@10086 --password='zt11111' registry.cn-shenzhen.aliyuncs.com
cd /opt/crazydice/yaml
# 存储pod的yaml文件用于读取version作版本比较
kubectl get pods -n dice-test `kubectl get pods -n dice-test|grep ${serviceName,,}| awk '{print $1}'|grep -v NAME|head -n 1` -o yaml >${serviceName,,}.yaml
oldversion=`grep "shenzhen.aliyuncs.com/superant" ${serviceName,,}.yaml |head -n 1|awk -F '/' '{print $3}'|awk -F ':' '{print $2}'`
echo $oldversion
cd $code_dir/Services/$serviceName
version=`cat version`
echo $version
function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$version"; }
# 比较版本号,大于原来版本则发布新版本
if version_gt $version $oldversion
then
cd $code_dir/Services/$serviceName
go mod tidy&&go mod vendor
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags=jsoniter -o crazydice-${serviceName,,}
docker build -t crazydice-${serviceName,,} .
rm -f crazydice-${serviceName,,}
docker tag crazydice-${serviceName,,} registry.cn-shenzhen.aliyuncs.com/superant/crazydice-${serviceName,,}:$version
docker push registry.cn-shenzhen.aliyuncs.com/superant/crazydice-${serviceName,,}:$version
docker rmi registry.cn-shenzhen.aliyuncs.com/superant/crazydice-${serviceName,,}:$version
# 部署pod
kubectl set image deployment/${serviceName,,} *=registry.cn-shenzhen.aliyuncs.com/superant/crazydice-${serviceName,,}:$version -n dice-test
echo "seccuss"
else
echo "version is too low"
fi
Jenkins开机启动
# 查看Jenkins的进程
ps -ef|grep jenkins.war
# 自启动服务脚本
$ cat <<EOF | sudo tee /etc/systemd/system/jenkins.service
[Unit]
Description=jenkins
After=network.target
[Service]
WorkingDirectory=/usr/bin
ExecStart=/bin/java -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/share/jenkins/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8080
ExecReload=/bin/kill -HUP
Type=simple
KillMode=process
Restart=on-failure
RestartSec=10s
User=root
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable jenkins&& systemctl start jenkins
更多推荐
已为社区贡献5条内容
所有评论(0)