2024-11-02 04:14:44 +01:00
# Create a private registry for containers with Ansible
2024-11-02 04:05:28 +01:00
2024-11-02 04:14:44 +01:00
Ref: < https: / / www . redhat . com / sysadmin / simple-container-registry >
2024-11-02 04:05:28 +01:00
2024-11-02 04:21:39 +01:00
## Run the playbook
2024-11-02 04:05:28 +01:00
2024-11-02 04:21:39 +01:00
```bash
ansible-playbook container_registry.yaml
```
## Setup
2024-11-02 04:05:28 +01:00
Make sure the local CA (domain.crt) is accepted on all the machines that will commit the images AND on all the target systems (the machines where the images will be deployed).
Manually, for Debian:
2024-11-02 04:21:39 +01:00
```bash
2024-11-02 04:05:28 +01:00
HOST=k3s
REGISTRY=tiptop:5000
ssh root@$HOST mkdir -p /etc/containers/certs.d/$REGISTRY
scp certs/domain.crt root@$HOST:/etc/containers/certs.d/$REGISTRY/
2024-11-02 04:21:39 +01:00
```
### Kubernetes
2024-11-02 04:05:28 +01:00
2024-11-02 04:21:39 +01:00
Add the credential to the kubernetes cluster:
2024-11-02 04:05:28 +01:00
2024-11-02 04:21:39 +01:00
```bash
2024-11-02 04:05:28 +01:00
kubectl create secret docker-registry regcred --docker-server=tiptop:5000 --docker-username=admin --docker-password=admin -n default
2024-11-02 04:21:39 +01:00
```
2024-11-02 04:05:28 +01:00
2024-11-02 04:21:39 +01:00
## Use
2024-11-02 04:05:28 +01:00
To push to the registry:
2024-11-02 04:21:39 +01:00
```bash
podman push < image name > docker://< host name > :5000/< image name >
```
2024-11-02 04:05:28 +01:00
2024-11-02 04:14:44 +01:00
To use it in Kubernetes, see < https: / / kubernetes . io / docs / tasks / configure-pod-container / pull-image-private-registry / >
2024-11-02 19:52:53 +01:00
## Maintenance
### Remove images / tags
In short:
* login to the registry container
* delete the directories keeping the metadata of the images and tags
* run command *registry garbage_collect* to delete the unreferenced blobs
In practice:
```bash
## Login to the machine with the dedicated user
ssh registry@tiptop
## Run a shell in a registry container
# podman run -it --rm myregistry sh # if the registry is not started
2024-11-03 05:16:16 +01:00
podman exec -it myregistry sh
2024-11-02 19:52:53 +01:00
# List all images and their tags
2024-11-03 05:16:16 +01:00
ls -ldrt /var/lib/registry/docker/registry/v2/repositories/*/_manifests/tags/*
2024-11-02 19:52:53 +01:00
## To remove an image with all its tags:
rm -rf /var/lib/registry/docker/registry/v2/repositories/image_to_be_deleted
## To remove only a tag, eg "latest":
rm -rf /var/lib/registry/docker/registry/v2/repositories/image_to_be_deleted/_manifests/tags/latest
## Clean up the unreferenced blobs
registry garbage-collect -m /etc/docker/registry/config.yml
```