环境介绍
服务器角色 | ip |
---|---|
harbor | 192.168.188.91 |
gitlab | 192.168.188.92 |
jenkins | 192.168.188.93 |
Habor是由VMWare公司开源的容器镜像仓库。事实上,Habor是在Docker Registry上进行了相应的 企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:管理用户界面,基于角色的访 问控制 ,AD/LDAP集成以及审计日志等,足以满足基本企业需求。
组件 | 功能 |
---|---|
harbor-adminserver | 配置管理中心 |
harbor-db | Mysql数据库 |
harbor-jobservice | 负责镜像复制 |
harbor-log | 记录操作日志 |
harbor-ui | web管理页面和API |
nginx | 前端代理,负责前端页面和镜像上传/下载转发 |
redis | 会话 |
registry | 镜像存储 |
安装需要的安装包
yum install -y yum-utils
设置镜像仓库
我们用阿里云
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
开启存储库
yum-config-manager --enable docker-ce-nightly
yum-config-manager --enable docker-ce-test
安装docker
yum install docker-ce docker-ce-cli containerd.io
systemctl enable docker --now
下载安装compose
curl -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-offline-installer-v1.9.1
[root@harbor ~]# tar -xf harbor-offline-installer-v1.9.1.tar
[root@harbor ~]# ls
harbor harbor-offline-installer-v1.9.1.tar
[root@harbor ~]# cd harbor
[root@harbor harbor]# ls
harbor.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 -pHarbor12345
WARNING! 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
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
现在下载一个镜像nginx
docker pull nginx
给镜像打tag
docker tag nginx:latest 192.168.188.91/maomao/nginx
上传镜像
docker push 192.168.188.91/maomao/nginx
再通过浏览器查看上传的镜像
安装git首先需要拥有ssh软件,但是该程序linux是自带拥有的,所以可以直接进行下载
yum install -y git
查看版本
git version
创建用户,用户名字随意,但是尽量还是使用和git相关的名字
[root@gitlab ~]# useradd gitme
[root@gitlab ~]# passwd gitme
Changing password for user gitme.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
切换到gitme用户
su - gitme
在家目录创建
mkdir demo.git
cd demo.git
git --bare init
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'] = true
gitlab_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 git
git clone http://192.168.188.95/maomao/springbootdemo.git
[root@gitlab ~]# git clone http://192.168.188.95/maomao/springbootdemo.git
Cloning into 'springbootdemo'...
Username for 'http://192.168.188.95': maomao
Password for 'http://[email protected]':
warning: You appear to have cloned an empty repository.
当前目录就会有一个仓库目录springbootdemo
[root@gitlab ~]# ls
gitlab-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]# ls
SpringBootDemo-master
git add .
git commit -m 'v1'
如果这里出现
*** Please tell me who you are.
则需要验证用户
git config user.name "maomao"
git config user.email "[email protected]"
我们重来一次
[root@gitlab springbootdemo]# git add .
[root@gitlab springbootdemo]# git commit -m 'v1'
[master (root-commit) 608f5ef] v1
14 files changed, 656 insertions(+)
create mode 100644 SpringBootDemo-master/.gitignore
create mode 100644 SpringBootDemo-master/.mvn/wrapper/maven-wrapper.jar
create mode 100644 SpringBootDemo-master/.mvn/wrapper/maven-wrapper.properties
create mode 100644 SpringBootDemo-master/Dockerfile
create mode 100644 SpringBootDemo-master/README.md
create mode 100644 SpringBootDemo-master/kube.yaml
create mode 100644 SpringBootDemo-master/mvnw
create mode 100644 SpringBootDemo-master/mvnw.cmd
create mode 100644 SpringBootDemo-master/pom.xml
create mode 100644 SpringBootDemo-master/src/main/java/com/example/demo/SpringBootDemoApplication.java
create mode 100644 SpringBootDemo-master/src/main/java/com/example/demo/web/controller/IndexController.java
create mode 100644 SpringBootDemo-master/src/main/java/com/example/demo/web/controller/RandomController.java
create mode 100644 SpringBootDemo-master/src/main/resources/application.yml
create mode 100644 SpringBootDemo-master/src/test/java/com/example/demo/SpringBootDemoApplicationTests.java
推送代码至仓库
[root@gitlab springbootdemo]# git push -u origin master
Username for 'http://192.168.188.95': maomao
Password for 'http://[email protected]':
Counting objects: 33, done.
Delta compression using up to 2 threads.
Compressing 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 -> master
Branch master set up to track remote branch master from origin.
打开浏览器查看是否上传成功
Jenkins是一款开源自动化服务器,旨在自动化连续集成和交付软件所涉及的重复技术任务。 Jenkins是基于Java的,可以从yum安装,也可以通过下载和运行其Web应用程序ARchive(WAR)文件进行安装 - 构成一个完整的Web应用程序的集合,该文件旨在在服务器上运行。
安装maven
yum install -y maven
安装jdk
yum install -y java
安装tomcat,上传安装包
tar xf apache-tomcat-8.3.5.tar -C /usr/local
cd /usr/local
mv apache-tomcat-8.5.16 tomcat
安装docker
安装需要的安装包
yum install -y yum-utils
设置镜像仓库
我们用阿里云
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装docker
yum install -y docker-ce docker-ce-cli containerd.io
systemctl enable docker --now
docker version
我使用jenkins.war包进行安装
删除tomcat所有的网页文件并将jenkins解压到ROOT下
cd /usr/local/tomcat/webapps/ROOT
rm -rf ./*
将jenkins.war放入/usr/local/tomcat/webapps/ROOT
[root@jenkins ROOT]# mv /root/jenkins.war ./
[root@jenkins ROOT]# ls
jenkins.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.io
docker 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"]
}
重启docker
systemctl restart docker
生成ssh密钥对传送给git仓库主机
ssh-keygen
ssh-copy-id [email protected]
这里gitme就是之前gitlab服务器创建的账户
[root@jenkins ~]# cat .jenkins/secrets/initialAdminPassword
ab2c9e68357e41c79267e0ac57bcaf7c
将密码输入
从公开的git仓库下载课件代码文件
git clone https://github.com/407777853/java-demo
cd java-demo
.git为隐藏文件目录
vim .git/config
将这个参数修改为自己创建的git仓库地址和仓库路径
url = [email protected]:/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+oXPsiA7P0t2eK964B0mSowWjiFjt
Cw0ynLpIYA2FcZ6E+fXO7z+u/NKLYpMbf29jQSf9tYIlITePEewM5i0rHwst+deq
lybocPAhJGW3h3G1IgV17uWd3DHr+fk+Js4WaEtmIUvryQjbFQrHsMuR2Q/21bUt
HgFvrGTOIfBg74jGnqR3Uy5vA27wFvfGtn77HF4M7SP7rbmZ3Wg8jJLrzsm1dJoR
qo/qSsZ7VkhbVTk1lD7/WF0Mqd9oE9JQz4OQOp1XHckKvybGxYmYG2hnfcqXwL3v
MzUubFiMRTD9J9wlBT2LOTj8+VO/4As0fi2kOQIDAQABAoIBAQCv9YejC4HpzHq+
Xj08w9QF0M7k+zBTb4eKbkaQq4/R8rpZuGthic496TWmAvCm7UrREFu7PyDUnU7r
p7AiOWSU3fpCkRVCT1AZpN5YCr5R2Rwon6zJpI7HqjEvXOic1MBgNcw2jPXKFbJw
fvb+W6Zb+BEWW6FejBIajeFHDZGcCjgIUj8PnH3rthIIfqY9X8RRLowPw5tIZvKC
mVSe1/NXLrmblDLkHikk01aAAENyc2rBrvYlAZ6qDOiWOrMnohN9NfE6d3DG1IMK
8qbyDpcr5Twb3ZMhvHF9oOWRpR+iohb28jsU1Rd5uBPN68/HpTwhlkIU/mESsV2v
xItYAE41AoGBANy7Tk+IVeyF1cHs52je9wDScP92TgZn8v3e9VhuKeHPT5sTfPBe
U9nWpZsci/mVadofa1WHmkAhZLHeXIqVjN0rM/oZD/rz5eY3a9zXGVSNTVeQUncv
vv9JT1RdewiSNOJdBc/v9pEwfC9QEHHztkYvPZJaCh5KGkTf1YM/aTm7AoGBANkK
5GX4kFcTUMUMO0e8/ZkTUS3+61r35PD+v+rglN5YT3zBGtSNsFEcjnqODmsLXE3K
fPe4MRp8fSqKTJU6Ev0A34WVi1BgBtGOvRTikZqKlX1GAiLYWLB9fjrwyXmxQO05
kl3XLtv8vHJWEoHXhU+hH/d+4rxMR+YfR5011BCbAoGBAM2o3zAPMS+tCxsipgH6
LF3htVLwYYZqd+a+RA5epjs0s1Ej+TbkTHAdJDO/h1PGONf4ikMokznJY06uhb7B
1HuFc2pEy7TgZoX3gVJEP9+dwEAjwhI2tFibb4SFjMrRnuCCF5ZEWJGygSJ8ELZ7
gosmHNItflv2Yz9k5kThYiNdAoGBAId189zsyPF/va5uFuWMP971HnqNmsjY5QvA
g3mNl3w6M8oSH7O/hubo4sf8t2T53y8yDNLzI7p7LgeJ74hHiahcjiUqk5ujgz3x
T3OkgOmqSui39AdZbekhOdKBi82m4nXss7xb3AQy9k6KB9z/7VUoo/HtQIfzOACZ
TzIAcY23AoGAfYgpJCIoOD6uL65CkohqE34R6LBZoyoUiuPsUFSu2YyoyOrYaRvK
P1RPe8E0+7ias+NtCk2YGxyj1xYnYMTq8goxF688H2P/vTYLwgF7jVXeW3IFl7L1
jWuBx5B3IfyhPATh67nYjbyxff1sj4YgkmwOkRX0VJ63B6vzud93C3s=
-----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: '[email protected]:/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:v1
MAINTAINER maomao
RUN 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.91
docker push $REPOSITORY
'''
}
// 部署到Docker主机
stage('Deploy to Docker') {
sh '''
REPOSITORY=192.168.188.91/maomao/java-demo:${Branch}
docker rm -f java-demo |true
docker image rm $REPOSITORY |true
docker login -u admin -p Harbor12345 192.168.188.91
docker run -d --name java-demo -p 88:8080 $REPOSITORY
'''
}
}
[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 ..." to unstage)
#
# modified: src/main/resources/templates/index.ftl
#
[root@jenkins java-demo]# git commit -m "test"
自动检测出哪个文件发生了变化
[master 82dcff9] test
1 file changed, 1 insertion(+), 1 deletion(-)
[root@jenkins java-demo]# git push origin master
Counting objects: 13, done.
Delta compression using up to 2 threads.
Compressing 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 [email protected]:/home/gitme/demo.git
bf670b2..82dcff9 master -> master