kubernetes how to write deployment yaml just for replica

6/20/2018

I have a pod yaml file(see below). It has all the properties I want, except replica properties. So that I have to write deployment yaml.

apiVersion: v1
kind: Pod
metadata:
  name: app-ykt
  labels:
    app: app-ykt
    purpose: ykt_production
spec:
  containers:
  - name: app-ykt
    image: app
    imagePullPolicy: IfNotPresent
    ports:
      - containerPort: 80
    volumeMounts:
      - name: volume-app-ykt
        mountPath: /usr/application
    env:
      - name: spring.config.location
        value: application.properties
  volumes:
      - name: volume-app-ykt
        hostPath:
          path: /opt/docker/datalook-pre-core
          type: Directory

When writing deployment file, I have to mention image and other properties which I donot want. What's more, it creates another pod and missing some properties I must have, for example volume. My target is just this replica, so that I could get high availability for the pod. Any solution?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-ykt
  labels:
    app: app-ykt
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app-ykt
  template:
    metadata:
      labels:
        app: app-ykt
    spec:
      containers:
      - name: app-ykt
        image: app
        ports:
        - containerPort: 80
-- user84592
kubernetes

2 Answers

6/20/2018

First of all, I think hostPath volume is not HA solutions, so you should concern to share a volume as ReadWriteMany Persistent Volume across kubernetes cluster. And then you also need the internal load balancer among replicated pods like Services[0].

I hope it help you.

[0][https://kubernetes.io/docs/concepts/services-networking/service/]

-- Daein Park
Source: StackOverflow

6/21/2018

every Kubernetes object has its own requirements. If you want to create Deployment you must specify this properties. Deployment object is a "abstract" layer for ReplicaSet creation and manage it. You cannot create any Deployment (RelicaSet) without container properties description (images, volumes etc.)

If you are going to use Kubernetes you need to deploy your apps by "Kubernetes way". You could create Deployment .yml file with your app description, then you would scale the Deployment to any count of replicas.

Service and maybe Ingress objects could help you to load balance among replicas, as @Daein mentioned it above

Your Deployment could look like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-ykt
  namespace: elk
  labels:
    app: app-ykt
spec:
  replicas: 3
  selector:
    matchLabels:
      app: app-ykt
  template:
    metadata:
      labels:
        app: app-ykt
    spec:
      containers:
      - name: app-ykt
        image: <your_image>
        ports:
        - containerPort: 80
        env:
        - name: spring.config.location
          value: application.properties
        volumeMounts:
          - name: volume-app-ykt
            mountPath: /usr/application
      volumes:
      - name: volume-app-ykt
        hostPath:
          path: /opt/docker/datalook-pre-core
          type: Directory

This will create Deployment, which in turn will automaticly create 3 replicas (3 Pods) with only one app-ykt container within

Note: I also think using hostPath here is not a good idea, as it mentioned by @Daein, even though I don't know your app configuration. hostPath commonly used to access any files on your nodes...

Remember to use hostPath volumes only if you need to read or write system files on the node. Never use them to persist data across pods. "Kubernetes in Action" (c) Marko Luksa

-- Konstantin Vustin
Source: StackOverflow