基于docker构建cicd平台
务器规划:
角色 |
ip |
loadbalance-master,gitlab |
192.168.31.61 |
loadbalance-master,jenkins |
192.168.31.62 |
harbor |
192.168.31.70 |
配置aliyun镜像仓库
用来保存一些学习镜像
阿里云镜像仓库控制台:https://cr.console.aliyun.com/cn-hangzhou/instances/repositories
1. 登录阿里云Docker Registry
$ sudo docker login --username=benjamin7788 registry.cn-hangzhou.aliyuncs.com
用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。
您可以在访问凭证页面修改凭证密码。
2. 从Registry中拉取镜像
sudo docker pull registry.cn-hangzhou.aliyuncs.com/benjamin-learn/[镜像名称]:[镜像版本号]
3. 将镜像推送到Registry
sudo docker login --username=benjamin7788 registry.cn-hangzhou.aliyuncs.com
sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/benjamin-learn/[镜像名称]:[镜像版本号]
sudo docker push registry.cn-hangzhou.aliyuncs.com/benjamin-learn/[镜像名称]:[镜像版本号]
实例:
上传
[root@loadbalancer1 ~]# sudo docker login --username=benjamin7788 registry.cn-hangzhou.aliyuncs.com
[root@loadbalancer1 ~]# sudo docker tag 18da462b5ff5 registry.cn-hangzhou.aliyuncs.com/benjamin-learn/gitlab-ce-zh:latest
[root@loadbalancer1 ~]# docker push registry.cn-hangzhou.aliyuncs.com/benjamin-learn/gitlab-ce-zh:latest
下载
[root@loadbalancer2 ~]# sudo docker pull registry.cn-hangzhou.aliyuncs.com/benjamin-learn/gitlab-ce-zh:latest
harbor安装
harbor官方地址:https://github.com/goharbor/harbor
点击relase选择要下载的版本
这边用离线包安装:
包有点大 600MB 建议上github 下载
安装docker
略。。。。
安装docker-compose
由于FQ不便,这边直接保存了 docker-compose的二进制包
📎docker-compose-Linux-x86_64.zip
unzip docker-compose-Linux-x86_64.zip
mv docker-compose-Linux-x86_64 /usr/bin/docker-compose
部署harbor
根据下载好的harbor离线包部署harbor
tar zxvf harbor-offline-installer-v1.9.1.tgz
cd harbor
sed -i s/reg.mydomain.com/192.168.31.70/g harbor.yml
./prepare
./install.sh
默认端口 80访问 浏览器192.168.31.70 访问harbor控制台
默认登录口令 admin/Harbor12345
登录会有个默认library项目
上传已经构建的镜像
在上传之前需要注意docker默认不支持http的请求方式
除非。。。。。
手动添加ip地址的信任,这样就可以使用http方式操作私有registry
[root@harbor harbor]# docker login 192.168.31.70 --username=admin --password=Harbor12345
[root@harbor harbor]# docker tag php:v1 192.168.31.70/library/php:v1
[root@harbor harbor]# docker tag nginx:v1 192.168.31.70/library/nginx:v1
[root@harbor harbor]# docker tag tomcat:v1 192.168.31.70/library/tomcat:v1
[root@harbor harbor]# docker push 192.168.31.70/library/php:v1
[root@harbor harbor]# docker push 192.168.31.70/library/nginx:v1
[root@harbor harbor]# docker push 192.168.31.70/library/tomcat:v1
如何pull私有仓库镜像
docker login 192.168.31.70 --username=admin --password=Harbor12345
docker pull 192.168.31.70/library/nginx:v1
docker部署gitlab
部署gitlab
docker run -d \
--name gitlab \
-p 8443:443 \
-p 9999:80 \
-p 9998:22 \
-v $PWD/gitlab/config:/etc/gitlab \
-v $PWD/gitlab/logs:/var/log/gitlab \
-v $PWD/gitlab/data:/var/opt/gitlab \
-v /etc/localtime:/etc/localtime \
registry.cn-hangzhou.aliyuncs.com/benjamin-learn/gitlab-ce-zh:latest
通过 192.168.31.61:9999 访问gitlab
登录后会提示修改密码 默认用户是 root
创建项目,命名为java-demo 其他默认(类型为私有)即可
提交java-demo的代码
yum install git -y
git clone http://192.168.31.61:9999/root/java-demo.git
unzip tomcat-java-demo-master.zip
cd tomcat-java-demo-master
mv * ../java-demo/ && cd ../java-demo
git add .
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git commit -m 'all'
git push origin master
docker部署jenkins
📎apache-maven-3.5.0-bin.tar.gz
jdk 官网下载即可。。。包太大 165MB
准备JDK和Maven环境
tar zxvf jdk-8u45-linux-x64.tar.gz
mv jdk1.8.0_45 /usr/local/jdk
tar zxf apache-maven-3.5.0-bin.tar.gz
mv apache-maven-3.5.0 /usr/local/maven
# 部署jenkins
docker run -d --name jenkins -p 80:8080 -p 50000:50000 -u root \
-v /opt/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
-v /usr/local/maven:/usr/local/maven \
-v /usr/local/maven/bin/mvn:/usr/bin/mvn \
-v /usr/local/jdk:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
--name jenkins registry.cn-hangzhou.aliyuncs.com/benjamin-learn/jenkins:lts
获取密码
根据volume 映射规则 -v /opt/jenkins_home:/var/jenkins_home
[root@loadbalancer2 opt]# cat /opt/jenkins_home/secrets/initialAdminPassword
0b23bee2709049cc886f928b81f5b3b7
进入后跳过插件安装
所有插件都不要勾选,然后点击安装
创建一个管理员
admin,保存 来到开始页面
替换jenkins的插件源
由于默认jenkins的源是国外的下载速度较慢,换成国内的源
cd /opt/jenkins_home/updates/
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.g
oogle.com/https:\/\/www.baidu.com/g' default.json
重启jenkins容器
有选择性的安装插件
manage jenkins---》manage plugin
点击available 插件 ,分别搜索git pipline两个插件
创建job
选择流水线
pipeline脚本
这个脚本完成 工作
- 从 gitlab 拉取代码
- 使用maven 构建war包
- 构建镜像
- 部署docker
#!/usr/bin/env groovy
def registry = "192.168.31.70"
def project = "welcome"
def app_name = "demo"
def image_name = "${registry}/${project}/${app_name}:${Branch}-${BUILD_NUMBER}"
def git_address = "http://192.168.31.61:9999/root/java-demo.git"
def docker_registry_auth = "4add8c05-bfdb-48bd-8b74-6b9a2c4b6af6"
def git_auth = "7c36059c-cb02-461f-999d-32c713962413"
pipeline {
agent any
stages {
stage('拉取代码'){
steps {
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
}
stage('代码编译'){
steps {
sh """
JAVA_HOME=/usr/local/jdk
PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH
mvn clean package -Dmaven.test.skip=true
"""
}
}
stage('构建镜像'){
steps {
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
echo '
FROM ${registry}/library/tomcat:v1
LABEL maitainer benjamin
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}' ${registry}
docker push ${image_name}