POD启动时执行多条命令

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的初始化
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要排查pod启动失败的问题,可以使用以下命令进行排查: 1. 使用命令 `kubectl get pods` 查看pod的状态,如果状态为 CrashLoopBackOff,说明pod启动失败。 2. 使用命令 `kubectl describe pods <pod-name> -n <namespace>` 查看pod的详细信息,包括错误信息。例如,如果错误信息显示为 "read-only file system error",则可能是由于文件系统只读导致的问题。 3. 检查容器的启动时间是否过长,可以通过调整容器的 `initialDelaySeconds` 参数来延迟探测容器的启动。如果容器还没完全启动就开始探测,可能会导致检查失败,然后pod会被kill并不断重启。另外,节点负载过高也可能导致pod启动失败,可以查看节点的负载情况。 4. 检查kubelet是否报错,可以使用命令 `kubectl describe pods <pod-name> -n <namespace>` 查看pod的事件,进而判断是否有kubelet报错。例如,若报错信息为 "OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:301: running exec setns process for init caused \"signal: killed\"": unknown",则可能是拉取镜像失败导致的问题。 5. 如果pod的状态为 Pending,说明pod还没有调度到某个节点上。可以使用命令 `kubectl describe pods <pod-name> -n <namespace>` 查看pod的事件,进而判断为什么没有调度。例如,Events中可能会显示 "FailedScheduling",并给出具体的原因,如 "0/4 nodes are available: 2 Insufficient cpu",表示节点资源不足。 通过以上命令和步骤,可以帮助你排查pod启动失败的问题,并找到解决方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值