Jenkins+harbor+gitlab+k8s 部署maven项目
概述安装harbor仓库Harbor核心组件解释Harbor部署安装docker和compose安装harbor测试推送镜像安装git或者gitlab使用git使用gitlab上传项目安装Jenkins安装jenkins所需环境安装jenkins配置jenkins流水线工作概述
可以查看我写的更详细的文章 Jenkins+Docker+SpringCloud持续集成
环境介绍
安装harbor仓库
Habor是由VMWare公司开源的容器镜像仓库。事实上,Habor是在Docker Registry上进行了相应的 企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:管理用户界面,基于角色的访 问控制 ,AD/LDAP集成以及审计日志等,足以满足基本企业需求。
Harbor核心组件解释
Harbor部署
安装docker和compose
安装需要的安装包yum install -y yum-utils设置镜像仓库我们用阿里云yum-config-manager \--add-repo \/docker-ce/linux/centos/docker-ce.repo开启存储库yum-config-manager --enable docker-ce-nightlyyum-config-manager --enable docker-ce-test安装dockeryum install docker-ce docker-ce-cli containerd.iosystemctl enable docker --now下载安装composecurl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m) > /usr/local/bin/docker-compose 授权chmod +x /usr/local/bin/docker-compose 查看版本docker-compose version
安装harbor
使用的 harbor-offline-installer-v1.9.1
[root@harbor ~]# tar -xf harbor-offline-installer-v1.9.1.tar [root@harbor ~]# lsharbor harbor-offline-installer-v1.9.1.tar[root@harbor ~]# cd harbor[root@harbor harbor]# lsharbor.v1.9.1.tar.gz harbor.yml install.sh LICENSE prepare修改harbor.yml 文件里面的hostname 改成本机地址[root@harbor harbor]# vim harbor.yml hostname: 192.168.188.91配置http镜像仓库可信任vim /etc/docker/daemon.json添加{"insecure-registries": ["192.168.188.91"]}systemctl restart docker拉取镜像./prepare安装./install.sh当完成安装后,即可通过本机地址进行浏览器的访问,账户名为admin,密码默认为Harbor12345
测试推送镜像
换一台安装过docker的服务器配置http镜像仓库可信任vim /etc/docker/daemon.json添加{"insecure-registries": ["192.168.188.91"]}登录仓库docker login 192.168.188.91 -uadmin -pHarbor12345[root@gitlab ~]# docker login 192.168.188.91 -uadmin -pHarbor12345WARNING! Using --password via the CLI is insecure. Use --password-stdin.WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. See/engine/reference/commandline/login/#credentials-storeLogin Succeeded现在下载一个镜像nginxdocker pull nginx给镜像打tagdocker tag nginx:latest 192.168.188.91/maomao/nginx上传镜像docker push 192.168.188.91/maomao/nginx再通过浏览器查看上传的镜像
安装git或者gitlab
使用git
Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。安装git首先需要拥有ssh软件,但是该程序linux是自带拥有的,所以可以直接进行下载yum install -y git查看版本git version创建用户,用户名字随意,但是尽量还是使用和git相关的名字[root@gitlab ~]# useradd gitme[root@gitlab ~]# passwd gitmeChanging password for user gitme.New password: BAD PASSWORD: The password is shorter than 8 charactersRetype new password: passwd: all authentication tokens updated successfully.切换到gitme用户su - gitme在家目录创建mkdir demo.gitcd demo.gitgit --bare init
使用gitlab
GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭 建起来的web服务。安装方法是参考GitLab在GitHub上的Wiki页面。
国外站点下载比较慢,可以在国内镜像去下载,比如清华镜像
Centos7 gitlab 12.9.5社区版
下载完成之后将包上传至git服务器下载依赖yum install -y policycoreutils-python openssh-server通过rpm安装rpm -ivh gitlab-ee-12.9.5-ee.0.el6.x86_64.rpm
安装成功
修改配置文件vim /etc/gitlab/gitlab.rb修改地址external_url 'http://192.168.188.95'修改时区gitlab_rails['time_zone'] = 'Asia/Shanghai'将注释取消gitlab_rails['manage_backup_path'] = truegitlab_rails['backup_path'] = "/var/opt/gitlab/backups"gitlab_rails['backup_archive_permissions'] = 0644备份保持的时间gitlab_rails['backup_keep_time'] = 604800加载配置gitlab-ctl reconfigure重启gitlab-ctl restart安装查看系统负载工具,这个工具必 须安装阿里云epplo源yum -y install htop 浏览器登录gitlab仓库
创建账号
修改密码之后 登录
账号是root
密码是你修改的
创建授权一个账号给开发使用
给新用户设置密码
再创建一个jenkins账号 给jenkins用来拉取代码
上传项目
gitlab创建maven项目
创建项目
登录gitlab,创建一个项目springbootdemo
添加成员
添加2个成员,一个是jenkins用户,用来拉取代码的。一个是maomao,用来提交代码的。
将gitlab仓库来去到开发本地
yum install -y gitgit clone http://192.168.188.95/maomao/springbootdemo.git[root@gitlab ~]# git clone http://192.168.188.95/maomao/springbootdemo.gitCloning into 'springbootdemo'...Username for 'http://192.168.188.95': maomao Password for 'http://maomao@192.168.188.95': warning: You appear to have cloned an empty repository.当前目录就会有一个仓库目录springbootdemo [root@gitlab ~]# lsgitlab-ce-12.9.5-ce.0.el7.x86_64.rpm springbootdemo SpringBootDemo-master SpringBootDemo-master.zip然后将项目目录移动到仓库目录[root@gitlab ~]# mv SpringBootDemo-master springbootdemo/[root@gitlab ~]# cd springbootdemo/[root@gitlab springbootdemo]# lsSpringBootDemo-mastergit add .git commit -m 'v1'如果这里出现*** Please tell me who you are.则需要验证用户git config user.name "maomao"git config user.email "123@"我们重来一次[root@gitlab springbootdemo]# git add .[root@gitlab springbootdemo]# git commit -m 'v1'[master (root-commit) 608f5ef] v114 files changed, 656 insertions(+)create mode 100644 SpringBootDemo-master/.gitignorecreate mode 100644 SpringBootDemo-master/.mvn/wrapper/maven-wrapper.jarcreate mode 100644 SpringBootDemo-master/.mvn/wrapper/maven-wrapper.propertiescreate mode 100644 SpringBootDemo-master/Dockerfilecreate mode 100644 SpringBootDemo-master/README.mdcreate mode 100644 SpringBootDemo-master/kube.yamlcreate mode 100644 SpringBootDemo-master/mvnwcreate mode 100644 SpringBootDemo-master/mvnw.cmdcreate mode 100644 SpringBootDemo-master/pom.xmlcreate mode 100644 SpringBootDemo-master/src/main/java/com/example/demo/SpringBootDemoApplication.javacreate mode 100644 SpringBootDemo-master/src/main/java/com/example/demo/web/controller/IndexController.javacreate mode 100644 SpringBootDemo-master/src/main/java/com/example/demo/web/controller/RandomController.javacreate mode 100644 SpringBootDemo-master/src/main/resources/application.ymlcreate mode 100644 SpringBootDemo-master/src/test/java/com/example/demo/SpringBootDemoApplicationTests.java推送代码至仓库[root@gitlab springbootdemo]# git push -u origin masterUsername for 'http://192.168.188.95': maomaoPassword for 'http://maomao@192.168.188.95': Counting objects: 33, done.Delta compression using up to 2 pressing objects: 100% (21/21), done.Writing objects: 100% (33/33), 48.63 KiB | 0 bytes/s, done.Total 33 (delta 0), reused 0 (delta 0)To http://192.168.188.95/maomao/springbootdemo.git* [new branch]master -> masterBranch master set up to track remote branch master from origin.打开浏览器查看是否上传成功
安装Jenkins
Jenkins是一款开源自动化服务器,旨在自动化连续集成和交付软件所涉及的重复技术任务。 Jenkins是基于Java的,可以从yum安装,也可以通过下载和运行其Web应用程序ARchive(WAR)文件进行安装 - 构成一个完整的Web应用程序的集合,该文件旨在在服务器上运行。
安装jenkins所需环境
安装mavenyum install -y maven安装jdkyum install -y java安装tomcat,上传安装包tar xf apache-tomcat-8.3.5.tar -C /usr/localcd /usr/localmv apache-tomcat-8.5.16 tomcat安装docker安装需要的安装包yum install -y yum-utils设置镜像仓库我们用阿里云yum-config-manager \--add-repo \/docker-ce/linux/centos/docker-ce.repo安装dockeryum install -y docker-ce docker-ce-cli containerd.iosystemctl enable docker --nowdocker version
安装jenkins
我使用jenkins.war包进行安装
删除tomcat所有的网页文件并将jenkins解压到ROOT下cd /usr/local/tomcat/webapps/ROOTrm -rf ./*将jenkins.war放入/usr/local/tomcat/webapps/ROOT[root@jenkins ROOT]# mv /root/jenkins.war ./[root@jenkins ROOT]# lsjenkins.war解压war包unzip jenkins.war开启tomcat/usr/local/tomcat/bin/startup.sh配置镜像加速curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.iodocker version >= 1.12{"registry-mirrors": ["http://f1361db2.m.daocloud.io"]}Success.You need to restart docker to take effect: sudo systemctl restart docker设置信任192.168.188.91 harbor仓库vim /etc/docker/daemon.json添加{"registry-mirrors": ["http://f1361db2.m.daocloud.io"],"insecure-registries":["192.168.188.91"]}重启dockersystemctl restart docker生成ssh密钥对传送给git仓库主机ssh-keygenssh-copy-id gitme@192.168.188.92这里gitme就是之前gitlab服务器创建的账户
通过浏览器打开jenkins
[root@jenkins ~]# cat .jenkins/secrets/initialAdminPassword ab2c9e68357e41c79267e0ac57bcaf7c将密码输入
选择安装推荐的插件
配置jenkins流水线工作
从公开的git仓库下载课件代码文件git clone /407777853/java-democd java-demo.git为隐藏文件目录vim .git/config将这个参数修改为自己创建的git仓库地址和仓库路径url = gitme@192.168.188.92:/home/gitme/demo.git将java-demo代码上传到自己的git仓库上cd ~/java-demo添加指定目录到暂存区git add . 描述git commit -m “all”将代码推送至远程仓库的master节点git push origin master
浏览器中打开jenkins
添加凭据
[root@jenkins java-demo]# cat /root/.ssh/id_rsa-----BEGIN RSA PRIVATE KEY-----MIIEpQIBAAKCAQEAuyQpoTeiy/Nn8XJsfA4+oXPsiA7P0t2eK964B0mSowWjiFjtCw0ynLpIYA2FcZ6E+fXO7z+u/NKLYpMbf29jQSf9tYIlITePEewM5i0rHwst+deqlybocPAhJGW3h3G1IgV17uWd3DHr+fk+Js4WaEtmIUvryQjbFQrHsMuR2Q/21bUtHgFvrGTOIfBg74jGnqR3Uy5vA27wFvfGtn77HF4M7SP7rbmZ3Wg8jJLrzsm1dJoRqo/qSsZ7VkhbVTk1lD7/WF0Mqd9oE9JQz4OQOp1XHckKvybGxYmYG2hnfcqXwL3vMzUubFiMRTD9J9wlBT2LOTj8+VO/4As0fi2kOQIDAQABAoIBAQCv9YejC4HpzHq+Xj08w9QF0M7k+zBTb4eKbkaQq4/R8rpZuGthic496TWmAvCm7UrREFu7PyDUnU7rp7AiOWSU3fpCkRVCT1AZpN5YCr5R2Rwon6zJpI7HqjEvXOic1MBgNcw2jPXKFbJwfvb+W6Zb+BEWW6FejBIajeFHDZGcCjgIUj8PnH3rthIIfqY9X8RRLowPw5tIZvKCmVSe1/NXLrmblDLkHikk01aAAENyc2rBrvYlAZ6qDOiWOrMnohN9NfE6d3DG1IMK8qbyDpcr5Twb3ZMhvHF9oOWRpR+iohb28jsU1Rd5uBPN68/HpTwhlkIU/mESsV2vxItYAE41AoGBANy7Tk+IVeyF1cHs52je9wDScP92TgZn8v3e9VhuKeHPT5sTfPBeU9nWpZsci/mVadofa1WHmkAhZLHeXIqVjN0rM/oZD/rz5eY3a9zXGVSNTVeQUncvvv9JT1RdewiSNOJdBc/v9pEwfC9QEHHztkYvPZJaCh5KGkTf1YM/aTm7AoGBANkK5GX4kFcTUMUMO0e8/ZkTUS3+61r35PD+v+rglN5YT3zBGtSNsFEcjnqODmsLXE3KfPe4MRp8fSqKTJU6Ev0A34WVi1BgBtGOvRTikZqKlX1GAiLYWLB9fjrwyXmxQO05kl3XLtv8vHJWEoHXhU+hH/d+4rxMR+YfR5011BCbAoGBAM2o3zAPMS+tCxsipgH6LF3htVLwYYZqd+a+RA5epjs0s1Ej+TbkTHAdJDO/h1PGONf4ikMokznJY06uhb7B1HuFc2pEy7TgZoX3gVJEP9+dwEAjwhI2tFibb4SFjMrRnuCCF5ZEWJGygSJ8ELZ7gosmHNItflv2Yz9k5kThYiNdAoGBAId189zsyPF/va5uFuWMP971HnqNmsjY5QvAg3mNl3w6M8oSH7O/hubo4sf8t2T53y8yDNLzI7p7LgeJ74hHiahcjiUqk5ujgz3xT3OkgOmqSui39AdZbekhOdKBi82m4nXss7xb3AQy9k6KB9z/7VUoo/HtQIfzOACZTzIAcY23AoGAfYgpJCIoOD6uL65CkohqE34R6LBZoyoUiuPsUFSu2YyoyOrYaRvKP1RPe8E0+7ias+NtCk2YGxyj1xYnYMTq8goxF688H2P/vTYLwgF7jVXeW3IFl7L1jWuBx5B3IfyhPATh67nYjbyxff1sj4YgkmwOkRX0VJ63B6vzud93C3s=-----END RSA PRIVATE KEY-----将上面的密钥复制到浏览器里
创建流水线项目
添加字符参数
流水线语法
node {// 拉取代码stage('Git Checkout') {checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'a426fefe-9cbc-4bc0-abc7-7a5ac5d11a27', url: 'gitme@192.168.188.92:/home/gitme/demo.git']]])}// credentialsId就是之前的凭据ID url为通过ssh方式再git仓库服务器拉去代码的用户@地址:仓库目录// 代码编译stage('Maven Build') {sh '''/usr/bin/mvn clean package -Dmaven.test.skip=true'''}// 项目打包到镜像并推送到镜像仓库stage('Build and Push Image') {sh '''REPOSITORY=192.168.188.91/maomao/java-demo:${Branch}echo ' FROM 192.168.188.91/maomao/tomcat:v1MAINTAINER maomaoRUN rm -rf /usr/local/tomcat/webapps/*ADD target/*.war /usr/local/tomcat/webapps/ROOT.war ' > Dockerfile docker build -t $REPOSITORY .docker login -u admin -p Harbor12345 192.168.188.91docker push $REPOSITORY'''}// 部署到Docker主机stage('Deploy to Docker') {sh '''REPOSITORY=192.168.188.91/maomao/java-demo:${Branch}docker rm -f java-demo |truedocker image rm $REPOSITORY |truedocker login -u admin -p Harbor12345 192.168.188.91docker run -d --name java-demo -p 88:8080 $REPOSITORY'''}}
构建项目
构建成功
验证
通过浏览器输入jenkins服务器的ip+流水线脚本里的端口号
修改代码 测试项目能否改变
[root@jenkins java-demo]# pwd/root/java-demo[root@jenkins java-demo]# vim src/main/resources/templates/index.ftl 修改项目主页文字whatToType: "学生管理系统-Test版本",将代码推送至仓库[root@jenkins java-demo]# git add .[root@jenkins java-demo]# git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)##modified: src/main/resources/templates/index.ftl#[root@jenkins java-demo]# git commit -m "test"自动检测出哪个文件发生了变化[master 82dcff9] test1 file changed, 1 insertion(+), 1 deletion(-)[root@jenkins java-demo]# git push origin masterCounting objects: 13, done.Delta compression using up to 2 pressing objects: 100% (6/6), done.Writing objects: 100% (7/7), 561 bytes | 0 bytes/s, done.Total 7 (delta 3), reused 0 (delta 0)To gitme@192.168.188.92:/home/gitme/demo.gitbf670b2..82dcff9 master -> master
我们重新构建项目
构建完成之后 项目发生变化
如果觉得《Jenkins——Jenkins+harbor+gitlab(git) 部署maven项目》对你有帮助,请点赞、收藏,并留下你的观点哦!