|
| 1 | +# How to use docker-registry-proxy with kops |
| 2 | + |
| 3 | +## Install docker-registry-proxy |
| 4 | + |
| 5 | +For running docker-registry-proxy with kops you will need to run it outside the cluster you want to configure, you can either use and EC2 instance and run: |
| 6 | + |
| 7 | +```bash |
| 8 | +docker run --rm --name docker_registry_proxy -it \ |
| 9 | + -p 0.0.0.0:3128:3128 -e ENABLE_MANIFEST_CACHE=true \ |
| 10 | + -v $(pwd)/docker_mirror_cache:/docker_mirror_cache \ |
| 11 | + -v $(pwd)/docker_mirror_certs:/ca \ |
| 12 | + rpardini/docker-registry-proxy:0.6.0 |
| 13 | +``` |
| 14 | + |
| 15 | +or you can run it from another cluster, maybe a management/observability one with provided yaml, in this case, you will need to change the following lines: |
| 16 | + |
| 17 | +``` |
| 18 | + annotations: |
| 19 | + external-dns.alpha.kubernetes.io/hostname: docker-registry-proxy.<your_domain> |
| 20 | + service.beta.kubernetes.io/aws-load-balancer-internal: "true" |
| 21 | +``` |
| 22 | + |
| 23 | +with the correct domain name, so then you can reference the proxy as `http://docker-registry-proxy.<your_domain>:3128` |
| 24 | + |
| 25 | +## Test the connection to the proxy |
| 26 | + |
| 27 | +A simple curl should return: |
| 28 | + |
| 29 | +``` |
| 30 | +❯ curl docker-registry-proxy.<your_domain>:3128 |
| 31 | +docker-registry-proxy: The docker caching proxy is working!% |
| 32 | +``` |
| 33 | + |
| 34 | +## Configure kops to use the proxy |
| 35 | + |
| 36 | +Kops has the option to configure a cluster wide proxy, as explained [here](https://github.com/kubernetes/kops/blob/master/docs/http_proxy.md) but this wont work, as nodeup will fail to download the images, what you need is to use `additionalUserData`, which is part of the instance groups configuration. |
| 37 | + |
| 38 | +So consider a node configuration like this one: |
| 39 | + |
| 40 | +``` |
| 41 | +apiVersion: kops.k8s.io/v1alpha2 |
| 42 | +kind: InstanceGroup |
| 43 | +metadata: |
| 44 | + labels: |
| 45 | + kops.k8s.io/cluster: spot.k8s.local |
| 46 | + name: spotgroup |
| 47 | +spec: |
| 48 | + image: 099720109477/ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-20200528 |
| 49 | + machineType: c3.xlarge |
| 50 | + maxSize: 15 |
| 51 | + minSize: 2 |
| 52 | + mixedInstancesPolicy: |
| 53 | + instances: |
| 54 | + - c3.xlarge |
| 55 | + - c4.xlarge |
| 56 | + - c5.xlarge |
| 57 | + - c5a.xlarge |
| 58 | + onDemandAboveBase: 0 |
| 59 | + onDemandBase: 0 |
| 60 | + spotAllocationStrategy: capacity-optimized |
| 61 | + nodeLabels: |
| 62 | + kops.k8s.io/instancegroup: spotgroup |
| 63 | + role: Node |
| 64 | + subnets: |
| 65 | + - us-east-1a |
| 66 | + - us-east-1b |
| 67 | + - us-east-1c |
| 68 | +``` |
| 69 | + |
| 70 | +you will need to add the following: |
| 71 | + |
| 72 | +``` |
| 73 | + additionalUserData: |
| 74 | + - name: docker-registry-proxy.sh |
| 75 | + type: text/x-shellscript |
| 76 | + content: | |
| 77 | + #!/bin/sh |
| 78 | +
|
| 79 | + # Add environment vars pointing Docker to use the proxy |
| 80 | + # https://docs.docker.com/config/daemon/systemd/#httphttps-proxy |
| 81 | +
|
| 82 | + mkdir -p /etc/systemd/system/docker.service.d |
| 83 | + cat << EOD > /etc/systemd/system/docker.service.d/http-proxy.conf |
| 84 | + [Service] |
| 85 | + Environment="HTTP_PROXY=http://docker-registry-proxy.<your_domain>:3128/" |
| 86 | + Environment="HTTPS_PROXY=http://docker-registry-proxy.<your_domain>:3128/" |
| 87 | + EOD |
| 88 | +
|
| 89 | + # Get the CA certificate from the proxy and make it a trusted root. |
| 90 | + curl http://docker-registry-proxy.<your_domain>:3128/ca.crt > /usr/share/ca-certificates/docker_registry_proxy.crt |
| 91 | + echo "docker_registry_proxy.crt" >> /etc/ca-certificates.conf |
| 92 | + update-ca-certificates --fresh |
| 93 | +
|
| 94 | + # Reload systemd |
| 95 | + systemctl daemon-reload |
| 96 | +
|
| 97 | + # Restart dockerd |
| 98 | + systemctl restart docker.service |
| 99 | +``` |
| 100 | + |
| 101 | +so the final InstanceGroup will look like this: |
| 102 | + |
| 103 | +``` |
| 104 | +apiVersion: kops.k8s.io/v1alpha2 |
| 105 | +kind: InstanceGroup |
| 106 | +metadata: |
| 107 | + labels: |
| 108 | + kops.k8s.io/cluster: spot.k8s.local |
| 109 | + name: spotgroup |
| 110 | +spec: |
| 111 | + additionalUserData: |
| 112 | + - name: docker-registry-proxy.sh |
| 113 | + type: text/x-shellscript |
| 114 | + content: | |
| 115 | + #!/bin/sh |
| 116 | +
|
| 117 | + # Add environment vars pointing Docker to use the proxy |
| 118 | + # https://docs.docker.com/config/daemon/systemd/#httphttps-proxy |
| 119 | +
|
| 120 | + mkdir -p /etc/systemd/system/docker.service.d |
| 121 | + cat << EOD > /etc/systemd/system/docker.service.d/http-proxy.conf |
| 122 | + [Service] |
| 123 | + Environment="HTTP_PROXY=http://docker-registry-proxy.<your_domain>:3128/" |
| 124 | + Environment="HTTPS_PROXY=http://docker-registry-proxy.<your_domain>:3128/" |
| 125 | + EOD |
| 126 | +
|
| 127 | + # Get the CA certificate from the proxy and make it a trusted root. |
| 128 | + curl http://docker-registry-proxy.<your_domain>:3128/ca.crt > /usr/share/ca-certificates/docker_registry_proxy.crt |
| 129 | + echo "docker_registry_proxy.crt" >> /etc/ca-certificates.conf |
| 130 | + update-ca-certificates --fresh |
| 131 | +
|
| 132 | + # Reload systemd |
| 133 | + systemctl daemon-reload |
| 134 | +
|
| 135 | + # Restart dockerd |
| 136 | + systemctl restart docker.service |
| 137 | + image: 099720109477/ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-20200528 |
| 138 | + machineType: c3.xlarge |
| 139 | + maxSize: 15 |
| 140 | + minSize: 2 |
| 141 | + mixedInstancesPolicy: |
| 142 | + instances: |
| 143 | + - c3.xlarge |
| 144 | + - c4.xlarge |
| 145 | + - c5.xlarge |
| 146 | + - c5a.xlarge |
| 147 | + onDemandAboveBase: 0 |
| 148 | + onDemandBase: 0 |
| 149 | + spotAllocationStrategy: capacity-optimized |
| 150 | + nodeLabels: |
| 151 | + kops.k8s.io/instancegroup: spotgroup |
| 152 | + role: Node |
| 153 | + subnets: |
| 154 | + - us-east-1a |
| 155 | + - us-east-1b |
| 156 | + - us-east-1c |
| 157 | +``` |
| 158 | + |
| 159 | +Now all you need is to upgrade your cluster and do a rolling-update of the nodes, all images will be cached from now on. |
0 commit comments