How to include all kubernetes objects into one file

12/8/2016

Assuming I have two multi-container pods and services for exposing the pods to the cluster. I also have Replication controller for maintaining the number of pods at any time.

The above cluster is an trivial example. In this point I have two pod files, two service files and two RC files. This makes the file management difficult.

I know that all the files can be put in a directory and use kubectl create -f directory to execute whole thing in a single command. But, I feel the file management is a overhead. Is there something like docker-compose.yml, where we can include all pods in a single file.

I would like to know the best practices for using kubernetes in production. Changing many files in production does not seem to be an good idea.

-- Lakshman Diwaakar
google-cloud-platform
google-kubernetes-engine
kubernetes

1 Answer

12/8/2016

You can use triple dashes or List resource as in these examples:

examples:

List

apiVersion: v1
kind: List
items:
- apiVersion: v1
  kind: Service
  metadata:
    name: list-service-test
  spec:
    ports:
    - protocol: TCP
      port: 80
    selector:
      app: list-deployment-test
- apiVersion: extensions/v1beta1
  kind: Deployment
  metadata:
    name: list-deployment-test
    labels:
      app: list-deployment-test
  spec:
    replicas: 1
    template:
      metadata:
        labels:
          app: list-deployment-test
      spec:
        containers:
          - name: nginx
            image: nginx

Triple dash:

---
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: redis
    redis-sentinel: "true"
    role: master
  name: redis-master
spec:
  containers:
    - name: master
      image: kubernetes/redis:v1
      env:
        - name: MASTER
          value: "true"
      ports:
        - containerPort: 6379
      resources:
        limits:
          cpu: "0.5"
      volumeMounts:
        - mountPath: /redis-master-data
          name: data
    - name: sentinel
      image: kubernetes/redis:v1
      env:
        - name: SENTINEL
          value: "true"
      ports:
        - containerPort: 26379
  volumes:
    - name: data
      emptyDir: {}
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: redis-proxy
    role: proxy
  name: redis-proxy
spec:
  containers:
  - name: proxy
    image: kubernetes/redis-proxy:v1
    ports:
    - containerPort: 6379
      name: api
-- Ivan Frolov
Source: StackOverflow