通过kubectl run在单个容器中运行两个容器不会用两个容器终止容器

时间:2019-05-10 12:13:38

标签: mongodb kubernetes

尝试在一个kubernetes容器中运行两个docker容器。我们面临的问题是,在Kubernetes吊舱中有2个图像/容器,而以前在该吊舱运行良好并正常关闭吊舱的情况下,我们以前只有1个图像。现在的问题是,在同一容器中有2个图像或容器时,两个图像不能同时正常关闭。

我们正在针对MongoDB数据库对容器进行集成测试,我们的容器终止了,但是MongoDB无法在此处终止并继续运行

while getopts n:c:l:i:d:t: option
do
case "${option}"
in
n) Company_NAMESPACE=$OPTARG;;
c) Company_CONFIG=$OPTARG;;
l) Company_LICENSE=$OPTARG;;
i) Company_IMAGE=$OPTARG;;
d) Company_CONTAINER_NAME=$OPTARG;;
t) Company_TOKEN=$OPTARG;;
esac
done

kubectl run $Company_CONTAINER_NAME -n $Company_NAMESPACE --restart=Never --overrides='
{
  "apiVersion": "v1",
  "spec": {
    "imagePullSecrets": [
      {
        "name": "Company-regsecret"
      }
    ],
    "initContainers": [
      {
        "name": "copy-configs",
        "image": "busybox",
        "command": ["sh", "-c", "cp /tmp/Company-config-volume/server/* /tmp/ng-rt/config/server/ 2>/dev/null || true; cp /tmp/Company-license-volume/licenses/* /tmp/ng-rt/config/licenses 2>/dev/null || true"],
        "volumeMounts": [
          {
            "name": "Company-config-volume",
            "mountPath": "mount_path"
          },
          {
            "name": "'$Company_CONFIG'",
            "mountPath": "mount_path"
          },
          {
            "name": "Company-license-volume",
            "mountPath": "mount_path"
          },
          {
            "name": "'$Company_LICENSE'",
            "mountPath": "mount_path"
          }
        ]
      }
    ],
    "containers": [
    {
         "name": "mongodb-test",
          "image": "mongo:3.6.8",
          "command": [
              "numactl",
              "--interleave=all",
              "mongod",
              "--wiredTigerCacheSizeGB",
            "0.1",
            "--replSet",
            "MainRepSet",
            "--bind_ip_all"
        ],
        "ports": [{
            "containerPort": 27017
        }],
        "readinessProbe": {
            "exec": {
                "command": ["mongo", "--eval", "rs.initiate()"]
            }
        },
        "terminationGracePeriodSeconds": 10
    },
      {
        "env": [
          {
            "name": "AWS_ACCESS_KEY_ID",
            "valueFrom": {
              "secretKeyRef": {
                "key": "AWS_ACCESS_KEY_ID",
                "name": "aws-secrets"
              }
            }
          },
          {
            "name": "AWS_SECRET_ACCESS_KEY",
            "valueFrom": {
              "secretKeyRef": {
                "key": "AWS_SECRET_ACCESS_KEY",
                "name": "aws-secrets"
              }
            }
          },
          {
            "name": "AWS_REGION",
            "valueFrom": {
              "secretKeyRef": {
                "key": "AWS_REGION",
                "name": "aws-secrets"
              }
            }
          },
          {
            "name": "BUILD_ID",
            "valueFrom": {
              "configMapKeyRef": {
                "key": "BUILD_ID",
                "name": "config"
              }
            }
          }
        ],
        "command": [
          "sh",
          "-c",
          "mkdir -p mount_path 2\u003e/dev/null && npm test --skipConnectivityTestRethinkDB"
        ],
        "name": "'$Company_CONTAINER_NAME'",
        "image": "'$Company_IMAGE'",
        "volumeMounts": [
          {
            "mountPath": "mount_path",
            "name": "'$Company_CONFIG'"
          },
          {
            "mountPath": "mount_path",
            "name": "'$Company_LICENSE'"
          }
        ]
      }
    ],
    "volumes": [
      {
        "name": "Company-config-volume",
        "configMap": {
          "name": "'$Company_CONFIG'"
        }
      },
      {
        "name": "'$Company_CONFIG'",
        "emptyDir": {}
      },
      {
        "name": "Company-license-volume",
        "configMap": {
          "name": "'$Company_LICENSE'"
        }
      },
      {
        "name": "'$Company_LICENSE'",
        "emptyDir": {}
      }
    ]
  }
}
' --image=$Company_IMAGE -ti --rm --token=$Company_TOKEN
{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "test-dev-58663",
    "namespace": "d-int-company-dev-v2",
    "labels": {
      "run": "test-dev-58663"
    },
    "annotations": {
      "kubernetes.io/psp": "nfs-provisioner"
    }
  },
  "spec": {
    "volumes": [
      {
        "name": "company-config-volume",
        "configMap": {
          "name": "test-core",
          "defaultMode": 420
        }
      },
      {
        "name": "test-core",
        "emptyDir": {}
      },
      {
        "name": "company-license-volume",
        "configMap": {
          "name": "company-license",
          "defaultMode": 420
        }
      },
      {
        "name": "company-license",
        "emptyDir": {}
      },
      {
        "name": "default-token-wqp5x",
        "secret": {
          "secretName": "default-token-wqp5x",
          "defaultMode": 420
        }
      }
    ],
    "initContainers": [
      {
        "name": "copy-configs",
        "image": "busybox",
        "command": [
          "sh",
          "-c",
          "cp mount_path* mount_path 2>/dev/null || true; cp mount_path* mount_path 2>mount_path|| true"
        ],
        "resources": {},
        "volumeMounts": [
          {
            "name": "company-config-volume",
            "mountPath": "mount_path"
          },
          {
            "name": "test-core",
            "mountPath": "mount_path"
          },
          {
            "name": "company-license-volume",
            "mountPath": "mount_path"
          },
          {
            "name": "company-license",
            "mountPath": "mount_path"
          },
          {
            "name": "default-token-wqp5x",
            "readOnly": true,
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
          }
        ],
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "imagePullPolicy": "Always"
      }
    ],
    "containers": [
      {
        "name": "mongodb-test",
        "image": "mongo:3.6.8",
        "ports": [
          {
            "containerPort": 27017,
            "protocol": "TCP"
          }
        ],
        "resources": {},
        "volumeMounts": [
          {
            "name": "default-token-wqp5x",
            "readOnly": true,
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
          }
        ],
        "readinessProbe": {
          "exec": {
            "command": [
              "mongo",
              "--eval",
              "rs.initiate()"
            ]
          },
          "timeoutSeconds": 1,
          "periodSeconds": 10,
          "successThreshold": 1,
          "failureThreshold": 3
        },
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "imagePullPolicy": "IfNotPresent"
      },
      {
        "name": "test-dev-58663",
        "image": "image_path",
        "command": [
          "sh",
          "-c",
          "mkdir -p mount_path 2>/dev/null && npm test --skipConnectivityTestRethinkDB"
        ],
        "env": [
          {
            "name": "AWS_ACCESS_KEY_ID",
            "valueFrom": {
              "secretKeyRef": {
                "name": "aws-secrets",
                "key": "AWS_ACCESS_KEY_ID"
              }
            }
          },
          {
            "name": "AWS_SECRET_ACCESS_KEY",
            "valueFrom": {
              "secretKeyRef": {
                "name": "aws-secrets",
                "key": "AWS_SECRET_ACCESS_KEY"
              }
            }
          },
          {
            "name": "AWS_REGION",
            "valueFrom": {
              "secretKeyRef": {
                "name": "aws-secrets",
                "key": "AWS_REGION"
              }
            }
          },
          {
            "name": "BUILD_ID",
            "valueFrom": {
              "configMapKeyRef": {
                "name": "tbsp-config",
                "key": "BUILD_ID"
              }
            }
          }
        ],
        "resources": {},
        "volumeMounts": [
          {
            "name": "test-core",
            "mountPath": "mount_path"
          },
          {
            "name": "company-license",
            "mountPath": "mount_path"
          },
          {
            "name": "default-token-wqp5x",
            "readOnly": true,
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
          }
        ],
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "imagePullPolicy": "IfNotPresent"
      }
    ],
    "restartPolicy": "Never",
    "terminationGracePeriodSeconds": 10,
    "dnsPolicy": "ClusterFirst",
    "serviceAccountName": "default",
    "serviceAccount": "default",
    "nodeName": "pc212",
    "securityContext": {},
    "imagePullSecrets": [
      {
        "name": "company-regsecret"
      }
    ],
    "schedulerName": "default-scheduler",
    "tolerations": [
      {
        "key": "node.kubernetes.io/not-ready",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      },
      {
        "key": "node.kubernetes.io/unreachable",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      }
    ]
  },
  "status": {
    "phase": "Pending",
    "conditions": [
      {
        "type": "Initialized",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-05-10T12:50:39Z"
      },
      {
        "type": "Ready",
        "status": "False",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-05-10T12:49:54Z",
        "reason": "ContainersNotReady",
        "message": "containers with unready status: [mongodb-test test-dev-58663]"
      },
      {
        "type": "PodScheduled",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-05-10T12:49:54Z"
      }
    ],
    "hostIP": "10.10.2.12",
    "podIP": "172.16.4.22",
    "startTime": "2019-05-10T12:49:54Z",
    "initContainerStatuses": [
      {
        "name": "copy-configs",
        "state": {
          "terminated": {
            "exitCode": 0,
            "reason": "Completed",
            "startedAt": "2019-05-10T12:50:39Z",
            "finishedAt": "2019-05-10T12:50:39Z",
            "containerID": "docker://1bcd12f5848e32e82f7dfde8e245223345e87f70061b789cbbabc0f798436b59"
          }
        },
        "lastState": {},
        "ready": true,
        "restartCount": 0,
        "image": "busybox:latest",
        "imageID": "docker-pullable://busybox@sha256:0b184b74edc63924be0d7f67d16f5afbcdbe61caa1aca9312ed3b5c57792f6c1",
        "containerID": "docker://1bcd12f5848e32e82f7dfde8e245223345e87f70061b789cbbabc0f798436b59"
      }
    ],
    "containerStatuses": [
      {
        "name": "mongodb-test",
        "state": {
          "waiting": {
            "reason": "PodInitializing"
          }
        },
        "lastState": {},
        "ready": false,
        "restartCount": 0,
        "image": "mongo:3.6.8",
        "imageID": ""
      },
      {
        "name": "test-dev-58663",
        "state": {
          "waiting": {
            "reason": "PodInitializing"
          }
        },
        "lastState": {},
        "ready": false,
        "restartCount": 0,
        "image": "image_path",
        "imageID": ""
      }
    ],
    "qosClass": "BestEffort"
  }
}


容器和托管吊舱都应正常终止。

1 个答案:

答案 0 :(得分:0)

您的吊舱由两个容器组成,其中一个容器可以无限期运行,另一个容器可以完成运行。这是不好的做法。您应该将Pod分为两部分:Mongo Pod和集成脚本Job。您必须编写逻辑来监视Job完成,然后终止Pod和Job。您可以这样做:

kubectl apply -f integration-test.yaml
kubectl wait --for=condition=Complete --timeout=5m job/test
kubectl delete -f integration-test.yaml