Files
docs/Servers/Containerization/Kubernetes/Migrating Docker-Compose.yml to k8s.md
Nicole Rappe 9202ddd355
All checks were successful
GitOps Automatic Deployment / GitOps Automatic Deployment (push) Successful in 8s
Update Servers/Containerization/Kubernetes/Migrating Docker-Compose.yml to k8s.md
2025-12-14 02:10:16 -07:00

6.5 KiB

Migrating docker-compose.yml to Rancher RKE2 Cluster

You may be comfortable operating with Portainer or docker-compose, but there comes a point where you might want to migrate those existing workloads to a Kubernetes cluster as easily-as-possible. Lucklily, there is a way to do this using a tool called "Kompose'. Follow the instructions seen below to convert and deploy your existing docker-compose.yml into a Kubernetes cluster such as Rancher RKE2.

!!! info "RKE2 Cluster Deployment" This document assumes that you have an existing Rancher RKE2 cluster deployed. If not, you can deploy one following the [Deploy RKE2 Cluster](https://docs.bunny-lab.io/Servers/Containerization/Kubernetes/Deployment/Rancher RKE2/) documentation.

Installing Kompose

The first step involves downloading Kompose from https://kompose.io/installation. Once you have it downloaded and installed onto your environment of choice, save a copy of your docker-compose.yml file somewhere on-disk, then open up a terminal and run the following command:

kompose --file docker-compose.yaml convert --stdout > ntfy-k8s.yaml

This will attempt to convert the docker-compose.yml file into a Kubernetes manifest YAML file. The Before and after example can be seen below:

=== "(Original) docker-compose.yml"

``` yaml
version: "2.1"
services:
  ntfy:
    image: binwiederhier/ntfy
    container_name: ntfy
    command:
      - serve
    environment:
      - NTFY_ATTACHMENT_CACHE_DIR=/var/lib/ntfy/attachments
      - NTFY_BASE_URL=https://ntfy.bunny-lab.io
      - TZ=America/Denver    # optional: Change to your desired timezone
    #user: UID:GID # optional: Set custom user/group or uid/gid
    volumes:
      - /srv/containers/ntfy/cache:/var/cache/ntfy
      - /srv/containers/ntfy/etc:/etc/ntfy
    ports:
      - 80:80
    restart: always
    networks:
        docker_network:
          ipv4_address: 192.168.5.45

networks:
  default:
    external:
      name: docker_network
  docker_network:
    external: true
```

=== "(Converted) ntfy-k8s.yaml"

``` yaml
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: C:\ProgramData\chocolatey\lib\kubernetes-kompose\tools\kompose.exe --file ntfy.yaml convert --stdout
    kompose.version: 1.37.0 (fb0539e64)
  labels:
    io.kompose.service: ntfy
  name: ntfy
spec:
  ports:
    - name: "80"
      port: 80
      targetPort: 80
  selector:
    io.kompose.service: ntfy

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: C:\ProgramData\chocolatey\lib\kubernetes-kompose\tools\kompose.exe --file ntfy.yaml convert --stdout
    kompose.version: 1.37.0 (fb0539e64)
  labels:
    io.kompose.service: ntfy
  name: ntfy
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: ntfy
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: C:\ProgramData\chocolatey\lib\kubernetes-kompose\tools\kompose.exe --file ntfy.yaml convert --stdout
        kompose.version: 1.37.0 (fb0539e64)
      labels:
        io.kompose.service: ntfy
    spec:
      containers:
        - args:
            - serve
          env:
            - name: NTFY_ATTACHMENT_CACHE_DIR
              value: /var/lib/ntfy/attachments
            - name: NTFY_BASE_URL
              value: https://ntfy.bunny-lab.io
            - name: TZ
              value: America/Denver
          image: binwiederhier/ntfy
          name: ntfy
          ports:
            - containerPort: 80
              protocol: TCP
          volumeMounts:
            - mountPath: /var/cache/ntfy
              name: ntfy-claim0
            - mountPath: /etc/ntfy
              name: ntfy-claim1
      restartPolicy: Always
      volumes:
        - name: ntfy-claim0
          persistentVolumeClaim:
            claimName: ntfy-claim0
        - name: ntfy-claim1
          persistentVolumeClaim:
            claimName: ntfy-claim1

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    io.kompose.service: ntfy-claim0
  name: ntfy-claim0
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    io.kompose.service: ntfy-claim1
  name: ntfy-claim1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
```

Deploy Workload into Rancher RKE2 Cluster

At this point, you need to import the yaml file you created into the Kubernetes cluster. This will occur in four sequential stages:

  • Setting up a "Project" to logically organize your containers
  • Setting up a "Namespace" for your container to isolate it from other containers in your Kubernetes cluster
  • Importing the YAML file into the aforementioned namespace
  • Configuring Ingress to allow external access to the container / service stack.

Create a Project

The purpose of the project is to logically organize your services together. This can be something like Home Automation, Log Analysis Systems, Network Tools, etc. You can do this by logging into your Rancher RKE2 cluster (e.g. https://rke2-cluster.bunny-lab.io). This Project name is unique to Rancher and purely used for organizational purposes and does not affect the namespaces / containers in any way.

  • Navigate to: Clusters > local > Projects/Namespaces > "Create Project"
    • Name: (e.g. Home Automation)
    • Description: (e.g. Various services that automate things within Bunny Lab)
    • Click the "Create" button

Create a Namespace within the Project

At this point, we need to create a namespace. This basically isolates the networking, credentials, secrets, and storage between the services/stacks. This ensures that if someone exploits one of your services, they will not be able to laterally move into another service within the same Kubernetes cluster.

  • Navigate to: Clusters > local > Projects/Namespaces > > "Create Namespace"
    • The name for the namespace should be named based on its operational-context, such as prod-ntfy or dev-ntfy.