我尝试发送请求192.168.49.2:31083,但没有成功。我正在将 Kubernetes 与 Docker 结合使用。
kubectl expose deployment k8s-web-hello --port=3000 --type=NodePort
kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
k8s-web-hello NodePort 10.109.32.222 <none> 3000:31083/TCP 9m54s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d
minikube ip
192.168.49.2
如何解决?
编辑:
minikube service k8s-web-hello
|-----------|---------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|---------------|-------------|---------------------------|
| default | k8s-web-hello | 3000 | http://192.168.49.2:31083 |
|-----------|---------------|-------------|---------------------------|
🏃 Starting tunnel for service k8s-web-hello.
|-----------|---------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|---------------|-------------|------------------------|
| default | k8s-web-hello | | http://127.0.0.1:45767 |
|-----------|---------------|-------------|------------------------|
🎉 Opening service default/k8s-web-hello in default browser...
❗ Because you are using a Docker driver on linux, the terminal needs to be open to run it.
Opening in existing browser session.
我不明白为什么我无法通过第一个带有 Minikube 虚拟机 IP 地址的 URL 访问,但第二个带有本地 IP 的 URL 可以访问?
请参阅此官方文档,根据此您需要在 yaml 中使用“type: NodePort”来指定 NodePort 值,并且您只需要使用此 NodePort 值进行curl。
以下是示例:
apiVersion: v1 kind: Service metadata: name: my-service spec: type: NodePort selector: app.kubernetes.io/name: MyApp ports: # By default and for convenience, the `targetPort` is set to the same value as the `port` field. - port: 80 targetPort: 80 # Optional field # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767) nodePort: 30007
请注意,当您使用 minikube IP 时,您需要隧道进入 Minikube 才能访问。
我可以知道您是如何通过 Curl 或使用 HTTP 浏览发送请求吗?您能否也分享一下您的 yaml,以便更容易回答。