.NET Core Docker 容器无法在 Kubernetes 中运行

.NET Core Docker Container won't work in Kubernetes

请阅读更新 2

我有一个非常简单的 EventHubClient 应用程序。它只会收听 EventHub 消息。

我明白 运行 Docker 2017 年 Visual Studio 支持(Linux 容器)。

但是当我尝试在 Kubernetes 中部署它时,我得到 "Back-off restarting failed container"

C#代码:

public static void Main(string[] args)
{
    // Init Mapper
    AutoMapper.Mapper.Initialize(cfg =>
    {
        cfg.AddProfile<AiElementProfile>();
    });

    Console.WriteLine("Registering EventProcessor...");

    var eventProcessorHost = new EventProcessorHost(
        EventHubPath,
        ConsumerGroupName,
        EventHubConnectionString,
        AzureStorageConnectionString,
        ContainerName
    );

    // Registers the Event Processor Host and starts receiving messages
    eventProcessorHost.RegisterEventProcessorAsync<EventProcessor>();

    Console.WriteLine("Receiving. Press ENTER to stop worker.");
    Console.ReadLine();
}

Kubernetes 清单文件 (.yaml):

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: historysvc-deployment
spec:
  selector:
    matchLabels:
      app: historysvc
  replicas: 2
  template:
    metadata:
      labels:
        app: historysvc
    spec:
      containers:
      - name: historysvc
        image: vncont.azurecr.io/historysvc:v1
        ports:
        - containerPort: 80
      imagePullSecrets:
        - name: acr-auth

kubectl get pods:

   NAME                                     READY     STATUS             RESTARTS   AGE
    historysvc-deployment-558fc5649f-bln8f   0/1       CrashLoopBackOff   17         1h
    historysvc-deployment-558fc5649f-jgjvq   0/1       CrashLoopBackOff   17         1h

kubectl describe pod historysvc-deployment-558fc5649f-bln8f

Name:           historysvc-deployment-558fc5649f-bln8f
Namespace:      default
Node:           aks-nodepool1-81522366-0/10.240.0.4
Start Time:     Tue, 24 Jul 2018 10:15:37 +0200
Labels:         app=historysvc
                pod-template-hash=1149712059
Annotations:    <none>
Status:         Running
IP:             10.244.0.11
Controlled By:  ReplicaSet/historysvc-deployment-558fc5649f
Containers:
  historysvc:
    Container ID:   docker://59e66f1e6420146f6eca4f19e2801a4ee0435a34c7ac555a8d04f699a1497f35
    Image:          vncont.azurecr.io/historysvc:v1
    Image ID:       docker-pullable://vncont.azurecr.io/historysvc@sha256:636d81435bd421ec92a0b079c3841cbeb3ad410509a6e37b1ec673dc4ab8a444
    Port:           80/TCP
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 24 Jul 2018 10:17:10 +0200
      Finished:     Tue, 24 Jul 2018 10:17:10 +0200
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 24 Jul 2018 10:16:29 +0200
      Finished:     Tue, 24 Jul 2018 10:16:29 +0200
    Ready:          False
    Restart Count:  4
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-mt8mm (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          False
  PodScheduled   True
Volumes:
  default-token-mt8mm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-mt8mm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason                 Age              From                               Message
  ----     ------                 ----             ----                               -------
  Normal   Scheduled              1m               default-scheduler                  Successfully assigned historysvc-deployment-558fc5649f-bln8f to aks-nodepool1-81522366-0
  Normal   SuccessfulMountVolume  1m               kubelet, aks-nodepool1-81522366-0  MountVolume.SetUp succeeded for volume "default-token-mt8mm"
  Normal   Pulled                 8s (x5 over 1m)  kubelet, aks-nodepool1-81522366-0  Container image "vncont.azurecr.io/historysvc:v1" already present on machine
  Normal   Created                7s (x5 over 1m)  kubelet, aks-nodepool1-81522366-0  Created container
  Normal   Started                6s (x5 over 1m)  kubelet, aks-nodepool1-81522366-0  Started container
  Warning  BackOff                6s (x8 over 1m)  kubelet, aks-nodepool1-81522366-0  Back-off restarting failed container

我错过了什么?


更新 1

kubectl describe pod historysvc-deployment-558fc5649f-jgjvq

Name:           historysvc-deployment-558fc5649f-jgjvq
Namespace:      default
Node:           aks-nodepool1-81522366-0/10.240.0.4
Start Time:     Tue, 24 Jul 2018 10:15:37 +0200
Labels:         app=historysvc
                pod-template-hash=1149712059
Annotations:    <none>
Status:         Running
IP:             10.244.0.12
Controlled By:  ReplicaSet/historysvc-deployment-558fc5649f
Containers:
  historysvc:
    Container ID:   docker://ccf83bce216276450ed79d67fb4f8a66daa54cd424461762478ec62f7e592e30
    Image:          vncont.azurecr.io/historysvc:v1
    Image ID:       docker-pullable://vncont.azurecr.io/historysvc@sha256:636d81435bd421ec92a0b079c3841cbeb3ad410509a6e37b1ec673dc4ab8a444
    Port:           80/TCP
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Wed, 25 Jul 2018 09:32:34 +0200
      Finished:     Wed, 25 Jul 2018 09:32:35 +0200
    Ready:          False
    Restart Count:  277
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-mt8mm (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          False
  PodScheduled   True
Volumes:
  default-token-mt8mm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-mt8mm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason   Age                  From                               Message
  ----     ------   ----                 ----                               -------
  Warning  BackOff  2m (x6238 over 23h)  kubelet, aks-nodepool1-81522366-0  Back-off restarting failed container

更新 2

当我 运行 在本地使用时:

docker run <image> 

它立即结束(忽略读取行)(完成),这似乎是问题所在。

我要写

docker run -it <image> 

-它在最后为它做读取行。

kubernetes 运行 如何生成 docker 镜像?我在哪里可以设置它?

这可以通过将参数附加到 运行 和您的 deployment 来完成。

在您的情况下,Kubernetes 清单文件 (.yaml) 应如下所示:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: historysvc-deployment
spec:
  selector:
matchLabels:
  app: historysvc
  replicas: 2
  template:
metadata:
  labels:
    app: historysvc
spec:
  containers:
  - name: historysvc
    image: vncont.azurecr.io/historysvc:v1
    ports:
    - containerPort: 80
    args: ["-it"]
  imagePullSecrets:
    - name: acr-auth

您可以在 k8s 文档中找到解释 inject-data-application/define-command-argument-container

When you create a Pod, you can define a command and arguments for the containers that run in the Pod. To define a command, include the command field in the configuration file. To define arguments for the command, include the args field in the configuration file. The command and arguments that you define cannot be changed after the Pod is created.

The command and arguments that you define in the configuration file override the default command and arguments provided by the container image. If you define args, but do not define a command, the default command is used with your new arguments.