目录

  • Docker 镜像仓库
  • 代码上线流程(传统)
  • 基于 Docker 代码上线流程
  • 没有 Harbor
  • 有 Harbor
  • Docker 私有镜像仓库
  • Docker 私有仓库 Harbor
  • 环境准备
  • 安装 docker (harbor)
  • habor 的使用(创建项目,登录 harbor)
  • 新建项目
  • 将 harbor 注册导 docker 中
  • docker 登录失败解决方法
  • 其他两台机器连接 harbor
  • 推送镜像到 harbor
  • 从 harbor 拉取镜像到 docker02
  • Docker 官方私有仓库 Registry
  • 推送镜像到 registry
  • nginx 代理 Registry (不知道是不是这样)

Docker 镜像仓库

代码上线流程(传统)

Gitlab Jenkins
1)开发将代码上传到gitlab
2)使用Jenkins拉取代码
- freestyle
- maven
- pipeline
3)使用sonarqube对代码进行质量检测
4)如果需要编译的代码,就编译构建(JaVa、C等)
5)使用金丝雀发布方式,发布单台服务器
6)对单台服务器进行测试(测试根据测试用例进行功能测试)
7)单机机器加入集群,发布其他机器
8)整套集群进行测试

基于 Docker 代码上线流程

没有 Harbor

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_nginx

有 Harbor

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_nginx_02

Docker 私有镜像仓库

  • registry // 可以使用 nginx 做代理,无图形化
  • harbor // 千万不能使用 nginx 代理,有图形化

Docker 私有仓库 Harbor

环境准备

主机

IP

角色

docker01

10.0.0.101

gitlab、jenkins

harbor

10.0.0.100

harbor 私有镜像仓库

docker02

10.0.0.102

web

Harbor 是为企业用户设计的容器镜像仓库开源项目,包括了权限管理 (RBAC)、LDAP、审计、安全漏洞扫描、镜像验真、管理界面、自我注册、HA 等企业必需的功能,同时针对中国用户的特点,设计镜像复制和中文支持等功能。

安装 docker (harbor)

## 换源
[root@harbor ~]# wget -O https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@harbor ~]# https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 
## 下载docker和docker-compose
[root@harbor ~]# yum install -y docker-ce docker-ce-cli containerd.io docker-compose
 
## docker镜像加速
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://7t3bpp45.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
 
## 下载harbor安装包
### harbor的github地址
https://github.com/goharbor/harbor/releases/tag/v2.9.0
### 在harbor机器中下载harbor
[root@harbor ~]wget https://github.com/goharbor/harbor/releases/download/v2.9.0/harbor-offline-installer-v2.9.0.tgz
 
## 解压harbor
[root@harbor ~]# tar xf harbor-offline-installer-v1.10.0.tgz 
 
# 修改harbor的docker-compose配置文件
[root@harbor harbor]# vim harbor.yml 
hostname: 10.0.0.100
harbor_admin_password: 123
----------注释https内容---------------------
# https related config
#https:
  # https port for harbor, default is 443
#  port: 443
  # The path of cert and key files for nginx
#  certificate: /your/certificate/path
#  private_key: /your/private/key/path
-------------------------------------------
 
## 安装harbor
[root@harbor ~]# cd harbor/
[root@harbor harbor]# ./install.sh 
 ----Harbor has been installed and started successfully.----

浏览器访问 harbor

10.0.0.100

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_docker_03

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_nginx_04

habor 的使用(创建项目,登录 harbor)

新建项目

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_Docker_05

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_docker_06

将 harbor 注册导 docker 中
## 登录harbor
[root@harbor harbor]# docker login

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_docker_07

默认情况下,登录dockerhub

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_Docker_08

[root@harbor harbor]# docker login 10.0.0.100:80
docker 默认使用443,需要安全证书认证登录
## 修改docker配置文件(所有机子)
[root@harbor harbor]# vim /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://7t3bpp45.mirror.aliyuncs.com"],
  "insecure-registries": ["http://10.0.0.100"]
}
 
## 重启docker与docker-compose
[root@harbor harbor]# systemctl restart docker
[root@harbor harbor]# docker-compose restart
docker 登录失败解决方法
## 解决方法
# 进入harbor目录
[root@harbor harbor]# cd /root/harbor
# 停止compose启动的所有容器
[root@harbor harbor]# docker-compose stop
# 删除所有容器
[root@harbor harbor]# docker rm -f $(docker ps -aq)
# compose启动的所有容器并在后台运行
[root@harbor harbor]# docker-compose up -d
其他两台机器连接 harbor
[root@docker01 ~]# vim /etc/docker/daemon.json 
[root@docker01 ~]# systemctl restart docker
[root@docker01 ~]# docker login 10.0.0.100
Username: admin
Password: 
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
 
[root@docker02 ~]# vim /etc/docker/daemon.json
[root@docker02 ~]# systemctl restart docker
[root@docker02 ~]# docker login 10.0.0.100
Username: admin
Password: 
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
推送镜像到 harbor

镜像格式:harbor 地址 / 项目名称 / 镜像名:标签

# 修改镜像名
[root@docker01 ~]# docker tag web:v1 10.0.0.100/wordpress/web:v1、
 
# 推送镜像
[root@docker01 ~]# docker push 10.0.0.100/wordpress/web:v1 
The push refers to repository [10.0.0.100/wordpress/web]
8b297ea61669: Pushed 
419df8b60032: Pushed 
0e835d02c1b5: Pushed 
5ee3266a70bd: Pushed 
3f87f0a06073: Pushed 
1c9c1e42aafa: Pushed 
8d3ac3489996: Pushed 
v1: digest: sha256:f384a3de55d5aa9d8eb21159b0e1e9d7681c47dceb17e1b1eb5187629d63456d size: 1775
从 harbor 拉取镜像到 docker02

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_nginx_09

[root@docker02 ~]# docker pull 10.0.0.100/wordpress/web:v1
v1: Pulling from wordpress/web
59bf1c3509f3: Already exists 
f3322597df46: Already exists 
d09cf91cabdc: Already exists 
3a97535ac2ef: Already exists 
919ade35f869: Already exists 
40e5d2fe5bcd: Already exists 
6feddb8018e1: Pull complete 
Digest: sha256:f384a3de55d5aa9d8eb21159b0e1e9d7681c47dceb17e1b1eb5187629d63456d
Status: Downloaded newer image for 10.0.0.100/wordpress/web:v1
10.0.0.100/wordpress/web:v1
 
# 运行拉取的镜像
[root@docker02 ~]# docker run -p 80:80 -d 10.0.0.100/wordpress/web:v1
d0e71a5343336a708d120a88530487d3f153d75e0ef601965bf05d39f5c12b75

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_docker_10

Docker 官方私有仓库 Registry

主机

IP

角色

docker01

10.0.0.101

gitlab、jenkins

docker02

10.0.0.102

Registry 私有镜像仓库

# 拉registry镜像
[root@docker02 ~]# docker pull registry
 
# 查看镜像详细信息
[root@docker02 ~]# docker inspect registry:latest 
 
# 启动镜像
[root@docker02 ~]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry

推送镜像到 registry

镜像格式:registry 地址 / 镜像名:标签

# 修改镜像名
[root@docker01 ~]# docker tag web:v1 10.0.0.102:5000/web:v1
 
# 修改docker配置文件
[root@docker01 ~]# vim /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://7t3bpp45.mirror.aliyuncs.com"],
  "insecure-registries": ["http://10.0.0.100","http://10.0.0.102:5000"]
}
 
# 重启docker
[root@docker01 ~]# systemctl restart docker
 
# 推送镜像到registry
[root@docker01 ~]# docker push 10.0.0.102:5000/web:v1
The push refers to repository [10.0.0.102:5000/web]
8b297ea61669: Pushed 
419df8b60032: Pushed 
0e835d02c1b5: Pushed 
5ee3266a70bd: Pushed 
3f87f0a06073: Pushed 
1c9c1e42aafa: Pushed 
8d3ac3489996: Pushed 
v1: digest: sha256:f384a3de55d5aa9d8eb21159b0e1e9d7681c47dceb17e1b1eb5187629d63456d size: 1775
 
# 查看镜像仓库中的镜像
[root@docker01 ~]# curl http://10.0.0.102:5000/v2/_catalog
{"repositories":["web"]}
 
# 查看镜像及标签
[root@docker01 ~]# curl http://10.0.0.102:5000/v2/web/tags/list
{"name":"web","tags":["v1"]}
 
# 查看本地持久化目录
[root@docker02 ~]# ll /opt/myregistry/docker/registry/v2/repositories/
total 0
drwxr-xr-x 5 root root 55 Sep 13 11:44 web
 
# 删除私有仓库中的镜像
[root@docker02 ~]# cd /opt/myregistry/docker/registry/v2/repositories/
[root@docker02 repositories]# ll
total 0
drwxr-xr-x 5 root root 55 Sep 13 11:44 web
[root@docker02 repositories]# rm -fr web
 
# 再次查看镜像
[root@docker01 ~]# curl http://10.0.0.102:5000/v2/_catalog
{"repositories":[]}

nginx 代理 Registry (不知道是不是这样)

# 启动nginx
docker run -d \
  --restart always \
  --name nginx \
  -v /srv/data/nginx/conf/conf.d:/etc/nginx/conf.d \
  -p 80:80 \
  nginx
 
# 编写nginx配置文件
[root@docker02 conf.d]# vim /srv/data/nginx/conf/conf.d/registry.conf
server {
    listen 80;
    server_name _;
 
    location / {
        proxy_pass http://10.0.0.102:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

docker 列出远程镜像仓库指定路径下所有镜像 docker镜像仓库地址_Docker_11