Jenkins+Kubernetes+Docker完成微服务持续集成
目录1、创建一个流水线的新项目2、创建NFS共享目录3、项目里配置参数4、配置harbor凭证5、编写构建Pipeline6、开始构建项目1、创建一个流水线的新项目2、创建NFS共享目录让所有Jenkins-Slave构建指向NFS的Maven的共享仓库目录[root@k8s-master ~]# vim /etc/exports/opt/nfs/jenkins *(rw,no_root_squa
·
目录
1、创建一个流水线的新项目
2、创建NFS共享目录
让所有Jenkins-Slave构建指向NFS的Maven的共享仓库目录
[root@k8s-master ~]# vim /etc/exports
/opt/nfs/jenkins *(rw,no_root_squash)
/opt/nfs/maven *(rw,no_root_squash)
[root@k8s-master ~]# systemctl restart nfs
3、项目里配置参数
4、配置harbor凭证
5、编写构建Pipeline
粘贴以下模板,参数自行修改 !!!
def git_address = "http://192.168.195.180:82/kgc_group/tensquare_back.git"
def git_auth = "f06c4b06-ac26-4683-ad00-2d3599c827ee"
//构建版本的名称
def tag = "latest"
//Harbor私服地址
def harbor_url = "192.168.195.183:85"
//Harbor的项目名称
def harbor_project_name = "tensquare"
//Harbor的凭证
def harbor_auth = "46c4a44a-65d7-419f-a34b-9dd0a25710c9"
podTemplate(label: 'jenkins-slave', cloud: 'kubernetes', containers: [
containerTemplate(
name: 'jnlp',
image: "192.168.195.183:85/library/jenkins-slave-maven:latest"
),
containerTemplate(
name: 'docker',
image: "docker:stable",
ttyEnabled: true,
command: 'cat'
),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
nfsVolume(mountPath: '/usr/local/apache-maven/repo', serverAddress: '192.168.195.200' , serverPath: '/opt/nfs/maven'),
],
)
{
node("jenkins-slave"){
// 第一步
stage('pull code'){
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
// 第二步
stage('make public sub project'){
//编译并安装公共工程
sh "mvn -f tensquare_common clean install"
}
// 第三步
stage('make image'){
//把选择的项目信息转为数组
def selectedProjects = "${project_name}".split(',')
for(int i=0;i<selectedProjects.size();i++){
//取出每个项目的名称和端口
def currentProject = selectedProjects[i];
//项目名称
def currentProjectName = currentProject.split('@')[0]
//项目启动端口
def currentProjectPort = currentProject.split('@')[1]
//定义镜像名称
def imageName = "${currentProjectName}:${tag}"
//编译,构建本地镜像
sh "mvn -f ${currentProjectName} clean package dockerfile:build"
container('docker') {
//给镜像打标签
sh "docker tag ${imageName} ${harbor_url}/${harbor_project_name}/${imageName}"
//登录Harbor,并上传镜像
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')])
{
//登录
sh "docker login -u ${username} -p ${password} ${harbor_url}"
//上传镜像
sh "docker push ${harbor_url}/${harbor_project_name}/${imageName}"
}
//删除本地镜像
sh "docker rmi -f ${imageName}"
sh "docker rmi -f ${harbor_url}/${harbor_project_name}/${imageName}"
}
}
}
}
}
在构建中需要创建仓库目录,是因为NFS共享目录权限不足,需更改权限
[root@k8s-master ~]# mkdir -p /opt/nfs/maven
[root@k8s-master ~]# chmod -R 777 /opt/nfs/maven/
还需要在从节点设置docker命令权限
[root@k8s-node1 ~]# chmod 777 /var/run/docker.sock
[root@k8s-node2 ~]# chmod 777 /var/run/docker.sock
6、开始构建项目
Eureka服务构建成功!!!
更多推荐
已为社区贡献2条内容
所有评论(0)