container中的command的使用

前面说了init容器initContainers,这主要是为应用容器做前期准备工作的,一般都会用到shell脚本,这就会用到command,这里写写command的用法。

      command就是将命令在创建的容器中执行,有这些命令去完成一些工作,command用法和dockerfile中的cmd差不多, command可以单独写,也可以分成command和参数args,可以参考之前的CMD去理解,例如下面的写法都可以。

command: ['/bin/sh']


command:  ["rm", "-fr", "/var/lib/dbs/lost+found"]


command:
- 'sh'
- '-c'
- 'DATA_SOURCE_NAME="root@(localhost:3306)/" /bin/mysqld_exporter'


command: ["sh", "-c", "until nslookup myservice; do echo waiting for myservice; sleep 2; done;"]


command: ["sh"]
args: ["-c","until nslookup myservice; do echo waiting for myservice; sleep 2; done;"]


          livenessProbe:
            exec:
              command:
              - /bin/sh
              - -c
              - 'wget -O - -q --header "Authorization: Basic `echo -n \"$RABBIT_MANAGEMENT_USER:$RABBIT_MANAGEMENT_PASSWORD\" | base64`" http://localhost:15672/api/healthchecks/node | grep -qF "{\"status\":\"ok\"}"'
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}

另外args还有一种写法,可以理解成args后面是一个.sh文件,command来直接执行一个脚本文件,可以写相对复杂的脚本。

        command: ['sh']
        args:
        - "-c"
        - |
          set -ex
          if [ ! -d "/opt/ShenTong/odbs/OSRDB" ];then
            mkdir /opt/ShenTong/odbs/
            cp -r /opt/OSRDB /opt/ShenTong/odbs/
          else
            echo "数据库文件已存在"
          fi

最后贴一个官方写的一个rabbitmq的完整例子

    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      securityContext:
{{ toYaml .Values.securityContext | indent 10 }}
      serviceAccountName: {{ template "rabbitmq-ha.serviceAccountName" . }}
      initContainers:
        - name: bootstrap
          image: {{ .Values.registry }}{{ .Values.busyboxImage.repository}}{{ .Values.arch }}:{{ .Values.busyboxImage.tag}}
          imagePullPolicy: {{ .Values.busyboxImage.pullPolicy }}
          command: ['sh']
          args:
          - "-c"
          - |
            set -ex
            cp /configmap/* /etc/rabbitmq
            rm -f /var/lib/rabbitmq/.erlang.cookie
            {{- if .Values.forceBoot }}
            if [ -d "${RABBITMQ_MNESIA_DIR}" ]; then
              touch "${RABBITMQ_MNESIA_DIR}/force_load"
            fi
            {{- end }}
          env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.name
          - name: RABBITMQ_MNESIA_DIR
            value: /var/lib/rabbitmq/mnesia/rabbit@$(POD_NAME).{{ template "rabbitmq-ha.fullname" . }}-discovery.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}
          resources:
{{ toYaml .Values.initContainer.resources | indent 12 }}
          volumeMounts:
            - name: configmap
              mountPath: /configmap
            - name: config
              mountPath: /etc/rabbitmq
            - name: {{ .Values.persistence.name }}
              mountPath: /var/lib/rabbitmq

猜你喜欢

转载自www.cnblogs.com/yanh0606/p/11396614.html