目标

安装harbor 作为docker的镜像仓库。
由于之前安装了k8s,安装的docker版本较低,新版的harbor要求较新版本的docker。故先升级安装docker再安装harbor

docker 升级安装

参考:https://www.cnblogs.com/wangzy-tongq/p/13273122.html
1 、停止docker服务

systemctl stop docker

2、卸载旧版本docker

yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
rm -rf /etc/systemd/system/docker.service.d
rm -rf /var/lib/dockerrm -rf /var/run/docker

3、安装依赖包

yum -y install yum-utils device-mapper-persistent-data lvm2

4、配置docker YUM镜像仓库

yum-config-manager --add-repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

5、查询并安装指定版本

yum list docker-ce --showduplicates|sort -r   # 查询docker版本
yum install docker-ce-18.06.2.ce-3.el7 -y  # 安装指定版本

6、启动并设置开机自启动

systemctl start docker
systemctl enable docker

安装harbor

安装harbor前需要安装docker-compose

安装docker-compose

参考:https://www.cnblogs.com/zhi-leaf/p/12090456.html
curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

docker-compose常用命令:
docker-compose -h                           # 查看帮助

docker-compose up                           # 创建并运行所有容器
docker-compose up -d                      # 创建并后台运行所有容器
docker-compose -f docker-compose.yml up -d  # 指定模板
docker-compose down                      # 停止并删除容器、网络、卷、镜像。

docker-compose logs       # 查看容器输出日志
docker-compose pull       # 拉取依赖镜像
dokcer-compose config     # 检查配置
dokcer-compose config -q  # 检查配置,有问题才有输出

docker-compose restart   # 重启服务
docker-compose start     # 启动服务
docker-compose stop      # 停止服务

安装harbor

参考:https://www.cnblogs.com/scajy/p/13846721.html
1、下载harbor

wget https://github.com/goharbor/harbor/releases/download/v2.1.3/harbor-offline-installer-v2.1.3.tgz

2、解压缩harbor

[root@k8sn01 ~]# tar zxvf harbor-offline-installer-v2.1.3.tgz 
harbor/harbor.v2.1.3.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml.tmpl

3、 拷贝配置文件

[root@k8sn01 ~]# cd harbor/
[root@k8sn01]# cp harbor.yml.tmpl harbor.yml

4、修改harbor.yml

hostname = 本机主机名                   # 修改连接地址,可以是域名(主机名作为本地),ip
harbor_admin_password = 123456          # harbor admin配置密码

修改配置文件

# Configuration file of Harbor

# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: k8sn01

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 808

# https related config
#https: 暂时不用https先注释
  # https port for harbor, default is 443
 # port: 443 暂时不用https先注释
  # The path of cert and key files for nginx
  #certificate: /your/certificate/path 暂时不用https先注释
  #private_key: /your/private/key/path 暂时不用https先注释

注: docker从本地harbor拉取和推送镜像需要通过域名方式,此处用主机的主机名。需要修改hostname和/etc/hosts

[root@k8sn01 harbor]# hostnamectl set-hostname k8sn01

[root@k8sn01 harbor]# hostname
k8sn01
[root@k8sn01 harbor]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.80.187 k8sn01

5、执行prepare
每次修改harbor配置文件后均需要执行prepare,需要先安装docker-compose

[root@k8sn01 harbor]# ./prepare
prepare base dir is set to /root/docker/harbor/harbor
WARNING:root:WARNING: HTTP protocol is insecure. Harbor will deprecate http protocol in the future. Please make sure to upgrade to https
Clearing the configuration file: /config/portal/nginx.conf
Clearing the configuration file: /config/log/logrotate.conf
Clearing the configuration file: /config/log/rsyslog_docker.conf
Clearing the configuration file: /config/nginx/nginx.conf
Clearing the configuration file: /config/core/env
Clearing the configuration file: /config/core/app.conf
Clearing the configuration file: /config/registry/passwd
Clearing the configuration file: /config/registry/config.yml
Clearing the configuration file: /config/registry/root.crt
Clearing the configuration file: /config/registryctl/env
Clearing the configuration file: /config/registryctl/config.yml
Clearing the configuration file: /config/db/env
Clearing the configuration file: /config/jobservice/env
Clearing the configuration file: /config/jobservice/config.yml
Generated configuration file: /config/portal/nginx.conf
Generated configuration file: /config/log/logrotate.conf
Generated configuration file: /config/log/rsyslog_docker.conf
Generated configuration file: /config/nginx/nginx.conf
Generated configuration file: /config/core/env
Generated configuration file: /config/core/app.conf
Generated configuration file: /config/registry/config.yml
Generated configuration file: /config/registryctl/env
Generated configuration file: /config/registryctl/config.yml
Generated configuration file: /config/db/env
Generated configuration file: /config/jobservice/env
Generated configuration file: /config/jobservice/config.yml
loaded secret from file: /data/secret/keys/secretkey
Generated configuration file: /compose_location/docker-compose.yml
Clean up the input dir

6、执行install安装脚本
安装成功后会自动运行harbor

[root@k8sn01 harbor]# ./install.sh 

由于没有安装证书,docker访问harbor需要证书,修改以下内容可以直接访问

[root@k8sn01 harbor]# cd /etc/docker/
[root@k8sn01 docker]# ls
daemon.json  daemon.json.rpmsave  key.json
[root@k8sn01 docker]# cat daemon.json
{ "insecure-registries":["k8sn01:808"]}

7、重启docker

[root@k8sn01 docker]# systemctl daemon-reload  #加载配置文件
[root@k8sn01 docker]# systemctl restart docker

8、重启docker-compose

[root@k8sn01 harbor]# ./prepare
[root@k8sn01 harbor]# docker-compose down -v
[root@k8sn01 harbor]# docker-compose up -d

9、harbor页面
此处新建一个项目
在这里插入图片描述

docker 上传和下载harbor镜像仓库

1、登录harbor仓库

[root@k8sn01 harbor]# docker login k8sn01:808
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

2、上传镜像
需要上传镜像的tag需要修改成和镜像仓库中命名保持一致,否则无法上传

[root@k8sn01 harbor] docker push k8sn01:808/lsnow8624/mynginx:v1.0

仓库中已经有镜像
在这里插入图片描述
3、下载镜像

[root@k8sn01 harbor]# docker rmi k8sn01:808/lsnow8624/mynginx:v1.0 # 先删掉该镜像
[root@k8sn01 harbor]# docker pull k8sn01:808/lsnow8624/mynginx:v1.0
v1.0: Pulling from lsnow8624/mynginx
7a0437f04f83: Already exists 
383779435197: Pull complete 
b9d5d5fa8ca1: Pull complete 
732dff0ce31a: Pull complete 
30aafac480a0: Pull complete 
e7e8343a4580: Pull complete 
Digest: sha256:ecd9c4102c4703dbda117d0829d08d0a309f7c492a537281638258718b472cb6
Status: Downloaded newer image for k8sn01:808/lsnow8624/mynginx:v1.0
[root@k8sn01 harbor]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
k8sn01:808/lsnow8624/mynginx    v1.0                a8c045d21e0a        About an hour ago   432MB
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐