采用rancher2+kubernetes+skywalking部署springcloud项目(二[helm版本])

距离上一篇记录
采用rancher2+kubernetes+skywalking部署springcloud项目(一[k8s yaml版本])发布已经2个多月了,之前写那篇文章时专门在标题右边写了一个序号,是因我想通过spring-boot-cloud项目来对k8s的发布进行一个整体的学习(尽管我同学告诉我对于项目发布的东西在他们公司都是专门的运维或devops工程师去处理的,作为开发小弟的我们不需要去掌握,但我个人感觉对这些内容还是挺有意思的,我决定还是坚持自己原本的想法先掌握了再说

最近由于我的个人原因(主要是太懒了)这些天来没有将文章写出来,看着又到月底了啥动静也没有感觉还是有点愧疚的,今天趁着周二我就来继续记录一下吧!

本系列文章目录(计划中)

  • 基础k8s yaml脚本发布
  • helm+shell脚本优化大量冗余配置发布
  • jenkins用户审核的流水化方式部署
  • service mesh(istio)服务网格化发布

前言

通过一篇博文采用rancher2+kubernetes+skywalking部署springcloud项目(一[k8s yaml版本])我们可以发现通过yaml文件去发布springcloud的项目还是挺复杂的,因为有太多重复而冗余的配置。
我们意识到了通过原生的yaml文件发布k8s项目具有一定的复杂性。

除了我们意识到了这一点外,谷歌的大佬们同样意识到了这一点。为了解决这一问题,helm便横空出世。

正如helm官网所说的那样:

Helm is the best way to find, share, and use software built for Kubernetes.
https://helm.sh/

helm下载与安装

对于新版(v3)的helm安装相对与以前版本安装过程已经简化了许多,直接安装下载helm客户端就行了,直接参考官方文档即可快速安成helm的安装。
helm的官网为:https://helm.sh/

helm下载

参考:https://helm.sh/docs/intro/quickstart/

在官网中找到下载链接,直接进行下载:https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz

当然,如果电脑网络比较可以的话,直接用脚本在线下载也可以

$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh

helm安装

参考: https://helm.sh/docs/intro/install/

tar -zxvf helm-v3.0.0-linux-amd64.tar.gz

将helm压缩包解压后将它copy到/usr/local/bin目录下

mv linux-amd64/helm /usr/local/bin/helm

测试一下,随便输入一个helm命令,查看下helm能否正常运行,如helm list

[root@k8s-node1 ~]# helm list
NAME	NAMESPACE	REVISION	UPDATED	STATUS	CHART	APP VERSION

生成通用helm的chart

正常情况下,一个服务就需要编写一个对应的chart。但是对于spring cloud这样的微服务其实大多数配置都是一样的,如果每个服务都单独编写一个chart的话对于管理的chart配置的话就有点不方便了。

综合考虑分析后,为了方便对chart管理,这里打算只写一个chart,让它作为springcloud的通用chart,每个服务部署时再通过通用chart来生成自己项目的chart,对于每个微服务不同的地方通过values.xml来传递不同的参数即可

生成chart模板

直接用idea下的newFile生成一个chart模板

idea生成chart

修改chart配置

官方文档:https://helm.sh/docs/topics/charts/

主要是修改deployment.yaml,修改后的可参考github上的文件

编写部署shell脚本

shell脚本中主要分为以下几个步骤:

  1. 拉取最新的项目代码
  2. 选择要发布的项目
  3. 将项目进行打包,生成jar包
  4. 将jar包打成docker镜像并推送到镜像仓库
  5. 通过通用chart生成对应项目的chart文件
  6. 执行helm命令,更新或安装relase

根据上面的思路,那么最终的脚本如下:

#!/usr/bin/env bash
################### 配置信息begin ###################
project_git_url=https://github.com/puhaiyang/spring-boot-cloud.git
#docker镜像仓库地址
image_domain=ccr.ccs.tencentyun.com/spring-boot-cloud
#项目名称
project_name=spring-boot-cloud
#所有项目的git根目录
git_root_path=~/git
#项目的根目录
project_root_path=~/git/$project_name
#通用chart目录
chart_root_path=$project_root_path/helms
#版本号,docker的tag
version=`date +%Y%m%d%H%M`
#部署的namespace
deploy_namespace="default"
#钉钉机器人的token
ding_robot_token="xxxxxxxxxxx"
################### 配置信息end ###################
########################初始化 begin###########################
#不存在项目目录的话,则先进行初始化
if [ ! -d $git_root_path ];then
    echo '不存项目目录,先进行初始化'
	mkdir $git_root_path
	cd $git_root_path
	git clone $project_git_url
fi
########################初始化 end###########################
source /etc/profile
#自定义分支名
if [ -z "$1" ]; then
    #默认分支名称
    branch=master
else
    branch=$1
fi

echo '----------------------------------------------'
echo ':-D hi,boy!welcome to automatic depoly sh! :-D'
echo '----------------------------------------------'

echo -e "branch:$branch, 系统服务列表:"
################################列目录 begin############################
pom_files=$(dirname $(find git/spring-boot-cloud/ -name pom.xml))
igrone_pro="spring-boot-cloud"
index=0
projects[0]="unknown"
for i in ${pom_files[@]};do
  pro_name=$(basename $i)
  if [ "$pro_name" = "$igrone_pro" ]; then
     continue
  fi
  ((index++))
  echo $index')'$pro_name
  projects[${#projects[*]}]="$pro_name"
done
echo -e "请输入模块对应的序号:"
read project_index
echo ${projects[$project_index]}
#要发布的项目名
pname=${projects[$project_index]}
################################列目录 end############################
echo "更新的模块:$pname"
#模块的目录
git=$project_root_path/$pname
#切换到项目的目录
cd $git
#获取到新的代码
git fetch --all
git reset --hard origin/$branch
#获取到最后一次提交的内容
git_last_commit=$(git show -s --format=%s)
#获取到最后一次提交者
git_last_commit_author=$(git show -s --format=%an)
#如果项目是maven聚合项目,需要在git的根目录进打包,-pl --projects <arg> 构建制定的模块,模块间用逗号分隔
#-am --also-make 同时构建所列模块的依赖模块;
cd $project_root_path
echo '下面开始打包(package)...'
mvn clean  package -Dmaven.test.skip=true -pl $pname -am
#mvn clean  package -Dmaven.test.skip=true
echo '好,包打好了!接下来准备打镜像!(docker image package)...'
cd $git
#docker镜像名
docker_image=$image_domain/$pname
#docker镜像名
image=$docker_image:$version
echo '现在开始打镜像...'
docker build -f  Dockerfile -t $image .
echo '打好了,把镜像推到docker镜像仓库里去(push...)'
docker push $image

echo '好!现在准备发布更新了(pre deploy...)'
echo '现在准备chart文件,通过通用chart文件来生成'
path_common_chart=$chart_root_path/common-modle-springbootcloud
echo $path_common_chart
#在项目的target目录下创建chart目录
mkdir chart
echo '复制到target下的chart目录下'
cp -r $path_common_chart chart
#重命名
mv chart/common-modle-springbootcloud chart/$pname
echo '替换chart'
sed -i "s/common-modle-springbootcloud/$pname/g" chart/$pname/Chart.yaml
echo '替换values'
sed -i "s/common-modle-springbootcloud/$pname/g" chart/$pname/values.yaml

#install or upgrade
###############判断是否已发布对应的项目 begin#####################
helm_ops='install'
helm list -A |grep $pname
if [ $? -ne 0 ];then
  echo "不存在"$pname"模块,将先进行install"
  helm_ops="install"
else
  echo "已存在"$pname"模块,将先进行upgrade"
  helm_ops="upgrade"
fi
###############判断是否已发布对应的项目 end#####################

echo '进行发布(deploy...)'
if [ -f "$chart_root_path/values/$pname/values.yaml" ];then
    echo '已经有配置文件了,采用配置文件进行发布'
	helm $helm_ops $pname chart/$pname -n $deploy_namespace -f $chart_root_path/values/$pname/values.yaml --set image.tag=$version --set image.repository=$docker_image
else
    echo '直接进行发布'
    helm $helm_ops $pname chart/$pname -n $deploy_namespace --set image.tag=$version --set image.repository=$docker_image
fi

Message="发布通知: \n模块:${pname} \ngit提交者:$git_last_commit_author \ngit提交内容:$git_last_commit \n------have a nice day------"

curl "https://oapi.dingtalk.com/robot/send?access_token=$ding_robot_token" \
           -H 'Content-Type: application/json' \
           -d "  {\"msgtype\": \"text\",
              \"text\": {\"content\": \"$Message\"}}"

要让上面的shell脚本能正常执行,需要确保对应的机器上已安装了git和maven

测试自动部署helm版脚本

将上面写好的shell放到安装好了helm的linux中,并添加执行权限执行一下
部署脚演示
搞定
部署脚本详见:https://github.com/puhaiyang/spring-boot-cloud/blob/master/helms/helm-install.sh

最后想说

一边实验着k8s部署,一边听到呼呼呼的声音。

再低头一看,原来是公司的电脑主机,吓得我赶紧把服务给删了。

cpu温度90多度了
k8s果然名不虚传,16G的主机也HOLD不住呀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章