Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

sql server - How to execute a sql script file in a Kubernetes Pod?

I wanted to create a SQL Server database in Kubernetes pod using a SQL script file. I have the SQL script which creates the database and inserts the master data. As I'm new to Kubernetes, I'm struggling to run the SQL script in a pod. I know the SQL script can be executed manually in a separate kubectl exec command, but I wanted it to be executed automatically in the pod deploy yml file itself.

Is there a way to mount the script file into pod's volume and run it after starting the container?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use kubernetes hooks for that case. There are two of them: PostStart and PreStop.

PostStart executes immediately after a container is created. PreStop on other hand is called immediately before a container is terminated.

You have two types of hook handlers that can be implemented: Exec or HTTP

Exec - Executes a specific command, such as pre-stop.sh, inside the cgroups and namespaces of the Container. Resources consumed by the command are counted against the Container. HTTP - Executes an HTTP request against a specific endpoint on the Container.

PostStart is the one to go with here, however please note that the hook is running in parallel with the main process. It does not wait for the main process to start up fully. Until the hook completes, the container will stay in waiting state.

You could use a little workaround for that and add a sleep command to your script in order to have it wait a bit for your main container creation. Your script file can be stored in the container image or mounted to volume shared with the pod using ConfigMap. Here`s some examples how to do that:

kind: ConfigMap
apiVersion: v1
metadata:
  namespace: <your-namespace> 
  name: poststarthook
data:
  poststart.sh: |
     #!/bin/bash
     echo "It`s done"

Make sure your script does not exceed 1mb limit for ConfigMap

After you define configMap you will have mount it using volumes:

spec:
      containers:
      - image: <your-image>
        name: example-container
        volumeMounts:
          - mountPath: /opt/poststart.sh
            subPath: poststart.sh
            name: hookvolume
      volumes:
      - name: hookvolume
        configMap:
          name: poststarthook
          defaultMode: 0755 #please remember to add proper (executable) permissions

And then you can define postStart in your spec:

spec:
  containers:
  - name: example-container
    image: <your-image> 
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", /opt/poststart.sh ]

You can read more about hooks in kubernetes documentation and in this article. Let me know if that was helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...