K8S Utility No. 5 - kompose

This article was last updated on: February 7, 2024 pm

Opening

📜 introduction

  • Sharpen knives and do not chop wood by mistake
  • Better tools make good work

Kubernetes + Compose = Kompose

Conversion tools from Docker Compose to Kubernetes

What is Kompose?

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

Why do developers love it?

  • Simplify the development process with Docker Compose, then deploy containers to production clusters
  • Convert yours docker-compose.yaml A simple command is required kompose convert

Easy as pie

  1. Find one docker-compose.yaml File;
  2. Execute:kompose convert
  3. execute kubectl apply And check your k8s cluster for your newly deployed containers!
1
2
3
4
5
6
7
8
9
10
11
$ 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 the K8S, which is officially provided docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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:

execute kompose convert After, from docker-compose.yml Generate the following files:

1
2
3
4
5
6
7
$ 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 born as a deployment and automatically convert fields such as label and env for you rsshub-deployment.yaml Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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: {}

Docker compose ports section, converted to SVC rsshub-service.yaml Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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: {}

Docker compose volumes field, converted to PVC redis-data-persistentvolumeclaim.yaml Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis-data
name: redis-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}

At ease!

Installation

1
2
3
4
5
6
7
8
# 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

That’s All

🎉🎉🎉