1

编辑文件 /lib/systemd/system/harbor.service

[Unit]
Description=Harbor
After=docker.service systemd-networkd.service systemd-resolved.service
Requires=docker.service
Documentation=http://github.com/vmware/harbor

[Service]
Type=simple
Restart=on-failure
RestartSec=5
ExecStart=/usr/local/bin/docker-compose -f /data/soft/harbor/docker-compose.yml up
ExecStop =/usr/local/bin/docker-compose -f /data/soft/harbor/docker-compose.yml down

[Install]
WantedBy=multi-user.target

设置 harbor 开机自动启动:
systemctl enable harbor

查看 harbor 服务状态,运行中:

[root@k8s-master harbor]# systemctl status harbor
● harbor.service - Harbor
Loaded: loaded (/usr/lib/systemd/system/harbor.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2022-06-05 16:13:00 CST; 428ms ago
Docs: http://github.com/vmware/harbor
Main PID: 61837 (docker-compose)
Tasks: 1
Memory: 49.0M
CGroup: /system.slice/harbor.service
└─61837 /usr/bin/python3 /usr/local/bin/docker-compose -f /data/soft/harbor/docker-compose.yml up
6月 05 16:13:00 k8s-master systemd[1]: Started Harbor.

使用 docker-compose 先查看 harbor 容器的状态,都是 Up (healthy),表示正常运行中:

[root@k8s-master harbor]# docker-compose ps -a
Name                     Command                  State                        Ports
harbor-core         /harbor/harbor_core              Up (healthy)
harbor-db           /docker-entrypoint.sh            Up (healthy)   5432/tcp
harbor-jobservice   /harbor/harbor_jobservice  ...   Up (healthy)
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)   127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Up (healthy)   8080/tcp
nginx               nginx -g daemon off;             Up (healthy)   0.0.0.0:8888->8080/tcp,:::8888->8080/tcp
redis               redis-server /etc/redis.conf     Up (healthy)   6379/tcp
registry            /home/harbor/entrypoint.sh       Up (healthy)   5000/tcp
registryctl         /home/harbor/start.sh            Up (healthy)

关闭 harbor 服务:

[root@k8s-master harbor]# systemctl stop harbor
[root@k8s-master harbor]# systemctl status harbor
● harbor.service - Harbor
Loaded: loaded (/usr/lib/systemd/system/harbor.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 日 2022-06-05 16:13:32 CST; 14s ago
Docs: http://github.com/vmware/harbor
Process: 62147 ExecStop=/usr/local/bin/docker-compose -f /data/soft/harbor/docker-compose.yml down (code=exited, status=0/SUCCESS)
Process: 61837 ExecStart=/usr/local/bin/docker-compose -f /data/soft/harbor/docker-compose.yml up (code=exited, status=0/SUCCESS)
Main PID: 61837 (code=exited, status=0/SUCCESS)
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 172.18.0.9 - - [10/Feb/2022:10:46:38 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 172.18.0.9 - - [10/Feb/2022:10:46:48 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 127.0.0.1 - - [10/Feb/2022:10:46:57 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 172.18.0.9 - - [10/Feb/2022:10:46:58 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 172.18.0.9 - - [10/Feb/2022:10:47:08 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 172.18.0.9 - - [10/Feb/2022:10:47:18 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 127.0.0.1 - - [10/Feb/2022:10:47:27 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 172.18.0.9 - - [10/Feb/2022:10:47:28 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:16 k8s-master docker-compose[61837]: registryctl    | 172.18.0.9 - - [10/Feb/2022:10:47:38 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 16:13:32 k8s-master systemd[1]: Stopped Harbor.

使用 docker-compose 查看 harbor 容器的状态,看不到任何容器,连关闭中的容器也没有:

[root@k8s-master harbor]# docker-compose ps -a
Name   Command   State   Ports
------------------------------

再次启动 harbor 服务:
systemctl start harbor

使用 docker-compose 查看 harbor 容器的状态,看到正在启动中的容器:

[root@k8s-master harbor]# docker-compose ps
Name                     Command                       State                            Ports
harbor-core         /harbor/harbor_core              Up (health: starting)
harbor-db           /docker-entrypoint.sh            Up (health: starting)   5432/tcp
harbor-jobservice   /harbor/harbor_jobservice  ...   Up (health: starting)
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (health: starting)   127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Up (health: starting)   8080/tcp
nginx               nginx -g daemon off;             Up (health: starting)   0.0.0.0:8888->8080/tcp,:::8888->8080/tcp
redis               redis-server /etc/redis.conf     Up (health: starting)   6379/tcp
registry            /home/harbor/entrypoint.sh       Up (health: starting)   5000/tcp
registryctl         /home/harbor/start.sh            Up (health: starting)

过一会重新查看 harbor 服务状态和 harbor 容器状态,已经全部正常:

[root@k8s-master harbor]# systemctl status harbor
● harbor.service - Harbor
Loaded: loaded (/usr/lib/systemd/system/harbor.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2022-06-05 16:26:21 CST; 56min ago
Docs: http://github.com/vmware/harbor
Process: 71339 ExecStop=/usr/local/bin/docker-compose -f /data/soft/harbor/docker-compose.yml down (code=exited, status=0/SUCCESS)
Main PID: 71752 (docker-compose)
Tasks: 11
Memory: 32.0M
CGroup: /system.slice/harbor.service
└─71752 /usr/bin/python3 /usr/local/bin/docker-compose -f /data/soft/harbor/docker-compose.yml up
6月 05 17:22:04 k8s-master docker-compose[71752]: registry       | 127.0.0.1 - - [05/Jun/2022:09:22:04 +0000] "GET / HTTP/1.1" 200 0 "" "curl/7.78.0"
6月 05 17:22:04 k8s-master docker-compose[71752]: harbor-portal  | 172.19.0.10 - - [05/Jun/2022:09:22:04 +0000] "GET / HTTP/1.1" 200 718 "-" "curl/7.78.0"
6月 05 17:22:04 k8s-master docker-compose[71752]: nginx          | 127.0.0.1 - "GET / HTTP/1.1" 200 718 "-" "curl/7.78.0" 0.004 0.004 .
6月 05 17:22:04 k8s-master docker-compose[71752]: harbor-core    | 2022/06/05 09:22:04.879 [D] [server.go:2843]  |      127.0.0.1| 200 |  14.855799ms|  .../api/ping
6月 05 17:22:14 k8s-master docker-compose[71752]: registry       | 172.19.0.8 - - [05/Jun/2022:09:22:14 +0000] "GET / HTTP/1.1" 200 0 "" "Go-http-client/1.1"
6月 05 17:22:14 k8s-master docker-compose[71752]: harbor-portal  | 172.19.0.8 - - [05/Jun/2022:09:22:14 +0000] "GET / HTTP/1.1" 200 718 "-" "Go-http-client/1.1"
6月 05 17:22:14 k8s-master docker-compose[71752]: registryctl    | 172.19.0.8 - - [05/Jun/2022:09:22:14 +0000] "GET /api/health HTTP/1.1" 200 9
6月 05 17:22:24 k8s-master docker-compose[71752]: registry       | 172.19.0.8 - - [05/Jun/2022:09:22:24 +0000] "GET / HTTP/1.1" 200 0 "" "Go-http-client/1.1"
6月 05 17:22:24 k8s-master docker-compose[71752]: harbor-portal  | 172.19.0.8 - - [05/Jun/2022:09:22:24 +0000] "GET / HTTP/1.1" 200 718 "-" "Go-http-client/1.1"
6月 05 17:22:24 k8s-master docker-compose[71752]: registryctl    | 172.19.0.8 - - [05/Jun/2022:09:22:24 +0000] "GET /api/health HTTP/1.1" 200 9
Hint: Some lines were ellipsized, use -l to show in full.
[root@k8s-master harbor]# docker-compose  ps
      Name                     Command                  State                        Ports                  
------------------------------------------------------------------------------------------------------------
harbor-core         /harbor/harbor_core              Up (healthy)                                           
harbor-db           /docker-entrypoint.sh            Up (healthy)   5432/tcp                                
harbor-jobservice   /harbor/harbor_jobservice  ...   Up (healthy)                                           
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)   127.0.0.1:1514->10514/tcp               
harbor-portal       nginx -g daemon off;             Up (healthy)   8080/tcp                                
nginx               nginx -g daemon off;             Up (healthy)   0.0.0.0:8888->8080/tcp,:::8888->8080/tcp
redis               redis-server /etc/redis.conf     Up (healthy)   6379/tcp                                
registry            /home/harbor/entrypoint.sh       Up (healthy)   5000/tcp                                
registryctl         /home/harbor/start.sh            Up (healthy)

通过这种操作可以得出结论,此种方式的本质是删除 harbor 组件的容器,重新创建。跟使用 docker-compose 重启容器的方式完全不同,后者只是重启容器,不是删除重建

所以本文重启 harbor 的方式,需谨慎使用!!!

通过对比 harbor 重启前后容器的 ID 也能进一步确定这个结论:
重启前的容器 ID:

[root@k8s-master harbor]# docker ps | grep harbor-core
5469180f3cd1   goharbor/harbor-core:v1.10.10                       "/harbor/harbor_core"    8 minutes ago        Up 8 minutes (healthy)                                                 harbor-core

重启后的容器 ID:

[root@k8s-master harbor]# docker ps | grep harbor-core
67cb29a68e1c   goharbor/harbor-core:v1.10.10                       "/harbor/harbor_core"    22 minutes ago   Up 22 minutes (healthy)                                                harbor-core

如果需要 harbor 的容器只是重启,而不是重新创建,在服务脚本 /lib/systemd/system/harbor.service 中就不能使用 docker-compose 的 up 和 down 这两个命令,而是要是改为 start 和 stop 。

如果希望很方便的重启 harbor ,还可以使用另外一种方法,请参考文章:
https://segmentfault.com/a/11...
【Docker 笔记 - 让 docker-compose 命令在任意路径下都能使用】


逆熵流
10 声望5 粉丝

终身学习,向上生长~