kubernetescontainersopenshiftkubernetes-podopenshift-3

Init container in openshift


I am new to openshift, I have gone through Openshift website for more details but wanted to know if anyone has deployed init container. I want to use that to take dump from database and restore it to new version of it with the help of init container We are using postgres database

Any help would be appreciated.

Thanks!


Solution

  • I want to use that to take dump from database and restore it to new version of it with the help of init container

    I would say you should rather use the operator instead of initContainer. Take a look at below Init Containers Design Considerations

    There are some considerations that you should take into account when you create initcontainers:


    The only thing I found about using it for dumping data is this example about doing that with mysql, maybe it can guide you how to do it with postgresql.

    In this scenario, we are serving a MySQL database. This database is used for testing an application. It doesn’t have to contain real data, but it must be seeded with enough data so that we can test the application's query speed. We use an init container to handle downloading the SQL dump file and restore it to the database, which is hosted in another container. This scenario can be illustrated as below:

    enter image description here

    The definition file may look like this:

    apiVersion: v1
    kind: Pod
    metadata:
      name: mydb
      labels:
        app: db
    spec:
      initContainers:
        - name: fetch
          image: mwendler/wget
          command: ["wget","--no-check-certificate","https://sample-videos.com/sql/Sample-SQL-File-1000rows.sql","-O","/docker-entrypoint-initdb.d/dump.sql"]
          volumeMounts:
            - mountPath: /docker-entrypoint-initdb.d
              name: dump
      containers:
        - name: mysql
          image: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "example"
          volumeMounts:
            - mountPath: /docker-entrypoint-initdb.d
              name: dump
      volumes:
        - emptyDir: {}
          name: dump
    

    The above definition creates a Pod that hosts two containers: the init container and the application one. Let’s have a look at the interesting aspects of this definition:

    The init container is responsible for downloading the SQL file that contains the database dump. We use the mwendler/wget image because we only need the wget command. The destination directory for the downloaded SQL is the directory used by the MySQL image to execute SQL files (/docker-entrypoint-initdb.d). This behavior is built into the MySQL image that we use in the application container. The init container mounts /docker-entrypoint-initdb.d to an emptyDir volume. Because both containers are hosted on the same Pod, they share the same volume. So, the database container has access to the SQL file placed on the emptyDir volume.


    Additionally for best practices I would suggest to take a look at kubernetes operators, as far as I know that's the best practice way to menage databases in kubernetes.

    If you're not familiar with operators I would suggest to start with kubernetes documentation and this short video on youtube.

    Operators are methods of packaging Kubernetes that enable you to more easily manage and monitor stateful applications. There are many operators already available, such as the

    which automates and simplifies deploying and managing open source PostgreSQL clusters on Kubernetes by providing the essential features you need to keep your PostgreSQL clusters up and running.