|
| 1 | +# Example: Node-to-Node image sharing on Kubernetes using `nerdctl ipfs registry` |
| 2 | + |
| 3 | +Usage: |
| 4 | +- Generate `bootstrap.yaml` by executing `bootstrap.yaml.sh` (e.g. `./bootstrap.yaml.sh > ${DIR_LOCATION}/bootstrap.yaml`) |
| 5 | + - [`ipfs-swarm-key-gen`](https://github.com/Kubuxu/go-ipfs-swarm-key-gen) is required (see https://github.com/ipfs/go-ipfs/blob/v0.10.0/docs/experimental-features.md#private-networks) |
| 6 | +- Deploy `bootstrap.yaml` and `nerdctl-ipfs-registry.yaml` (e.g. using `kubectl apply`) |
| 7 | +- Make sure nodes contain containerd >= v1.5.8 |
| 8 | + |
| 9 | +## Example on kind |
| 10 | + |
| 11 | +Prepare cluster (make sure kind nodes contain containerd >= v1.5.8). |
| 12 | + |
| 13 | +```console |
| 14 | +$ cat <<EOF > /tmp/kindconfig.yaml |
| 15 | +kind: Cluster |
| 16 | +apiVersion: kind.x-k8s.io/v1alpha4 |
| 17 | +nodes: |
| 18 | +- role: control-plane |
| 19 | +- role: worker |
| 20 | +- role: worker |
| 21 | +EOF |
| 22 | +$ kind create cluster --image=kindest/node:v1.23.1 --config=/tmp/kindconfig.yaml |
| 23 | +$ ./bootstrap.yaml.sh > ./bootstrap.yaml |
| 24 | +$ kubectl apply -f . |
| 25 | +``` |
| 26 | + |
| 27 | +Prepare `kind-worker` (1st node) for importing an image to IPFS |
| 28 | + |
| 29 | +(in `kind-worker`) |
| 30 | + |
| 31 | +```console |
| 32 | +$ docker exec -it kind-worker /bin/bash |
| 33 | +(kind-worker)# NERDCTL_VERSION=0.15.0 |
| 34 | +(kind-worker)# curl -sSL --output /tmp/nerdctl.tgz https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz |
| 35 | +(kind-worker)# tar zxvf /tmp/nerdctl.tgz -C /usr/local/bin/ |
| 36 | +``` |
| 37 | + |
| 38 | +Add an image to `kind-worker`. |
| 39 | + |
| 40 | +```console |
| 41 | +$ docker exec -it kind-worker /bin/bash |
| 42 | +(kind-worker)# mkdir -p /tmp/ipfsapi ; echo -n /ip4/127.0.0.1/tcp/5001 > /tmp/ipfsapi/api |
| 43 | +(kind-worker)# export IPFS_PATH=/tmp/ipfsapi |
| 44 | +(kind-worker)# nerdctl pull ghcr.io/stargz-containers/jenkins:2.60.3-org |
| 45 | +(kind-worker)# nerdctl push ipfs://ghcr.io/stargz-containers/jenkins:2.60.3-org |
| 46 | +(kind-worker)# nerdctl rmi ghcr.io/stargz-containers/jenkins:2.60.3-org |
| 47 | +``` |
| 48 | + |
| 49 | +The image added to `kind-worker` is shared to `kind-worker2` via IPFS. |
| 50 | +You can run this image on all worker nodes using the following manifest (assuming the image is added to IPFS as CID `localhost:5050/ipfs/bafkreife3j4tgtx23jcautprornrdp2p4g3j3ndnidzdlrpd7unbpnwkce`). |
| 51 | + |
| 52 | +```console |
| 53 | +$ cat <<EOF | kubectl apply -f - |
| 54 | +apiVersion: apps/v1 |
| 55 | +kind: Deployment |
| 56 | +metadata: |
| 57 | + name: jenkins |
| 58 | +spec: |
| 59 | + replicas: 2 |
| 60 | + selector: |
| 61 | + matchLabels: |
| 62 | + app: jenkins |
| 63 | + template: |
| 64 | + metadata: |
| 65 | + labels: |
| 66 | + app: jenkins |
| 67 | + spec: |
| 68 | + containers: |
| 69 | + - name: jenkins |
| 70 | + image: localhost:5050/ipfs/bafkreife3j4tgtx23jcautprornrdp2p4g3j3ndnidzdlrpd7unbpnwkce |
| 71 | + resources: |
| 72 | + requests: |
| 73 | + cpu: 1 |
| 74 | +EOF |
| 75 | +``` |
| 76 | + |
| 77 | +The image runs on all nodes. |
| 78 | + |
| 79 | +```console |
| 80 | +$ kubectl get pods -owide | grep jenkins |
| 81 | +jenkins-7bd8f96d79-2jbc6 1/1 Running 0 69s 10.244.1.3 kind-worker <none> <none> |
| 82 | +jenkins-7bd8f96d79-jb5lm 1/1 Running 0 69s 10.244.2.4 kind-worker2 <none> <none> |
| 83 | +``` |
| 84 | + |
| 85 | +## Example Dockerfile of nerdctl |
| 86 | + |
| 87 | +```Dockerfile |
| 88 | +FROM ubuntu:20.04 |
| 89 | +ARG NERDCTL_VERSION=0.16.0 |
| 90 | +RUN apt-get update -y && apt-get install -y curl && \ |
| 91 | + curl -sSL --output /tmp/nerdctl.tgz https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-${TARGETARCH:-amd64}.tar.gz && \ |
| 92 | + tar zxvf /tmp/nerdctl.tgz -C /usr/local/bin/ && \ |
| 93 | + rm /tmp/nerdctl.tgz |
| 94 | +ENTRYPOINT [ "/usr/local/bin/nerdctl", "ipfs", "registry", "serve" ] |
| 95 | +``` |
0 commit comments