jenkins部署k8s项目-CICD
一环境准备
1 docker仓库,harbor我这里用的是线上的hub https://hub.docker.com/ 自己注册即可
2 准备git仓库,我这里用的是码云 https://gitee.com/huningfei/java.git
3 准备好k8s环境,一主两从,可以二进制安装,也可以kubeadmin安装
master:172.16.78.103 node1:172.16.78.102 node2:172.16.78.101
4 准备好jenkins环境,(我是利用jenkins.war包直接部署的,也可以用k8s部署jenkins
二 k8s部署
我这里用kubeadmin部署
部署详情查看 https://www.cnblogs.com/huningfei/p/12697310.html
三设置jenkins
1 安装Kubernetes 和git,pipeline 安装这三个插件,在jenkins服务器上面安装maven和docker环境
yum install maven
# install docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
systemctl enable docker && systemctl start docker
# 还需要注意一点,如果你的jenkins是用普通用户启动的,当构建的时候可能会提示权限问题, 要改一下/var/run/docker.sock 这个文件的权限
2 设置jenkins去连接k8s api
系统管理---系统设置-cloud
2.1 kubernetes 服务证书从哪里来?
如果是kubeadmin部署的k8s,就比较麻烦,进入到 /etc/kubernetes目录下面打开admin.conf配置文件,文件中有三个值 certificate-authority-data 、client-certificate-data 、 client-key-data 分别用这三部分生成证书,这里我把生成的证书都放到了tmp目录下面
echo LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRFNU1USXlOVEE0TVRFMU4xb1hEVEk1TVRJeU1qQTRNVEUxTjFvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBT0d6Ckl0RzFDMTM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nV2dNQk1HQTFVZEpRUU1NQW9HQ0NzR0FRVUYKQndNQ01BMEdDU3FHU0liM0RRRUJDd1VBQTRJQkFRQkpaWlh5SEdhM1BMb1Rtb0I4RmtMRDN4OHRBTk9oRW8vMQozY0lBZ1NsMFd0SFU3Lzh0Wlo5alBIa3djNWZCeE94Uk5lZk4wWVdoSUR4UFFmSkZwWlEyL0JxTmw1dnpRNGlICko4ZGJZblJxTmcyYWJWUHQvdEtERGtKanBNQ2U4LzlJbFFZY3M5L0gxVUtpaW5WOHFPUmwvKzBvTFFDdkRMRzcKYWdXc1pMb3M1MWVWM3Z3WnBzSTZvSFNSVlFuaTZLVXc3RnJpUWNtYS94WkVweitPWlBlUFBrZVUzQXR2OXQyRQpWcHhDNlZudkMvaWJZU2xEWENmU1lYeEZNZFh2bzQxamgvSk11QU1KRGZqRDJOODM4TFhVMVd3d1hqUHRQUWFICllsbEtENjhoRmoycTJVR0xOMUZqSDZSMEJxeDV2cTkrRUNBQW5GOVgwYU9KYlpxdnVmNDgKLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= | base64 -d > /tmp/client.crt
然后到tmp目录,在生成一个ca.crt证书和一个cert.pfx
openssl pkcs12 -export -out cert.pfx -inkey client.key -in client.crt -certfile ca.crt
生成一个pfx的证书,会提示你输入密码 111
然后将ca.crt里面的内容放到jenkisn服务证书key的地方即可,当然这还没完,还需要添加凭据。
2.2 如何添加凭据?
点击add,选择kind类型,然后上传你刚才生成的cert.pfx这个证书,并且输入刚才的密码
最终去测试jenkins能否连接成功,如图
3 补充两外两种jenkins连接k8s方式:
3.1 如果你的jenkins是直接部署在k8s之内的,就比较简单,不需要填写证书,直接这样,如图:
3.2 如果你是用的二进制安装的k8s,这个证书在安装过程中,会产生,我测试了用ca.pem和server.pem发现都可以连接,并且不需要添加凭据。
四 发布项目
上面的步骤,让我们jenkins可以正常连接k8s了,然后就可以发布项目了
4.1 发布脚本
具体内容如下:
def registry = "huningfei"
// 项目
def project = "welcome"
def app_name = "demo"
def image_name = "${registry}/${project}:${BUILD_NUMBER}"
def git_address = "https://gitee.com/huningfei/java.git"
// 认证
def secret_name = "registry-pull-secret"
//id,下面的三个id都可以在jenkins里面产生
def docker_registry_auth = "450fb8b4-62c9-43fe-8a69-ec30705e724e"
def git_auth = "b363af0f-a96c-465c-a14e-60f7127727eb"
def k8s_auth = "a60b4b7f-2aef-4622-94a5-6efef67c4ac9"
node(){
// 第一步
stage('拉取代码'){
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
// 第二步
stage('代码编译'){
sh "mvn clean package -Dmaven.test.skip=true"
}
// 第三步
stage('构建镜像'){
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
echo '
FROM lizhenliang/tomcat
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
' > Dockerfile
docker build -t ${image_name} .
docker login -u ${username} -p '${password}'
docker push ${image_name}
"""
}
}
// 第四步
stage('部署到K8S平台'){
sh """
sed -i 's#\$IMAGE_NAME#${image_name}#' deploy.yml
sed -i 's#\$SECRET_NAME#${secret_name}#' deploy.yml
"""
kubernetesDeploy configs: 'deploy.yml', kubeconfigId: "${k8s_auth}"
}
}
注意: 脚本里面的deploy.yml是部署k8s的文件,这个文件必须存到gitlab仓库里面就是要跟代码在同一级目录里面
4.2 脚本里面的三个id如何产生?
添加完成之后,去凭据里面查看即可
最后构建项目
出现下面即可成功
五 验证
然后到k8s-master上面查看是否生成新的pod容器
查看ingress
绑定host即可访问