关于docker:Docker-笔记-将-Harbor-设置为系统服务

11次阅读

共计 7173 个字符,预计需要花费 18 分钟才能阅读完成。

编辑文件 /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
正文完
 0