Docker 笔记 - 将 Harbor 设置为系统服务

编辑文件 /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 命令在任意路径下都能使用】

终身学习,向上生长~

7 声望
5 粉丝
0 条评论
推荐阅读
K8S 笔记 - Pod 的 DNS 策略中的“坑”
ClusterFirst: 与配置的集群域后缀不匹配的任何 DNS 查询(例如 "www.kubernetes.io") 都将转发到从节点继承的上游名称服务器。集群管理员可能配置了额外的存根域和上游 DNS 服务器。

逆熵流1阅读 462

张晋涛:我的 2022 总结
大家好,我是张晋涛。2022 年已经结束,我每年都会惯例的做个小回顾,今年因为阳了在恢复身体,一直拖到了今天才写。生活在 2022 年初做回顾的时候,觉得 2021 是魔幻的一年,但现在看来 2022 年其实更加魔幻。一...

张晋涛6阅读 1.1k评论 2

封面图
利用Docker部署管理LDAP及其初次使用
前言:本周主要写了gitlabWebhook转github的项目,总体上没有遇到什么大问题,这周接触到了LDAP,于是就花时间实际操作了解了一下。

李明5阅读 1.3k

Docker学习:Image的本地存储结构
在使用Docker时候,针对镜像的操作一般就是docker pull,docker build,docker commit(刚开始接触Docker的时候,还不会Dockerfile,经常使用这个命令,但是经历了一次血的教训,已经放弃这个命令很久)这些操作...

backbp4阅读 10.1k评论 3

使用docker快速搭建xssPlatform测试平台实践
笔者之前给一些开发团队多次做Web安全开发培训,为了让培训的学员能够理解XSS原理和XSS的危害,将xssPlatform进行了更新,之前一直放在GitHub中;发现关注的人越来越多,很多人在安装的过程中遇到问题不知道怎么...

汤青松1阅读 26.3k

将node.js 应用构建成docker镜像
本文内容:将node.js 应用构建成dokcer镜像, 并上传到阿里云创建 Node.js 应用1.首先,创建一个 package.json 文件,描述你应用程序以及需要的依赖: {代码...} 2.运行 npm install这会自动生成一个 package-lock...

weiweiyi3阅读 749

突破难关:Docker镜像和容器的区别以及构建的最佳实践
Docker 可谓是开启了容器化技术的新时代,现在无论大中小公司基本上都对容器化技术有不同程度的尝试,或是已经进行了大量容器化的改造。伴随着 Kubernetes 和 Cloud Native 等技术和理念的普及,也大大增加了业务...

张晋涛4阅读 478

封面图

终身学习,向上生长~

7 声望
5 粉丝
宣传栏