gpt4 book ai didi

kubernetes - 无法从 minikube 获取 ClusterIP 服务 url

转载 作者:行者123 更新时间:2023-12-02 12:12:57 25 4
gpt4 key购买 nike

我根据下面的配置文件创建了一个 ClusterIP 服务,但是我似乎无法从 minikube 获取该服务的 URL

k create -f service-cluster-definition.yaml
➜ minikube service myapp-frontend --url
😿 service default/myapp-frontend has no node port

如果我尝试将 NodePort 添加到 端口 service-cluster-definition.yaml 的部分它提示错误,不推荐使用此类 key 。

我错过了什么或做错了什么?

服务集群定义.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-frontend
spec:
type: ClusterIP
ports:
- targetPort: 80
port: 80
selector:
app: myapp
type: etl


部署定义.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
app: myapp
env: experiment
type: etl
spec:
template:
metadata:
name: myapp-pod
labels:
app: myapp
env: experiment
type: etl
spec:
containers:
- name: nginx-container
image: nginx:1.7.1
replicas: 3
selector:
matchLabels:
type: etl
➜ k get pods --selector="app=myapp,type=etl" -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deployment-59856c4487-2g9c7 1/1 Running 0 45m 172.17.0.9 minikube <none> <none>
myapp-deployment-59856c4487-mb28z 1/1 Running 0 45m 172.17.0.4 minikube <none> <none>
myapp-deployment-59856c4487-sqxqg 1/1 Running 0 45m 172.17.0.8 minikube <none> <none>


(⎈ |minikube:default)
Projects/experiments/kubernetes
➜ k version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.3", GitCommit:"06ad960bfd03b39c8310aaf92d1e7c12ce618213", GitTreeState:"clean", BuildDate:"2020-02-11T18:14:22Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.3", GitCommit:"06ad960bfd03b39c8310aaf92d1e7c12ce618213", GitTreeState:"clean", BuildDate:"2020-02-11T18:07:13Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"linux/amd64"}
(⎈ |minikube:default)

最佳答案

首先让我们理清Documentation中的一些概念。 :

  • ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster.

  • NodePort: Exposes the Service on each Node’s IP at a static port (the NodePort). You’ll be able to contact the NodePort Service, from outside the cluster, by requesting NodeIP:NodePort.



问题一:

I have created a ClusterIP service according to configuration files below, however I can't seem to get the URL from minikube for that service.


  • 由于 Minikube 是单个主机上的虚拟化环境,我们往往会忘记集群与主机计算机是隔离的。如果您将服务设置为 ClusterIP , Minikube 不会给外部访问权限。

  • 问题二:

    And if I try to add NodePort into the ports section of service-cluster-definition.yaml it complains with error, that such key is deprecated.


  • 也许你粘贴在错误的位置。您应该只替换字段 type: ClusterIP对于 type: NodePort .这是您的 yaml 的正确形式:

  • apiVersion: v1
    kind: Service
    metadata:
    name: myapp-frontend
    spec:
    type: NodePort
    ports:
    - targetPort: 80
    port: 80
    selector:
    app: myapp
    type: etl

    转载:
    user@minikube:~$ kubectl apply -f deployment-definition.yaml 
    deployment.apps/myapp-deployment created

    user@minikube:~$ kubectl get pods
    NAME READY STATUS RESTARTS AGE
    myapp-deployment-59856c4487-7dw6x 1/1 Running 0 5m11s
    myapp-deployment-59856c4487-th7ff 1/1 Running 0 5m11s
    myapp-deployment-59856c4487-zvm5f 1/1 Running 0 5m11s

    user@minikube:~$ kubectl apply -f service-cluster-definition.yaml
    service/myapp-frontend created

    user@minikube:~$ kubectl get service myapp-frontend
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    myapp-frontend NodePort 10.101.156.113 <none> 80:32420/TCP 3m43s

    user@minikube:~$ minikube service list
    |-------------|----------------|-----------------------------|-----|
    | NAMESPACE | NAME | TARGET PORT | URL |
    |-------------|----------------|-----------------------------|-----|
    | default | kubernetes | No node port | |
    | default | myapp-frontend | http://192.168.39.219:32420 | |
    | kube-system | kube-dns | No node port | |
    |-------------|----------------|-----------------------------|-----|

    user@minikube:~$ minikube service myapp-frontend --url
    http://192.168.39.219:32420

    user@minikube:~$ curl http://192.168.39.219:32420
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    ...{{output suppressed}}...
  • 如您所见,服务设置为 NodePort minikube 使用 MinikubeIP:NodePort 开始为应用程序提供服务将连接路由到匹配的 pod。
  • 请注意 nodeport默认在 30000:32767
  • 之间选择

    如果您有任何问题,请在评论中告诉我。

    关于kubernetes - 无法从 minikube 获取 ClusterIP 服务 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60556096/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com