Set up the CRI Proxy
Set up the proxy for the CRI(Container Runtime Interface) will solve the connection problem in most situations. Considering most of people use docker to pull the container images and docker has containerd which is also a CRI, we can create configuration files and add some parameters.
Create the docker configuration file:
1 | mkdir -p /etc/systemd/system/docker.service.d |
Modify the http-proxy.conf file:
1 | [Service] |
Create the containerd configuration file:
1 | mkdir -p /etc/systemd/system/containerd.service.d |
Modify the http-proxy.conf file:
1 | [Service] |
Set up Private Registry
For security concerns, it will be better to create a private registry with authentication. Here I’m going to use the basic authentication to achieve that.
Create a password file with one entry for the user testuser, with password testpassword:
1 | mkdir auth |
We’re going to use the TLS channel to access the registry server, so we have to generate the certificate ourselves. Otherwise you can use the insecure-registries parameter to bypass the HTTPS warning. Edit the daemon.json file, whose default location is /etc/docker/daemon.json on Linux:
1 | { |
Let’s create the certificate and key file:
1 | mkdir certs && cd ./certs |
Install the certificate for your local machine, but it seems that docker will not refer to the Linux certificates when it connects to the registry.
1 | cp certs/certificate.pem /usr/local/share/ca-certificates/registry-certificate.crt |
Now we’ve install the self-signed certificate properly. Note that if you want to enable the deleting switch which allows you to delete the images on the registry, you need to create a yaml file and edit it like this:
1 | version: 0.1 |
Let’s start the registry with basic authentication:
1 | docker run -d \ |
Connection test:
1 | curl -XGET https://localhost:6000/v2/_catalog -u reporter:58fd24d4311a742d13464373398ff3d9 |
Maybe you have to modify the image pulling credentials in your kubernetes yaml file.
Create a secret named regcred:
1 | kubectl create secret docker-registry regcred \ |
And add the credential in your pod:
1 | apiVersion: v1 |
If you are using a virtual machine manager for kubernetes like kubevirt, it will be a little different:
1 | volumes: |
Push Images
Since we have enabled the basic authentication, you have to execute the login command first and foremost.
1 | docker login http://172.20.1.150:6000 |
After entering your username and password, the ~/.docker/config.json file will be generated automatically.
Then you can push your images to the registry by a few commands:
1 | docker image tag rhel-httpd:latest registry-host:6000/myadmin/rhel-httpd:latest |
Image Deleting
Commonly we get all the images kept on the registry by using the v2 api:
1 | curl -XGET https://localhost:6000/v2/_catalog -u name:password --insecure |
We can also list all the images along with their tags with a python script:
1 | #-*- coding:utf-8 -*- |
Run this script:
1 | python3 get_image.py |
We can use the v2 api to delete a specific image:
1 | curl -XDELETE https://localhost:6000/v2/<name>/manifests/<reference> -u name:password |
For example:
1 | curl -XDELETE https://localhost:6000/v2/<name>/manifests/sha256:60f2883fbfca71ff7740a6eca7bd8bd466988031dcf55093bb8ff2b26f2c5479 -u name:password --insecure |
The name is the image name you want to delete, and the reference is the digest of the targeted image. To get the digest of a image with docker:
1 | docker image ls --digests image_name |
Or retrieve the digest from the registry server with command:
1 | curl -X GET --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I |
For example:
1 | curl -XGET --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I https://localhost:6000/v2/<name>/manifests/latest -u name:password --insecure |
The response will be like this:
1 | HTTP/2 200 |
Finally, to make the deleting action take effect, you need to get into the container and execute the garbage-collection command:
1 | registry garbage-collect /etc/docker/registry/config.yml |
Then you can check the storage to verify:
1 | du -chs /var/lib/registry/ |
Using the Cloud Service Provider’s Registry
References
https://docs.docker.com/registry/insecure/