Running a shell script to initialize pods in kubernetes (initializing my db cluster)

Shachar Hamuzim Rajuan

I need to be able to run a shell script (my script is for initializing my db cluster) to initialize my pods in Kubernetes,

I don't want to create my script inside my dockerfile because I get my image directly from the web so I don't want to touch it.

So I want to know if there is a way to get my script in to one of my volumes so I can execute it like that:

spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["./init.sh"]
  restartPolicy: OnFailure
Shachar Hamuzim Rajuan

I finally decided to take the approach of creating a config file with the script we want to run and then call this configmap from inside the volume.

this is a short explanation:

In my pod.yaml file there is a VolumeMount called "/pgcong" which is the directory that the docker image reads any SQL script that you put there and run it when the pod is starting. And inside Volumes I will put the configmap name (postgres-init-script-configmap) which is the name of the config defined inside the configmap.yaml file.

There is no need to create the configmap using kubernetes, The pod will take the configuration from the configmap file as long as you place it in the same directory as the pod.yaml .

my POD yaml file:

apiVersion: v1
kind: Pod
metadata:
  name: "{{.Values.container.name.primary}}"
  labels:
    name: "{{.Values.container.name.primary}}"
spec:
  securityContext:
    fsGroup: 26
  restartPolicy: {{default "Always" .Values.restartPolicy}}

  containers:
  - name: {{.Values.container.name.primary}}
    image: "{{.Values.image.repository}}/{{.Values.image.container}}:{{.Values.image.tag}}"
    ports:
    - containerPort: {{.Values.container.port}}
    env:
    - name: PGHOST
      value: /tmp
    - name: PG_PRIMARY_USER
      value: primaryuser
    - name: PG_MODE
      value: primary
    resources:
      requests:
        cpu: {{ .Values.resources.cpu }}
        memory: {{ .Values.resources.memory }}
    volumeMounts:
    - mountPath: /pgconf
      name: init-script
      readOnly: true
  volumes:
  - name: init-script
    configMap:
      name: postgres-init-script-configmap

my configmap.yaml (Which contains the SQL script that will initial the DB):

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-init-script-configmap
data:
  setup.sql: |-
    CREATE USER david WITH PASSWORD 'david';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Stopping all pods in Kubernetes cluster before running database migration job

How to get the number of pods that my kubernetes cluster is able to have with prometheus?

Why are there so many duplicate pods in my kubernetes cluster?

Initialize PySpark shell by running a script in my Linux terminal (Spark Version 2.4.4)

Crontab not running my shell script

running pods and containers in Kubernetes

Kubernetes, Running two redis pods in the same cluster results in issues, running one of each on its own runs flawlessly

CockroachDB Cluster on Kubernetes Pods Crashing

Kubernetes PODs not accessible within the cluster

How do I avoid unschedulable pods when running an autoscale-enabled Google Container/Kubernetes Engine cluster?

Kubernetes pods cannot resolve private IP addresses within the cluster running weave CNI

Running shell script with sarge eats my quotes

How do I tell if my container is running inside a Kubernetes cluster?

How to connect ignite web console with my kubernetes cluster pods (server and client)

Avoid parallel processing of pods in an OpenShift/Kubernetes cluster

Is it possible to have a Kubernetes cluster with privileged pods by default?

Shell script should wait until kubernetes pod is running

Are there issues with running user pods on a Kubernetes master node?

Kubernetes pods not starting, running behind a proxy

kubernetes list all running pods name

Kubernetes prometheus metrics for running pods and nodes?

Consul on Kubernetes: Consul pods are running but not ready

How to update a set of pods running in kubernetes?

Running a command on all kubernetes pods of a service

How to change running pods limits in Kubernetes?

Monitoring the number of threads running on kubernetes PODS

Managing DB migrations on Kubernetes cluster

How to detect if my shell script is running through a pipe?

Running functions that were declared outside my shell script

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive