POD启动时执行多条命令(以pgsql为例)
创建Pod时设置命令及参数
创建Pod时,可以为其下的容器设置启动时要执行的命令及其参数。如果要设置命令,就填写在配置文件的command字段下,如果要设置命令的参数,就填写在配置文件的args字段下。一旦Pod创建完成,该命令及其参数就无法再进行更改了。
如果在配置文件中设置了容器启动时要执行的命令及其参数,那么容器镜像中自带的命令与参数将会被覆盖而不再执行。如果配置文件中只是设置了参数,却没有设置对应的命令,那么容器镜像中自带的命令会使用该新参数作为其执行时的参数。
具体使用方式如下:
1: 将要使用的shell命令以configmap的形式挂载到pod中,如下:
在pgsql启动时需要执行以下两个文件进行数据库的初始化
[root@nuc1 initdb]# pwd
/root/k8s-yaml/configuration/postgresql/initdb
[root@nuc1 initdb]# ll
total 8
-rwxrwxr-x 1 root root 202 Mar 31 16:43 001-init-chirpstack.sh
-rwxrwxr-x 1 root root 168 Mar 31 16:43 002-chirpstack_extensions.sh
2:创建configmap,将目录作为目标文件
[root@nuc1 initdb]# kubectl create configmap postgres-config --from-file=root/k8s-yaml/configuration/postgresql/initdb
3: 查看镜像的启动参数
镜像启动命令 :
[root@nuc1 k8s-yaml]# crictl inspecti docker.io/library/postgres:14-alpine
I0427 15:19:38.492445 14090 util_unix.go:103] "Using this endpoint is deprecated, please consider using full URL format" endpoint="/run/containerd/containerd.sock" URL="unix:///run/containerd/containerd.sock"
{
"status": {
"id": "sha256:9d94e6318ef20718479e031c9afdd911c572c0fdf724437b4c87de4b24136dae",
"repoTags": [
"docker.io/library/postgres:14-alpine"
],
"repoDigests": [],
"size": "244883710",
"uid": null,
"username": "",
"spec": null
},
"info": {
"chainID": "sha256:4af6f6944fa41c843eba7b4e2ad3ad45303ba0934425f9a8474f267056d14760",
"imageSpec": {
"created": "2023-03-30T01:52:50.75554073Z",
"architecture": "amd64",
"os": "linux",
"config": {
"ExposedPorts": {
"5432/tcp": {}
},
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=en_US.utf8",
"PG_MAJOR=14",
"PG_VERSION=14.7",
"PG_SHA256=cef60f0098fa8101c1546f4254e45b722af5431337945b37af207007630db331",
"PGDATA=/var/lib/postgresql/data"
],
"Entrypoint": [
"docker-entrypoint.sh"
],
"Cmd": [
"postgres" ###启动命令是docker-entrypoint.sh,参数是postgres
],
"Volumes": {
"/var/lib/postgresql/data": {}
},
"StopSignal": "SIGINT"
},
"rootfs": {
"type": "layers",
4:pod挂载configmap
[root@nuc1 k8s-yaml]# cat postgres-deployment.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
io.kompose.service: postgres
name: postgres
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: postgres
strategy:
type: Recreate
template:
metadata:
labels:
io.kompose.service: postgres
spec:
containers:
- env:
- name: POSTGRES_PASSWORD
value: root
- name: POSTGRES_USER
value: postgres
image: docker.io/postgres:14-alpine
imagePullPolicy: IfNotPresent
name: postgres
resources: {}
command: ["/bin/sh", "-c"]
args: ["/usr/local/bin/docker-entrypoint.sh postgres; ./docker-entrypoint-initdb.d/001-init-chirpstack.sh && ./docker-entrypoint-initdb.d/002-chirpstack_extensions.sh"]
####如果只是执行两条命令的话,使用分号分隔就好
####使用 command 和 args 字段指定多条命令的exec模式写法如下:
#### command: ["/bin/sh","-c"]
#### args: ["command one; command two && command three"]
volumeMounts:
- mountPath: /docker-entrypoint-initdb.d
name: postgres-config
- mountPath: /var/lib/postgresql/data
name: postgresqldata
restartPolicy: Always
volumes:
- name: postgres-config
configMap:
name: postgres-config ####挂载configmap
- name: postgresqldata
persistentVolumeClaim:
claimName: postgresqldata
status: {}
5:以上pod启动时既不会覆盖启动命令,在启动之后也会执行两条命令进行pgsql的初始化