Can Kubernetes be used like Docker Compose?

DockerKubernetesDocker ComposeGoogle Kubernetes-Engine

Docker Problem Overview


I have been digging through the Kubernetes documentation for hours. I understand the core design, and the notion of services, controllers, pods, etc.

What I don't understand, however, is the process in which I can declaratively configure the cluster. That is, a way for me to write a config file (or a set thereof) to define the makeup, and scaling options of the cloud deployment. I want to be able to declare which containers I want in which pods, how they will communicate, how they will scale, etc. without running a ton of cli commands.

Is there docker-compose functionality for Kubernetes?

I want my application to be defined in git—to be version controlled–without relying on manual cli interactions.

Is this possible to do in a concise way? Is there a reference that is more clear than the official documentation?

Docker Solutions


Solution 1 - Docker

If you're still looking, maybe this tool can help: https://github.com/kelseyhightower/compose2kube

You can create a compose file:

# sample compose file with 3 services
web:
  image: nginx
  ports:
    - "80"
    - "443"
database:
  image: postgres
  ports:
    - "5432"
cache:
  image: memcached
  ports:
    - "11211"

Then use the tool to convert it to kubernetes objects:

compose2kube -compose-file docker-compose.yml -output-dir output

Which will create these files:

output/cache-rc.yaml
output/database-rc.yaml
output/web-rc.yaml

Then you can use kubectl to apply them to kubernetes.

Solution 2 - Docker

If you have existing Docker Composer files, you may take a look at the Kompose project.

> kompose is a tool to help users who are familiar with docker-compose move to Kubernetes. kompose takes a Docker Compose file and translates it into Kubernetes resources. > > kompose is a convenience tool to go from local Docker development to managing your application with Kubernetes. Transformation of the Docker Compose format to Kubernetes resources manifest may not be exact, but it helps tremendously when first deploying an application on Kubernetes.

To run docker-compose.yaml file or your own, run:

kompose up

To convert docker-compose.yaml into Kubernetes deployments and services with one simple command:

$ kompose convert -f docker-compose.yaml
INFO Kubernetes file "frontend-service.yaml" created         
INFO Kubernetes file "redis-master-service.yaml" created     
INFO Kubernetes file "redis-slave-service.yaml" created      
INFO Kubernetes file "frontend-deployment.yaml" created      
INFO Kubernetes file "redis-master-deployment.yaml" created  
INFO Kubernetes file "redis-slave-deployment.yaml" created

For more info, check: http://kompose.io/

Solution 3 - Docker

Docker has officially announced the docker-compose functionality for the kubernetes cluster. So from now on you can compose the kubernetes resources in a file and apply them using that single file.

First we need to install the Compose on Kubernetes controller into your Kubernetes cluster. This controller uses the standard Kubernetes extension points to introduce the Stack to the Kubernetes API. Check the full documentation to install the docker compose controller:

> https://github.com/docker/compose-on-kubernetes

Let's write a simple compose yaml file:

version: "3.7"
services:
  web:
    image: dockerdemos/lab-web
    ports:
     - "33000:80"
  words:
    image: dockerdemos/lab-words
    deploy:
      replicas: 3
      endpoint_mode: dnsrr
  db:
    image: dockerdemos/lab-db

We’ll then use the docker client to deploy this to a Kubernetes cluster running the controller:

$ docker stack deploy --orchestrator=kubernetes -c docker-compose.yml words
Waiting for the stack to be stable and running...
db: Ready       [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
web: Ready      [pod status: 1/1 ready, 0/1 pending, 0/1 failed]
words: Ready    [pod status: 1/3 ready, 2/3 pending, 0/3 failed]
Stack words is stable and running

We can then interact with those objects via the Kubernetes API. Here you can see we’ve created the lower-level objects like Services, Pods, Deployments and ReplicaSets automatically:

$ kubectl get deployments
NAME                    DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/db      1         1         1            1           57s
deployment.apps/web     1         1         1            1           57s
deployment.apps/words   3         3         3            3           57s    

It’s important to note that this isn’t a one-time conversion. The Compose on Kubernetes API Server introduces the Stack resource to the Kubernetes API. So we can query and manage everything at the same level of abstraction as we’re building the application. That makes delving into the details above useful for understanding how things work, or debugging issues, but not required most of the time:

$ kubectl get stack
NAME      STATUS      PUBLISHED PORTS   PODS     AGE      
words     Running     33000             5/5      4m

Solution 4 - Docker

Kubernetes certainly has its own yaml (as shown in "Deploying Applications")

But as "Docker Clustering Tools Compared: Kubernetes vs Docker Swarm", it was not written (just) for Docker, and it has its own system.

You could use docker-compose to start Kubernetes though, as shown in "vyshane/kid": that does mask some of the kubectl commands cli in scripts (which can be versioned).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDon ScottView Question on Stackoverflow
Solution 1 - DockerAhmad AabedView Answer on Stackoverflow
Solution 2 - DockerJoaoCCView Answer on Stackoverflow
Solution 3 - DockerPrafull LadhaView Answer on Stackoverflow
Solution 4 - DockerVonCView Answer on Stackoverflow