> For the complete documentation index, see [llms.txt](https://pshizhsysu.gitbook.io/harbor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pshizhsysu.gitbook.io/harbor/an-zhuang.md).

# 安装

## 目录

* 安装harbor
* 高可用方案
* 注意事项

## 一、安装habror

### 1.1 安装docker

略

### 1.2 安装docker-compose

#### 1.2.1 安装

从TeleDCOS包中获取文件**docker-compose-1.9.0**，拷贝到主机的/usr/bin目录下，并重命名为 **docker-compose**，然后添加可执行权限

#### 1.2.2 验证

执行以下命令，查看是否能显示版本信息

```
$ docker-compose -v
docker-compose version 1.9.0, build 2585387
```

### 1.3 安装harbor

#### 1.3.1 下载

从TeleDCOS包中获取harbor安装包，文件名为 harbor-offline-installer-0.5.0.tgz，大概303M

#### 1.3.2 解压

把下载好的压缩包放在主机的 \~ 目录下，然后解压。解压后在当前目录下就会出现一个harbor文件夹。

#### 1.3.3 修改配置

* 进入到harbor根目录

```
cd ~/harbor
```

* 修改harbor.cfg

> 找到如下一行

```
hostname = reg.mydomain.com
```

修改为

```
x.x.x.x:8021
```

其中x.x.x.x为harbor所在宿主机的物理IP地址，8021为harbor对外的端口（可以自定义）

* 修改docker-compose.yml

> 找到如下一段配置

```
registry:
  image: library/registry:2.5.0
```

修改为

```
registry:
  image: registry:2.5.0
```

> 找到如下一段配置

```
proxy:
  image: nginx:1.11.5
  container_name: nginx
  restart: always
  volumes:
    - ./common/config/nginx:/etc/nginx
  ports:
    - 80:80
    - 443:443
  depends_on:
```

修改“- 80:80”这一行，删除“- 443:443”这一行，最终修改为

```
proxy:
  image: nginx:1.11.5
  container_name: nginx
  restart: always
  volumes:
    - ./common/config/nginx:/etc/nginx
  ports:
    - 8021:80
  depends_on:
```

**注意：** “- 8021:80” 中的8021要与 harbor.cfg中的 “hostname: x.x.x.x:8021” 中端口保持一致

* 修改 `common/templates/registry/config.yml`（可选）

如果想去掉认证（即不用docker login就能直接push与pull镜像），删除如下几行

```
auth:
  token:
    issuer: registry-token-issuer
    realm: $ui_url/service/token
    rootcertbundle: /etc/registry/root.crt
    service: token-service
```

* 更改数据目录（可选）

harbor的数据默认存储在`/data`目录下，如果要更改为其他目录比如`/dcos/data/harbor`，则把`docker-compose.yml`文件中的如下四行进行修改

```
第16行： '- /data/registry:/storage' -> '- /dcos/data/harbor/registry:/storage' 
第34行： '- /data/database:/var/lib/mysql' -> '- /dcos/data/harbor/database:/var/lib/mysql'
第53行： '- /data:/harbor_storage' -> '- /dcos/data/harbor:/harbor_storage'
第68行： '- /data/job_logs:/var/log/jobs' -> '- /dcos/data/harbor/job_logs:/var/log/jobs'
```

上述四行内容的修改，可以用如下命令达到

```
$ sudo sed -i "s#- /data#- /dcos/data/harbor#g" docker-compose.yml
```

修改了docker-compose.yml文件，我们还需要修改`prepare`脚本文件，更改第63行，把`default='/data/'`修改为`default='/dcos/data/harbor/'`

#### 1.3.4 启动

执行如下命令启动harbor

```
sudo chmod u+x ./install.sh
sudo ./install.sh
```

启动过程大约需求一两分钟，过程中且不断会有日志输出，耐心等待harbor启动完成

#### 1.3.5 验证

在harbor目录下执行以下命令，如果六个容器均为Up状态，说明启动成功

```
$ docker-compose ps
      Name                     Command               State               Ports             
------------------------------------------------------------------------------------------
harbor-db           docker-entrypoint.sh mysqld      Up      0.0.0.0:8306->3306/tcp        
harbor-jobservice   /harbor/harbor_jobservice        Up                                    
harbor-log          /bin/sh -c crond && rm -f  ...   Up      0.0.0.0:1514->514/tcp         
harbor-ui           /harbor/harbor_ui                Up                                    
nginx               nginx -g daemon off;             Up      443/tcp, 0.0.0.0:8021->80/tcp 
registry            /entrypoint.sh serve /etc/ ...   Up      5000/tcp
```

#### 1.3.6 访问

在浏览器中输入 x.x.x.x:8021，初始用户名与密码为 `admin/Harbor12345`

## 二、高可用方案

目前harbor的高可用是通过project之间的复制策略来保证的。假设我们在10.142.232.161和10.142.232.162上都装好了harbor，接下来，我们来介绍一下如何在两个harbor的project之间创建复制策略

### 2.1 创建用户

我们先在两个harbor实例上都创建一个相同的用户，假设为user1（注意两个harbor上都要创建用户，用户名相同，密码相同）

创建用户的操作要使用admin用户

![](/files/-MA1FX0FDQLGIgh2wnlk)

### 2.2 创建target

* 10.142.232.162

在10.142.232.162的harbor上创建一个target，指向10.142.232.161的harbor。

创建target的操作要使用admin用户

![](/files/-MA1FX0J3z7VQxbhWsFQ)

![](/files/-MA1FX0KIPAO7Kfn-2F4)

* 10.142.232.161

与上面的操作类似

### 2.3 创建project

使用user1用户登录，创建项目 project1user1（两个harbor实例上都要创建）

![](/files/-MA1FX0LuE1cTjAkGsqG)

### 2.4 创建复制策略

* 10.142.232.162

使用admin用户登录，为项目project1user1创建复制策略

![](/files/-MA1FX0MrqxHasMZkLMD)

![](/files/-MA1FX0N9Qw-QzlWon-t)

* 10.142.232.161

与上面的操作类似

### 2.5 验证

161的project1user1中的镜像会自动同步到162上，162的project1user1中的镜像也会同步到161，从而实现了harbor间镜像的同步。可以push镜像进行验证

### 2.6 keepalived

由于两个harbor实例的IP不一样，所以我们还需要在两台主机上安装keepalived，使两台主机绑定一个vip（假设为10.142.232.160），当其中一台主机挂了，vip会漂移到另一台主机上，这样保证了harbor的高可用。

**注意：** 我们使用的harbor的地址应该为vip，即 10.142.232.160:8021

## 三、注意事项

* dcos管理平台会在某个harbor实例上创建好用户与项目，所以我们创建用户与项目时只需要在其中某一个harbor实例上操作就可以


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pshizhsysu.gitbook.io/harbor/an-zhuang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
