Docker+Gitlab+Jenkins自动化部署
部署前准备:先安装个Linux服务器,配置随便准备好Maven3、JDK1.8压缩包和Git安装包好,下面开始操:一、安装docker-ce# 到 /etc/yum.repos.d 目录下使用wget指令拉取docker.repo文件,阿里云yum源地址:http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo...
·
部署前准备:
先安装个Linux服务器,配置随便
准备好Maven3、JDK1.8压缩包和Git安装包
好,下面开始操:
一、安装docker-ce
# 到 /etc/yum.repos.d 目录下使用wget指令拉取docker.repo文件,docker--yum源地址:
# https://download.docker.com/linux/centos/docker-ce.repo
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# wget https://download.docker.com/linux/centos/docker-ce.repo
[root@node1 yum.repos.d]# ls
bak centos.repo docker-ce.repo
# 清除yum缓存
[root@node1 yum.repos.d]# yum clean all
# 更新yum源
[root@node1 yum.repos.d]# yum -y update
# 安装docker依赖包
[root@node1 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
# 安装docker
[root@node1 ~]# yum -y install docker-ce
# 关闭selinux和防火墙
[root@node1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@node1 ~]# systemctl disable --now firewalld
[root@node1 ~]# iptables -F
[root@node1 ~]# iptables -X
[root@node1 ~]# iptables -Z
[root@node1 ~]# /usr/sbin/iptables-save
# 启动docker并设置开机自启动
[root@node1 yum.repos.d]# systemctl enable --now docker
# 查看docker服务状态
[root@node1 yum.repos.d]# systemctl status docker
到这里docker社区版就已经安装好了,下面我们得传maven3和jdk1.8压缩包到Linux服务器里面
二、传送maven3和jdk1.8的压缩包到服务器并解压配置环境文件
# 上传maven3和jdk1.8
# 配置jdk和maven的环境变量
[root@node1 ~]# ls
# 解压
[root@node1 ~]# tar -zxvf apache-maven-3.6.1-bin.tar.gz
[root@node1 ~]# tar -zxvf jdk-8u121-linux-x64.tar.gz
# 在/usr/local目录下 创建maven3和jdk1.8目录
[root@node1 ~]# mkdir -p /usr/local/maven3
[root@node1 ~]# mkdir -p /usr/local/jdk1.8
# 拷贝解压后的maven3和jdk1.8到 新创建的maven3和jdk1.8目录下
[root@node1 ~]# cd apache-maven-3.6.1
[root@node1 apache-maven-3.6.1]# cp -r * /usr/local/maven3/
[root@node1 apache-maven-3.6.1]# cd ..
[root@node1 ~]# cd jdk1.8.0_121/
[root@node1 jdk1.8.0_121]# cp -r * /usr/local/jdk1.8/
# 配置环境变量文件
[root@node1 ~]# vim /etc/profile
# 到最后一行添加以下内容:
# jdk
export JAVA_HOME=/usr/local/jdk1.8
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
# maven
export M2_HOME=/usr/local/maven3
export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
# 保存退出
# 生效环境变量文件并查看jdk和maven版本信息
[root@node1 ~]# source /etc/profile
[root@node1 ~]# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
[root@node1 ~]# mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/maven3
Java version: 1.8.0_121, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.45.1.el7.x86_64", arch: "amd64", family: "unix"
到这里jdk和maven的环境就配置好了,下面我们要在IDEA里创建一个SpringBoot的项目
三、IDEA创建SpringBoot项目
# 创建SpringBoot项目
# 新建controller包,新建LoginController.java类,编写测试程序
# 新建docker目录,新建Dockerfile文件,编写Dockerfile
# 在编写Dockerfile之前,先打包
# 编写Dockerfile
# 编写完成后再重新打包
到这里项目就配置完成了,下面要在我们的Windows本机安装Git
四、Windows安装Git
# 使用ssh-keygen -t rsa -C "root" 生成Windows本机秘钥和公钥
到这里Windows的Git安装好了,钥匙也生成好了,下面就到Linux服务器里拉取gitlab和jenkins的镜像了
五、拉取gitlab和jenkins镜像并部署gitlab
# gitlab镜像拉取
[root@node1 ~]# docker pull gitlab/gitlab-ce
# jenkins镜像拉取
[root@node1 ~]# docker pull jenkins/jenkins:lts
# 查看已下载镜像
[root@node1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gitlab/gitlab-ce latest cb10c65dbb7b 6 days ago 2.31GB
jenkins/jenkins lts e4ebf98bd6ca 3 weeks ago 441MB
# 部署gitlab
# 创建目录
[root@node1 ~]# mkdir -p /usr/local/gitlab/config
[root@node1 ~]# mkdir -p /usr/local/gitlab/logs
[root@node1 ~]# mkdir -p /usr/local/gitlab/data
# 启动运行gitlab容器
[root@node1 ~]# docker run --detach --hostname 192.168.40.150 --publish 7001:443 --publish 7002:80 --publish 7003:22 --name gitlab --restart always \
> --volume /usr/local/gitlab/config/:/etc/gitlab \
> --volume /usr/local/gitlab/logs/:/var/log/gitlab \
> --volume /usr/local/gitlab/data/:/var/opt/gitlab gitlab/gitlab-ce
a8eac9ac773b234c14eec081c3f39cb4e883d0cd36e4388de73e0751212e7c43
# 查看已运行容器
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8eac9ac773b gitlab/gitlab-ce "/assets/wrapper" About a minute ago Up About a minute (health: starting) 0.0.0.0:7003->22/tcp, :::7003->22/tcp, 0.0.0.0:7002->80/tcp, :::7002->80/tcp, 0.0.0.0:7001->443/tcp, :::7001->443/tcp gitlab
# 修改gitlab.rb配置文件
[root@node1 ~]# vim /usr/local/gitlab/config/gitlab.rb
# 到最后一行添加以下内容:
external_url 'http://192.168.40.150' # Linux服务器IP
gitlab_rails['gitlab_ssh_host'] = '192.168.40.150'
gitlab_rails['gitlab_shell_ssh_port'] = 7003 # ssh端口
# 保存退出
# 进入gitlab容器重启服务
[root@node1 ~]# docker exec -it gitlab /bin/bash
root@192:/# gitlab-ctl reconfigure
# 退出gitlab容器,重启gitlab容器
root@192:/# exit
exit
[root@node1 ~]# docker restart gitlab
gitlab
# 安装net-tools并查看端口
[root@node1 ~]# yum -y install net-tools
[root@node1 ~]# netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::7001 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
tcp6 0 0 :::7002 :::* LISTEN
tcp6 0 0 :::7003 :::* LISTEN
六、浏览器访问:http://192.168.40.150(Linux服务器IP):7002
# 出现这个界面说明安装成功,接下来需要进到容器里重置密码
[root@node1 ~]# docker exec -it gitlab /bin/bash
root@192:/# gitlab-rails console -e production
--------------------------------------------------------------------------------
Ruby: ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux]
GitLab: 14.4.1 (1a23d731c9f) FOSS
GitLab Shell: 13.21.1
PostgreSQL: 12.7
--------------------------------------------------------------------------------
Loading production environment (Rails 6.1.4.1)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id:1 @root>
irb(main):002:0> user.password = '12345678'
=> "12345678"
irb(main):003:0> user.password_confirmation = '12345678'
=> "12345678"
irb(main):004:0> user.save!
Enqueued ActionMailer::MailDeliveryJob (Job ID: 5f22b4b1-1534-482a-88c1-40b8a947cf2d) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", {:args=>[#<GlobalID:0x00007f6e42f74560 @uri=#<URI::GID gid://gitlab/User/1>>]}
=> true
# 重置密码成功,密码为:12345678,接下来重新访问网址,用户:root;密码:12345678
七、添加Windows本机的公钥到gitlab并在gitlab创建一个空项目——hellospringboot
-
拷贝公钥
-
创建空项目——hellospringboot
到这里gitlab的部署算是完成了,接下来我们到IDEA创建git库,推送项目到gitlab
八、将IDEA里的SpringBoot项目推送到gitlab上
到这里我们将SpringBoot项目推送到gitlab的项目里的任务已经完成了,接下来我们要在Linux服务器上安装Git以及Jenkins的部署
九、CentOS安装Git并部署Jenkins
# 先将原本的git卸载了
[root@node1 ~]# rpm -qa|grep git
net-tools-2.0-0.25.20131004git.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
linux-firmware-20200421-80.git78c0348.el7_9.noarch
[root@node1 ~]# yum remove -y crontabs-1.11-6.20121102git.el7.noarch linux-firmware-20200421-80.git78c0348.el7_9.noarch
[root@node1 ~]# rpm -qa|grep git
net-tools-2.0-0.25.20131004git.el7.x86_64
# 安装git
[root@node1 ~]# yum -y install git
[root@node1 ~]# rpm -qa|grep git
net-tools-2.0-0.25.20131004git.el7.x86_64
git-1.8.3.1-23.el7_8.x86_64
# 创建Jenkins的映射目录
[root@node1 ~]# mkdir -p /usr/local/jenkins/workspace
# 创建Jenkins容器并运行
[root@node1 ~]# docker run -uroot -d --restart=always -p 9001:8080 \
> -v /usr/local/jenkins/workspace/:/var/jenkins_home/workspace \
> -v /var/run/docker.sock:/var/run/docker.sock \
> -v /usr/bin/git:/usr/bin/git \
> -v /usr/local/jdk1.8/:/usr/local/jdk1.8/ \
> -v /usr/local/maven3/:/usr/local/maven3/ --name jenkins jenkins/jenkins:lts
bf5659973f89a77dd6b3cefa6c250679437cc3f153a60d3042cf62ded0f7e2c5
[root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf5659973f89 jenkins/jenkins:lts "/sbin/tini -- /usr/…" About a minute ago Up About a minute 50000/tcp, 0.0.0.0:9001->8080/tcp, :::9001->8080/tcp jenkins
a8eac9ac773b gitlab/gitlab-ce "/assets/wrapper" About an hour ago Up About an hour (healthy) 0.0.0.0:7003->22/tcp, :::7003->22/tcp, 0.0.0.0:7002->80/tcp, :::7002->80/tcp, 0.0.0.0:7001->443/tcp, :::7001->443/tcp gitlab
# 配置libpcre.so.3的软连接并生成Jenkins的秘钥和公钥
[root@node1 ~]# docker exec -it -uroot jenkins /bin/bash
root@bf5659973f89:/# git
git: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
root@bf5659973f89:/# ldd $(which /usr/bin/git)
linux-vdso.so.1 (0x00007ffd5c0b1000)
libpcre.so.1 => not found
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f62ce68b000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f62ce669000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f62ce4a4000)
/lib64/ld-linux-x86-64.so.2 (0x00007f62ce6ad000)
root@bf5659973f89:/# cd /lib/x86_64-linux-gnu/
root@bf5659973f89:/lib/x86_64-linux-gnu# ln -s libpcre.so.3 libpcre.so.1
root@bf5659973f89:/lib/x86_64-linux-gnu# ssh-keygen -t rsa -C "root"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:sNKM/ch97SBbKP8L/eCK3ip07qPQIVQWZ21Y737knAk root
The key's randomart image is:
+---[RSA 3072]----+
| +.o+. |
| o o. o. |
| . .. . |
| . = o . |
| . + = S E . |
| o.+.+ + * o |
| ...o+ * B B |
| .. o= B * |
| .==o=o+.o |
+----[SHA256]-----+
root@bf5659973f89:/lib/x86_64-linux-gnu# cd
root@bf5659973f89:~# cd /root/.ssh/
root@bf5659973f89:~/.ssh# ls
id_rsa id_rsa.pub known_hosts
root@bf5659973f89:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCtCOXBcIb6piqYG5o41eQkPwhsxr6Zu7/uZhopfKfleAL4/h9FdXNumLeVGzPUf6Zpub+Ei7AcMjX6jZvGuaWNwlHzFRIEOYk3HvDWkYxcsjA/j/NMANveEKPcK6j9W8PR5idJTN8AC1U6nDy2YYoioLsCMmJT/qydNdLe9nB+5k0i59J2l/eCXTVv3j2qpP1L9AvEFamlpHYEYhOtqeBlnANPl4sgcSmaBLpOh9XJcolZ64BVSAo6w36gvRMc48/dFUcbO3rY5K+aCbDhB6m2BkukmpHBkIpf5u/xKyuqrN6EryxHqVUpL3WBvvGfCnCRphfpm4LXwXzgAJuAIoTSi5SRU22y/oJ8rcbpXiYhqlUHIPdSV/xDei0q9+1fRABLNEadPVCetC2+UxRsQnFAXGxkcomj4BVht7jI6MNM+02h/nx1FkqxKYVVT7w8avMXky+A020FVNjhKMu9JQthZz21uguDIP4btHj0dr8arV6cHh07ItEh0EEnK23FMpE= root
到这里就将这个SSH公钥添加到gitlab上,之后做项目拉取
# 添加Jenkins的SSH公钥后,就可以进行项目拉取了
root@bf5659973f89:~/.ssh# git clone ssh://git@192.168.40.150:7003/root/hellospringboot.git
Cloning into 'hellospringboot'...
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 34 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (34/34), 53.54 KiB | 0 bytes/s, done.
root@bf5659973f89:~/.ssh#
# 拉取成功
到这里Jenkins的准备工作就做好了,接下来开始访问:http://192.168.40.150(Linux服务器地址):9001
十、访问http://192.168.40.150(Linux服务器地址):9001,进行部署Jenkins
# 查看管理员密码,将密码复制
root@bf5659973f89:~/.ssh# cat /var/jenkins_home/secrets/initialAdminPassword
e965f461c5c94fec80980061f810c99c
root@bf5659973f89:~/.ssh#
拷贝到Jenkins访问页面:
OK!到这里我们已经完成安装了,接下来进行插件安装、添加凭据、系统设置、全局设置、项目部署
到这里我们先去Linux服务器里修改 /usr/local/maven3/conf/settings.xml文件
# 配置镜像加速
[root@node1 conf]# vim /usr/local/maven3/conf/settings.xml
# 将以下内容添加到<mirrors></mirrors>之间
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
# 保存退出
再回到jenkins界面:
到这里我们的配置已经完成了,接下来进行项目部署
十一、hellospringboot项目在Jenkins自动化部署
到这里我们可以看到,部署完成,很顺利,接下来浏览器访问:http://192.168.40.150:8080/login
可以看到我们完成了docker+gitlab+jenkins的自动化部署,下面可以进到Linux服务器里面查看情况
docker images——查看所有已有镜像
可以看到lzzy/hellospringboot这个镜像就是我们刚刚自动化部署自动制作的一个镜像
相当于我们写了一个java程序项目
我们完成了java项目打包、创建git库、将java项目推送到gitlab上、再用jenkins将gitlab上的项目拉取到jenkins容器
然后通过jenkins实现自动将java项目包制作成镜像、做成docker容器、启动容器后我们即可访问
部署结束!!!
更多推荐
已为社区贡献1条内容
所有评论(0)