Windows 10 上的 MiniKube 端口转发不工作
Port Forward Not Working in MiniKube on Windows 10
我正在我的笔记本电脑 (Windows 10 OS) 上设置 kubernetes 以处理容器及其编排。 我使用以下命令创建了 Minikube VM,它成功了。
minikube start --vm-driver hyperv --hyperv-virtual-switch "Primary Virtual Switch"
我能够启动 Kubernetes,也能够启动 Minikube 仪表板
我启动了 Kubernetes 集群并将 nginx 应用程序部署到集群中。以下是命令。
kubectl run hello-nginx --image=nginx --port=8020
kubectl expose deployment hello-nginx --type=NodePort --port=8020 --target-port=8020
我可以使用以下命令查看 PODs 和服务。
kubectl get pods
kubectl get services
到这里都可以正常工作。我能够在 Minikube 仪表板中查看部署、PODs 和服务信息。
当我运行以下命令在浏览器中启动应用程序时,浏览器会抛出一条消息"Resource Not Found",但我能够在 MiniKube 仪表板中查看 POD 和服务信息。
minikube service hello-nginx
URL: http://192.168.43.20:32087/
在浏览器中出现异常
This website could not be found.
Error Code: INET_E_RESOURCE_NOT_FOUND
下面给出了部署 YAML 文件,
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "hello-nginx",
"namespace": "default",
"selfLink": "/apis/extensions/v1beta1/namespaces/default/deployments/hello-nginx",
"uid": "5629038e-93e5-11e9-ad2e-00155d162e0e",
"resourceVersion": "49313",
"generation": 1,
"creationTimestamp": "2019-06-21T05:28:01Z",
"labels": {
"run": "hello-nginx"
},
"annotations": {
"deployment.kubernetes.io/revision": "1"
}
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"run": "hello-nginx"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "hello-nginx"
}
},
"spec": {
"containers": [
{
"name": "hello-nginx",
"image": "nginx",
"ports": [
{
"containerPort": 8020,
"protocol": "TCP"
}
],
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {},
"schedulerName": "default-scheduler"
}
},
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxUnavailable": 1,
"maxSurge": 1
}
},
"revisionHistoryLimit": 2147483647,
"progressDeadlineSeconds": 2147483647
},
"status": {
"observedGeneration": 1,
"replicas": 1,
"updatedReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
"conditions": [
{
"type": "Available",
"status": "True",
"lastUpdateTime": "2019-06-21T05:28:01Z",
"lastTransitionTime": "2019-06-21T05:28:01Z",
"reason": "MinimumReplicasAvailable",
"message": "Deployment has minimum availability."
}
]
}
}
下面给出了副本集 YAML 文件信息,
{
"kind": "ReplicaSet",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "hello-nginx-76696c698f",
"namespace": "default",
"selfLink": "/apis/extensions/v1beta1/namespaces/default/replicasets/hello-nginx-76696c698f",
"uid": "562be1e8-93e5-11e9-ad2e-00155d162e0e",
"resourceVersion": "49310",
"generation": 3,
"creationTimestamp": "2019-06-21T05:28:01Z",
"labels": {
"pod-template-hash": "76696c698f",
"run": "hello-nginx"
},
"annotations": {
"deployment.kubernetes.io/desired-replicas": "1",
"deployment.kubernetes.io/max-replicas": "2",
"deployment.kubernetes.io/revision": "1"
},
"ownerReferences": [
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"name": "hello-nginx",
"uid": "5629038e-93e5-11e9-ad2e-00155d162e0e",
"controller": true,
"blockOwnerDeletion": true
}
]
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"pod-template-hash": "76696c698f",
"run": "hello-nginx"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"pod-template-hash": "76696c698f",
"run": "hello-nginx"
}
},
"spec": {
"containers": [
{
"name": "hello-nginx",
"image": "nginx",
"ports": [
{
"containerPort": 8020,
"protocol": "TCP"
}
],
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {},
"schedulerName": "default-scheduler"
}
}
},
"status": {
"replicas": 1,
"fullyLabeledReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
"observedGeneration": 3
}
}
现在我正在尝试将请求路由到 POD 的端口转发选项,但它不起作用。
kubectl port-forward deployment/hello-nginx 8020:8020
当我尝试访问 URL“http://127.0.0.1:8020”
时出现以下异常
Handling connection for 8020
E0622 01:07:06.306320 18888 portforward.go:331] an error occurred forwarding 8020 -> 8020: error forwarding port 8020 to pod c54d6faaa545992dce02f58490a26154134843eb7426a51e78df2cda172b514c, uid : exit status 1: 2019/06/21 08:01:18 socat[4535] E connect(5, AF=2 127.0.0.1:8020, 16): Connection refused
我已经阅读了很多这方面的文章,但找不到这个问题的根本原因。我在这里遗漏了什么重要的东西吗?
感谢您的提前帮助。
您的问题实际上与Minikube或端口转发无关。您公开端口 8020
,但应用程序 hello-nginx
使用 80
。所以你应该到处使用 80
而不是 8020
。例如:
kubectl run hello-nginx --image=nginx --port=80
也就是说,使用 Minikube 并不是 Windows 的最佳选择。更好的是使用 Docker Desktop,然后你 运行 在 Kubernetes 上的所有东西都可以在你的 localhost
.
上使用
我正在我的笔记本电脑 (Windows 10 OS) 上设置 kubernetes 以处理容器及其编排。 我使用以下命令创建了 Minikube VM,它成功了。
minikube start --vm-driver hyperv --hyperv-virtual-switch "Primary Virtual Switch"
我能够启动 Kubernetes,也能够启动 Minikube 仪表板
我启动了 Kubernetes 集群并将 nginx 应用程序部署到集群中。以下是命令。
kubectl run hello-nginx --image=nginx --port=8020
kubectl expose deployment hello-nginx --type=NodePort --port=8020 --target-port=8020
我可以使用以下命令查看 PODs 和服务。
kubectl get pods
kubectl get services
到这里都可以正常工作。我能够在 Minikube 仪表板中查看部署、PODs 和服务信息。
当我运行以下命令在浏览器中启动应用程序时,浏览器会抛出一条消息"Resource Not Found",但我能够在 MiniKube 仪表板中查看 POD 和服务信息。
minikube service hello-nginx
URL: http://192.168.43.20:32087/
在浏览器中出现异常
This website could not be found.
Error Code: INET_E_RESOURCE_NOT_FOUND
下面给出了部署 YAML 文件,
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "hello-nginx",
"namespace": "default",
"selfLink": "/apis/extensions/v1beta1/namespaces/default/deployments/hello-nginx",
"uid": "5629038e-93e5-11e9-ad2e-00155d162e0e",
"resourceVersion": "49313",
"generation": 1,
"creationTimestamp": "2019-06-21T05:28:01Z",
"labels": {
"run": "hello-nginx"
},
"annotations": {
"deployment.kubernetes.io/revision": "1"
}
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"run": "hello-nginx"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "hello-nginx"
}
},
"spec": {
"containers": [
{
"name": "hello-nginx",
"image": "nginx",
"ports": [
{
"containerPort": 8020,
"protocol": "TCP"
}
],
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {},
"schedulerName": "default-scheduler"
}
},
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxUnavailable": 1,
"maxSurge": 1
}
},
"revisionHistoryLimit": 2147483647,
"progressDeadlineSeconds": 2147483647
},
"status": {
"observedGeneration": 1,
"replicas": 1,
"updatedReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
"conditions": [
{
"type": "Available",
"status": "True",
"lastUpdateTime": "2019-06-21T05:28:01Z",
"lastTransitionTime": "2019-06-21T05:28:01Z",
"reason": "MinimumReplicasAvailable",
"message": "Deployment has minimum availability."
}
]
}
}
下面给出了副本集 YAML 文件信息,
{
"kind": "ReplicaSet",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "hello-nginx-76696c698f",
"namespace": "default",
"selfLink": "/apis/extensions/v1beta1/namespaces/default/replicasets/hello-nginx-76696c698f",
"uid": "562be1e8-93e5-11e9-ad2e-00155d162e0e",
"resourceVersion": "49310",
"generation": 3,
"creationTimestamp": "2019-06-21T05:28:01Z",
"labels": {
"pod-template-hash": "76696c698f",
"run": "hello-nginx"
},
"annotations": {
"deployment.kubernetes.io/desired-replicas": "1",
"deployment.kubernetes.io/max-replicas": "2",
"deployment.kubernetes.io/revision": "1"
},
"ownerReferences": [
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"name": "hello-nginx",
"uid": "5629038e-93e5-11e9-ad2e-00155d162e0e",
"controller": true,
"blockOwnerDeletion": true
}
]
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"pod-template-hash": "76696c698f",
"run": "hello-nginx"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"pod-template-hash": "76696c698f",
"run": "hello-nginx"
}
},
"spec": {
"containers": [
{
"name": "hello-nginx",
"image": "nginx",
"ports": [
{
"containerPort": 8020,
"protocol": "TCP"
}
],
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {},
"schedulerName": "default-scheduler"
}
}
},
"status": {
"replicas": 1,
"fullyLabeledReplicas": 1,
"readyReplicas": 1,
"availableReplicas": 1,
"observedGeneration": 3
}
}
现在我正在尝试将请求路由到 POD 的端口转发选项,但它不起作用。
kubectl port-forward deployment/hello-nginx 8020:8020
当我尝试访问 URL“http://127.0.0.1:8020”
时出现以下异常Handling connection for 8020
E0622 01:07:06.306320 18888 portforward.go:331] an error occurred forwarding 8020 -> 8020: error forwarding port 8020 to pod c54d6faaa545992dce02f58490a26154134843eb7426a51e78df2cda172b514c, uid : exit status 1: 2019/06/21 08:01:18 socat[4535] E connect(5, AF=2 127.0.0.1:8020, 16): Connection refused
我已经阅读了很多这方面的文章,但找不到这个问题的根本原因。我在这里遗漏了什么重要的东西吗?
感谢您的提前帮助。
您的问题实际上与Minikube或端口转发无关。您公开端口 8020
,但应用程序 hello-nginx
使用 80
。所以你应该到处使用 80
而不是 8020
。例如:
kubectl run hello-nginx --image=nginx --port=80
也就是说,使用 Minikube 并不是 Windows 的最佳选择。更好的是使用 Docker Desktop,然后你 运行 在 Kubernetes 上的所有东西都可以在你的 localhost
.