K8S Practical Tool – kompose

Kompose Kubernetes
Kompose Kubernetes
Table of Contents Hide
  1. Easy as pie
  2. Actual Combat
  3. Install
  4. Author

What is Kompose?

Kompose is a conversion tool from dockercompose to container orchestrators such as Kubernetes (or OpenShift).

Why do developers love it?

  • Use Docker Compose to simplify the development process and then deploy containers to production clusters
  • docker-compose.yamlConverting yourkompose convert

Easy as pie

  1. Find a docker-compose.yamlfile;
  2. implement:kompose convert
  3. Execute kubectl applyand check your k8s cluster for your newly deployed container!
$ wget https://raw.githubusercontent.com/kubernetes/kompose/master/examples/docker-compose-v3.yaml -O docker-compose.yaml

$ kompose convert

$ kubectl apply -f .

$ kubectl get po
NAME                            READY     STATUS              RESTARTS   AGE
frontend-591253677-5t038        1/1       Running             0          10s
redis-master-2410703502-9hshf   1/1       Running             0          10s
redis-slave-4049176185-hr1lr    1/1       Running             0          10s

Actual Combat

For example, I want to install RssHub on K8S, which is officially provided docker-compose.yml:

version: '3'
services:
    rsshub:
        image: diygod/rsshub
        restart: always
        ports:
            - '1200:1200'
        environment:
            NODE_ENV: production
            CACHE_TYPE: redis
            REDIS_URL: 'redis://redis:6379/'
            PUPPETEER_WS_ENDPOINT: 'ws://browserless:3000'
        depends_on:
            - redis
            - browserless
    browserless:
        # See issue 6680
        image: browserless/chrome:1.43-chrome-stable
        restart: always
        ulimits:
          core:
            hard: 0
            soft: 0
    redis:
        image: redis:alpine
        restart: always
        volumes:
            - redis-data:/data
volumes:
    redis-data:

After executing kompose convert , the following files are generated from :

$ ll
.rw-r--r--  711 casey  1 Dec 21:20 browserless-deployment.yaml
.rw-r--r--  715 casey  1 Dec 21:20 docker-compose.yml
.rw-r--r--  243 casey  1 Dec 21:20 redis-data-persistentvolumeclaim.yaml
.rw-r--r--  867 casey  1 Dec 21:20 redis-deployment.yaml
.rw-r--r-- 1.0k casey  1 Dec 21:20 rsshub-deployment.yaml
.rw-r--r--  352 casey  1 Dec 21:20 rsshub-service.yaml

Each docker-compose container will be generated as a deployment, and the label and env fields will be automatically converted for you. For example rsshub-deployment.yaml :

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.26.0 (40646f47)
  creationTimestamp: null
  labels:
    io.kompose.service: rsshub
  name: rsshub
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: rsshub
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert
        kompose.version: 1.26.0 (40646f47)
      creationTimestamp: null
      labels:
        io.kompose.service: rsshub
    spec:
      containers:
        - env:
            - name: CACHE_TYPE
              value: redis
            - name: NODE_ENV
              value: production
            - name: PUPPETEER_WS_ENDPOINT
              value: ws://browserless:3000
            - name: REDIS_URL
              value: redis://redis:6379/
          image: diygod/rsshub
          name: rsshub
          ports:
            - containerPort: 1200
          resources: {}
      restartPolicy: Always
status: {}

The part of Docker compose ports will be converted to SVC, taking rsshub-service.yaml as an example :

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.26.0 (40646f47)
  creationTimestamp: null
  labels:
    io.kompose.service: rsshub
  name: rsshub
spec:
  ports:
    - name: "1200"
      port: 1200
      targetPort: 1200
  selector:
    io.kompose.service: rsshub
status:
  loadBalancer: {}

The Docker compose volumes field will be converted to PVC, taking  redis-data-persistentvolumeclaim.yaml as an example :

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: redis-data
  name: redis-data
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

comfortable!

Install

# Linux curl -L https://github.com/kubernetes/kompose/releases/download/v1.25.0/kompose-linux-amd64 -o kompose # macOS curl -L https://github.com/kubernetes/kompose/releases/download/v1.25.0/kompose-darwin-amd64 -o kompose chmod +x kompose sudo mv ./kompose /usr/local/bin/kompose

Author

  • Mohamed BEN HASSINE

    Mohamed BEN HASSINE is a Hands-On Cloud Solution Architect based out of France. he has been working on Java, Web , API and Cloud technologies for over 12 years and still going strong for learning new things. Actually , he plays the role of Cloud / Application Architect in Paris ,while he is designing cloud native solutions and APIs ( REST , gRPC). using cutting edge technologies ( GCP / Kubernetes / APIGEE / Java / Python )

    View all posts
0 Shares:
Leave a Reply
You May Also Like