Skip to content

Instantly share code, notes, and snippets.

@edr3x
Last active April 13, 2024 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edr3x/250daeaf0aa04eff871de99558faed27 to your computer and use it in GitHub Desktop.
Save edr3x/250daeaf0aa04eff871de99558faed27 to your computer and use it in GitHub Desktop.
Deploy your own container registry on k3s for local development and debugging

Local Container Image Registry in K3S

1. Add reg.local on your /etc/hosts file

2. Create k8s namespace

kubectl create namespace registry

3. Create a k8s yaml manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  name:  local-registry
  namespace: registry
  labels:
    app:  local-registry
spec:
  selector:
    matchLabels:
      app: local-registry
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app:  local-registry
    spec:
      containers:
      - name:  local-registry
        image: registry:2
        ports:
          - containerPort: 5000
        volumeMounts:
        - name: registry-volume
          mountPath: /var/lib/registry
      volumes:
      - name: registry-volume
        hostPath:
          path: /mnt/registry
---
apiVersion: v1
kind: Service
metadata:
  name: registry-svc
  namespace: registry
spec:
  selector:
    app: local-registry
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 5000
---      
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: registry-ingress
  namespace: registry
spec:
  ingressClassName: traefik
  rules:
  - host: reg.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: registry-svc
            port:
              number: 5000
---

4. Add the following in /etc/rancher/k3s/registries.yaml file

mirrors:
  reg.local:
    endpoint:
      - "http://reg.local"

5. Now when build your docker image

docker build -t reg.local/<image-name>:<tag> .

6. Push the image

docker push reg.local/<image-name>:<tag>

7. Now you can use the image in your k8s deployment as

        image: reg.local/<image>:<tag>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment