Harbor IDEA 快速指引


網上安裝的教程比較多,理清思路之后,自己的安裝過程總結一下,以備后續參考。

參考Harbor官網教程 (Centos 7.5)

1.安裝前必備 :On a Linux host: docker 17.06.0-ce+ and docker-compose 1.18.0+ . (自行安裝好即可)

2. Harbor 下載的 harbor-offline-installer-v2.0.1.tgz 離線安裝包,從 https://github.com/goharbor/harbor/releases 下載。

3. 解壓開來,配置文件  harbor.yml.tmpl  復制為 harbor.yml  根據自己的需求修改即可(我只修改了主機名,證書,存放目錄 )。

 1 # The IP address or hostname to access admin UI and registry service.
 2 # DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
 3 hostname: harbor.grape.com
 4 
 5 # http related config
 6 http:
 7   # port for http, default is 80. If https enabled, this port will redirect to https port
 8   port: 80
 9 
10 # https related config
11 https:
12   # https port for harbor, default is 443
13   port: 443
14   # The path of cert and key files for nginx
15   certificate: /opt/cert/harbor.grape.com.crt
16   private_key: /opt/cert/harbor.grape.com.key
17 
18 # The default data volume
19 data_volume: /home/harbor_data

證書的制作是參考的 https://www.cnblogs.com/sanduzxcvbnm/p/11956347.html 的腳本 ,/opt/cert 目錄沒有的話,需要先創建。

 1 #!/bin/bash
 2 
 3 # 配置harbor證書
 4 
 5 cd /opt/cert
 6 
 7 openssl genrsa -out ca.key 4096
 8 openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.grape.com" -key ca.key -out ca.crt
 9 openssl genrsa -out harbor.grape.com.key 4096
10 openssl req -sha512 -new -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.grape.com" -key harbor.grape.com.key -out harbor.grape.com.csr
11 
12 cat > v3.ext <<-EOF
13 authorityKeyIdentifier=keyid,issuer
14 basicConstraints=CA:FALSE
15 keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
16 extendedKeyUsage = serverAuth
17 subjectAltName = @alt_names
18 
19 [alt_names]
20 DNS.1=harbor.grape.com
21 IP.1 = 192.168.111.9
22 IP.2 = 10.0.0.40
23 EOF
24 
25 openssl x509 -req -sha512 -days 3650 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in harbor.grape.com.csr -out harbor.grape.com.crt
26     
27 openssl x509 -inform PEM -in harbor.grape.com.crt -out harbor.grape.com.cert
View Code

其中注意兩點,dns 我配置兩個地址(內網和外網地址)用於做NAT或者端口映射,記得看過有個帖子說沒有找到解決辦法(此處是我的解決辦法,自簽證書,dns以及多個IP)

4.證書放到指定的目錄,以及docker compose 啟動等等就不細說了。啟動之后,其他機器 docker login 進行驗證,一般來說,正常的是 x509: certificate signed by unknown authority 錯誤 ???

linux 需要增加自建的ca證書到docker的信任,創建 /etc/docker/certs.d/harbor.grape.com 文件夾,復制ca.crt 到此目錄重啟docker 服務;

window 10 相對簡單一些,配置文件中增加 "insecure-registries": ["https://harbor.grape.com"] 即可

還有一點差點忘記了,所有訪問 https://harbor.grape.com 需要增加到 hosts文件之中。

 

IDEA中的使用過程

其實使用插件即可, com.spotify 的 dockerfile-maven-plugin 參見 https://github.com/spotify/dockerfile-maven

隨便建一個spring boot 測試程序,pom文件如下 :

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5     <groupId>com.example</groupId>
  6     <artifactId>demo</artifactId>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <name>demo</name>
  9     <description>Demo project for Spring Boot</description>
 10 
 11     <properties>
 12         <java.version>1.8</java.version>
 13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 14         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 15         <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
 16         <!--docker私服地址-->
 17         <docker.repository>harbor.grape.com</docker.repository>
 18         <docker.image.prefix>blade</docker.image.prefix>
 19     </properties>
 20 
 21     <dependencies>
 22         <dependency>
 23             <groupId>org.springframework.boot</groupId>
 24             <artifactId>spring-boot-starter-web</artifactId>
 25         </dependency>
 26 
 27         <dependency>
 28             <groupId>org.springframework.boot</groupId>
 29             <artifactId>spring-boot-starter-test</artifactId>
 30             <scope>test</scope>
 31             <exclusions>
 32                 <exclusion>
 33                     <groupId>org.junit.vintage</groupId>
 34                     <artifactId>junit-vintage-engine</artifactId>
 35                 </exclusion>
 36             </exclusions>
 37         </dependency>
 38     </dependencies>
 39 
 40     <dependencyManagement>
 41         <dependencies>
 42             <dependency>
 43                 <groupId>org.springframework.boot</groupId>
 44                 <artifactId>spring-boot-dependencies</artifactId>
 45                 <version>${spring-boot.version}</version>
 46                 <type>pom</type>
 47                 <scope>import</scope>
 48             </dependency>
 49         </dependencies>
 50     </dependencyManagement>
 51 
 52     <build>
 53         <plugins>
 54             <plugin>
 55                 <groupId>org.apache.maven.plugins</groupId>
 56                 <artifactId>maven-compiler-plugin</artifactId>
 57                 <configuration>
 58                     <source>1.8</source>
 59                     <target>1.8</target>
 60                     <encoding>UTF-8</encoding>
 61                 </configuration>
 62             </plugin>
 63             <plugin>
 64                 <groupId>org.springframework.boot</groupId>
 65                 <artifactId>spring-boot-maven-plugin</artifactId>
 66                 <!--需要注意,要么打包后在容器中啟動不起來-->
 67                 <executions>
 68                     <execution>
 69                         <goals>
 70                             <goal>repackage</goal>
 71                         </goals>
 72                     </execution>
 73                 </executions>
 74                 <configuration>
 75                     <includeSystemScope>true</includeSystemScope>
 76                 </configuration>
 77             </plugin>
 78             <plugin>
 79                 <groupId>org.apache.maven.plugins</groupId>
 80                 <artifactId>maven-deploy-plugin</artifactId>
 81                 <configuration>
 82                     <skip>true</skip>
 83                 </configuration>
 84             </plugin>
 85             <plugin>
 86                 <groupId>com.spotify</groupId>
 87                 <artifactId>dockerfile-maven-plugin</artifactId>
 88                 <version>1.4.13</version>
 89                 <executions>
 90                     <execution>
 91                         <id>default</id>
 92                         <goals>
 93                             <goal>build</goal>
 94                             <goal>push</goal>
 95                         </goals>
 96                     </execution>
 97                 </executions>
 98                 <configuration>
 99                     <!--<username>***</username>
100                     <password>*******</password>-->
101                     <repository>${docker.repository}/${docker.image.prefix}/${project.artifactId}</repository>
102                     <tag>latest</tag>
103                     <buildArgs>
104                         <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
105                     </buildArgs>
106                     <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
107                 </configuration>
108             </plugin>
109         </plugins>
110     </build>
111 
112 </project>
113             
View Code
值得注意的是 blade 是harbor先建立的項目,Dockerfile文件如下:
1 FROM java:8
2 ARG JAR_FILE
3 ADD ${JAR_FILE} app.jar
4 ENTRYPOINT ["java", "-jar", "/app.jar"]
View Code

 

 package -->  dockerfile:build (默認是連接到localhost:2375 ,也就是本地docker),我的系統是 winserver 2019 ,win10 2004 開啟wsl2 更酷。

 

 

 

 以上已經推動到本地的docker中了,然后執行 dockerfile:push ,推動到harbor私有倉庫。

 

 基本上就可以完成,其中有許多細節和知識點需要自己補充。

k8s 中拉取鏡像文件即可運行

 

 

計划將 idea -> gitlab  -> jenkins -> harbor -> k8s 串聯起來。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM