How to define volume mounts for logs in Kubernates deployments and access service through public IP?

6/1/2020
  • Question first Part: Every time I deploy latest service my data is lost so I want to mount some volume so that my data is not lost and also want to sync same volume mount to multiple services for data sharing.
  • Question Second Part: how could I access service using public IP, It's accessible on local network but not publicly

Here is the deployment file I write:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: setup
  labels:
    app: setup
spec:
  replicas: 1
  selector:
    matchLabels:
      app: setup
  template:
    metadata:
      labels:
        app: setup
    spec:
      containers:
      - name: setup
        image: esmagic/setup:1.0
        ports:
        - containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
  name: setup
  labels:
    app: setup
spec:
  ports:
  - name: tcp-8081-8081-setup
    port: 8081
    targetPort: 8081
    protocol: TCP
  selector:
    app: setup
  type: LoadBalancer
  externalIPs:
    - 192.168.1.249
  sessionAffinity: None
  externalTrafficPolicy: Cluster
status:
  loadBalancer: {}
-- Dominant Programmers
deployment
kubernetes

2 Answers

6/1/2020
  1. Follow this guide for using a volume mount in a pod. Specifically for logging you can write it to a volume and use Fluentd or FluentBit to send the logs to a centralized log aggregator system.
  2. You can use NodePort or LoadBalaancer type service to expose pods outside the cluster.If you are using NodePort then the Kubernetes cluster nodes need to have Public IP. Follow this guide for NodePort.
-- Arghya Sadhu
Source: StackOverflow

6/1/2020

For the first one, you have to define volume mounts in spec container and define volume in spec, and for second one you have to define public IP in external IPs

Deployment file after changes:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: setup
  labels:
    app: setup
spec:
  replicas: 1
  selector:
    matchLabels:
      app: setup
  template:
    metadata:
      labels:
        app: setup
    spec:
      containers:
      - name: setup
        image: esmagic/setup:1.0
        ports:
        - containerPort: 8081
        volumeMounts:
          - name: sync-dir
            mountPath: /abc/xyz #Any Real path like (/opt/software)
      volumes:
      - name: sync-dir
        hostPath:
          path: /abc/xyz
---
apiVersion: v1
kind: Service
metadata:
  name: setup
  labels:
    app: setup
spec:
  ports:
  - name: tcp-8081-8081-setup
    port: 8081
    targetPort: 8081
    protocol: TCP
  selector:
    app: setup
  type: LoadBalancer
  externalIPs:
    - 192.168.1.249 #Here comes public IP or write both
  sessionAffinity: None
  externalTrafficPolicy: Cluster
status:
  loadBalancer: {}

And also visit this guide for more details about the volume mount as said by Arghya Sadhu

-- Ammar Ali
Source: StackOverflow