带有 AspNet Core Web API 的容器在 Docker 上运行,但不在 AKS Kubernetes 上运行
Container with AspNet Core Web API runs on Docker but not on AKS Kubernetes
我有一个 ASP.Net Core Web API 2.2 项目,通常 运行 在我的本地 Docker 桌面上。我正在尝试 运行 它在 Azure 的 AKS 上,但它不会 运行 那里,我不明白为什么。
下面是我用来将项目发布到 app
目录的 PowerShell 脚本,该目录稍后将插入到容器中:
Remove-Item ..\..\..\..\projects\MyProject.Selenium.Commom\src\Selenium.API\bin\Release\* -Force -Recurse
dotnet publish ..\..\..\..\projects\MyProject.Selenium.Commom\src\Selenium.Comum.sln -c Release -r linux-musl-x64
$path = (Get-Location).ToString() + "\app"
if (Test-Path($path))
{
Remove-Item -Path $path -Force -Recurse
}
New-Item -ItemType Directory -Force app
Get-ChildItem ..\..\..\..\projects\MyProject.Selenium.Commom\src\Selenium.API\bin\Release\netcoreapp2.2\linux-musl-x64\publish\* | Copy-Item -Destination .\app -Recurse
这是我的 Dockerfile
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-alpine3.9
WORKDIR /app /app
WORKDIR /app
ENTRYPOINT ["dotnet", "Selenium.API.dll"]
下面是我的 Docker 构建命令:
docker build -t mylocaldocker/selenium-web-app:latest -t mylocaldocker/selenium-web-app:v0.0.2 .
还有我的Docker运行命令
docker run --name selweb --detach --rm -p 85:80 mylocaldocker/selenium-web-app:latest
一切都顺利进行,我可以毫无问题地在本地端口 85 上发送请求(IIS 正在使用端口 80)
但是,在 Azure 的 AKS 上执行类似的过程,容器不会启动。我使用相同的 PowerShell 脚本来发布我的应用程序,并且 dockerfile 也是相同的。我的构建命令发生变化,以便我可以推送到 Azure 的 Docker 注册表:
docker build -t myproject.azurecr.io/selenium-web-app:latest -t myproject.azurecr.io/selenium-web-app:v0.0.1 .
我登录到 Azure Docker 注册表,并将图像推送到它:
docker push myproject.azurecr.io/selenium-web-app:latest
我已经创建了我的 AKS 集群并授予了从我的注册表中提取图像的权限。我尝试使用以下命令 运行 AKS 上的图像:
kubectl run seleniumweb --image myproject.azurecr.io/selenium-web-app:latest --port 80
然后我收到回复
deployment.apps "seleniumweb" created
然而,当我得到 运行ning pods:
kubectl get pods
我的 pod 上出现错误状态
NAME READY STATUS RESTARTS AGE
seleniumweb-7b5f645698-9g7f6 0/1 Error 4 1m
当我从 pod 获取日志时:
kubectl logs seleniumweb-7b5f645698-9g7f6
我回来了:
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
下面是 pod 的 kubectl describe 结果:
kubectl describe pods
Name: seleniumweb-7b5f645698-9g7f6
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: aks-agentpool-41564776-0/10.240.0.4
Start Time: Sun, 02 Jun 2019 11:40:47 -0300
Labels: pod-template-hash=7b5f645698
run=seleniumweb
Annotations: <none>
Status: Running
IP: 10.240.0.25
Controlled By: ReplicaSet/seleniumweb-7b5f645698
Containers:
seleniumweb:
Container ID: docker://1d548f4934632efb0b7c5a59dd0ac2bd173f2ee8fa5196b45d480fb10e88a536
Image: myproject.azurecr.io/selenium-web-app:latest
Image ID: docker-pullable://myproject.azurecr.io/selenium-web-app@sha256:97e2915a8b43aa8e726799b76274bb9b5b852cb6c78a8630005997e310cfd41a
Port: 80/TCP
Host Port: 0/TCP
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 145
Started: Sun, 02 Jun 2019 11:43:39 -0300
Finished: Sun, 02 Jun 2019 11:43:39 -0300
Ready: False
Restart Count: 5
Environment:
KUBERNETES_PORT_443_TCP_ADDR: myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io
KUBERNETES_PORT: tcp://myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io:443
KUBERNETES_PORT_443_TCP: tcp://myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io:443
KUBERNETES_SERVICE_HOST: myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-mhvfv (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-mhvfv:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-mhvfv
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 5m default-scheduler Successfully assigned default/seleniumweb-7b5f645698-9g7f6 to aks-agentpool-41564776-0
Normal Created 4m (x4 over 5m) kubelet, aks-agentpool-41564776-0 Created container
Normal Started 4m (x4 over 5m) kubelet, aks-agentpool-41564776-0 Started container
Normal Pulling 3m (x5 over 5m) kubelet, aks-agentpool-41564776-0 pulling image "myproject.azurecr.io/selenium-web-app:latest"
Normal Pulled 3m (x5 over 5m) kubelet, aks-agentpool-41564776-0 Successfully pulled image "myproject.azurecr.io/selenium-web-app:latest"
Warning BackOff 20s (x24 over 5m) kubelet, aks-agentpool-41564776-0 Back-off restarting failed container
而且我不明白为什么,因为在我的本地 Docker 上一切 运行 都很好。任何帮助将不胜感激。谢谢
那个 Dockerfile 看起来很有趣。它什么都不做。 WORKDIR 只是 "sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile"(来自 docs.docker.com)。所以你设置了两次工作目录,然后没有别的。然后入口点将指向一个不存在的 .dll,因为您从未复制过它。我认为您想删除第一个 WORKDIR 命令并将其添加到剩余的 WORKDIR 命令之后:
COPY . ./
更好的是,使用 two stage build 构建在 docker 上,然后将构建复制到已发布的 运行time 图像。
我不知道为什么 docker 运行 在本地为您工作。它是否以某种方式拾取旧图像?根据您的 Dockerfile,它不应该 运行.
我有一个 ASP.Net Core Web API 2.2 项目,通常 运行 在我的本地 Docker 桌面上。我正在尝试 运行 它在 Azure 的 AKS 上,但它不会 运行 那里,我不明白为什么。
下面是我用来将项目发布到 app
目录的 PowerShell 脚本,该目录稍后将插入到容器中:
Remove-Item ..\..\..\..\projects\MyProject.Selenium.Commom\src\Selenium.API\bin\Release\* -Force -Recurse
dotnet publish ..\..\..\..\projects\MyProject.Selenium.Commom\src\Selenium.Comum.sln -c Release -r linux-musl-x64
$path = (Get-Location).ToString() + "\app"
if (Test-Path($path))
{
Remove-Item -Path $path -Force -Recurse
}
New-Item -ItemType Directory -Force app
Get-ChildItem ..\..\..\..\projects\MyProject.Selenium.Commom\src\Selenium.API\bin\Release\netcoreapp2.2\linux-musl-x64\publish\* | Copy-Item -Destination .\app -Recurse
这是我的 Dockerfile
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-alpine3.9
WORKDIR /app /app
WORKDIR /app
ENTRYPOINT ["dotnet", "Selenium.API.dll"]
下面是我的 Docker 构建命令:
docker build -t mylocaldocker/selenium-web-app:latest -t mylocaldocker/selenium-web-app:v0.0.2 .
还有我的Docker运行命令
docker run --name selweb --detach --rm -p 85:80 mylocaldocker/selenium-web-app:latest
一切都顺利进行,我可以毫无问题地在本地端口 85 上发送请求(IIS 正在使用端口 80)
但是,在 Azure 的 AKS 上执行类似的过程,容器不会启动。我使用相同的 PowerShell 脚本来发布我的应用程序,并且 dockerfile 也是相同的。我的构建命令发生变化,以便我可以推送到 Azure 的 Docker 注册表:
docker build -t myproject.azurecr.io/selenium-web-app:latest -t myproject.azurecr.io/selenium-web-app:v0.0.1 .
我登录到 Azure Docker 注册表,并将图像推送到它:
docker push myproject.azurecr.io/selenium-web-app:latest
我已经创建了我的 AKS 集群并授予了从我的注册表中提取图像的权限。我尝试使用以下命令 运行 AKS 上的图像:
kubectl run seleniumweb --image myproject.azurecr.io/selenium-web-app:latest --port 80
然后我收到回复
deployment.apps "seleniumweb" created
然而,当我得到 运行ning pods:
kubectl get pods
我的 pod 上出现错误状态
NAME READY STATUS RESTARTS AGE
seleniumweb-7b5f645698-9g7f6 0/1 Error 4 1m
当我从 pod 获取日志时:
kubectl logs seleniumweb-7b5f645698-9g7f6
我回来了:
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
下面是 pod 的 kubectl describe 结果:
kubectl describe pods
Name: seleniumweb-7b5f645698-9g7f6
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: aks-agentpool-41564776-0/10.240.0.4
Start Time: Sun, 02 Jun 2019 11:40:47 -0300
Labels: pod-template-hash=7b5f645698
run=seleniumweb
Annotations: <none>
Status: Running
IP: 10.240.0.25
Controlled By: ReplicaSet/seleniumweb-7b5f645698
Containers:
seleniumweb:
Container ID: docker://1d548f4934632efb0b7c5a59dd0ac2bd173f2ee8fa5196b45d480fb10e88a536
Image: myproject.azurecr.io/selenium-web-app:latest
Image ID: docker-pullable://myproject.azurecr.io/selenium-web-app@sha256:97e2915a8b43aa8e726799b76274bb9b5b852cb6c78a8630005997e310cfd41a
Port: 80/TCP
Host Port: 0/TCP
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 145
Started: Sun, 02 Jun 2019 11:43:39 -0300
Finished: Sun, 02 Jun 2019 11:43:39 -0300
Ready: False
Restart Count: 5
Environment:
KUBERNETES_PORT_443_TCP_ADDR: myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io
KUBERNETES_PORT: tcp://myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io:443
KUBERNETES_PORT_443_TCP: tcp://myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io:443
KUBERNETES_SERVICE_HOST: myprojectus-dns-54302b78.hcp.eastus2.azmk8s.io
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-mhvfv (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-mhvfv:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-mhvfv
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 5m default-scheduler Successfully assigned default/seleniumweb-7b5f645698-9g7f6 to aks-agentpool-41564776-0
Normal Created 4m (x4 over 5m) kubelet, aks-agentpool-41564776-0 Created container
Normal Started 4m (x4 over 5m) kubelet, aks-agentpool-41564776-0 Started container
Normal Pulling 3m (x5 over 5m) kubelet, aks-agentpool-41564776-0 pulling image "myproject.azurecr.io/selenium-web-app:latest"
Normal Pulled 3m (x5 over 5m) kubelet, aks-agentpool-41564776-0 Successfully pulled image "myproject.azurecr.io/selenium-web-app:latest"
Warning BackOff 20s (x24 over 5m) kubelet, aks-agentpool-41564776-0 Back-off restarting failed container
而且我不明白为什么,因为在我的本地 Docker 上一切 运行 都很好。任何帮助将不胜感激。谢谢
那个 Dockerfile 看起来很有趣。它什么都不做。 WORKDIR 只是 "sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile"(来自 docs.docker.com)。所以你设置了两次工作目录,然后没有别的。然后入口点将指向一个不存在的 .dll,因为您从未复制过它。我认为您想删除第一个 WORKDIR 命令并将其添加到剩余的 WORKDIR 命令之后:
COPY . ./
更好的是,使用 two stage build 构建在 docker 上,然后将构建复制到已发布的 运行time 图像。
我不知道为什么 docker 运行 在本地为您工作。它是否以某种方式拾取旧图像?根据您的 Dockerfile,它不应该 运行.