Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes 无法在本地访问我的应用程序(路由不起作用)_Kubernetes_Devops_Kubernetes Ingress_Nginx Ingress - Fatal编程技术网

Kubernetes 无法在本地访问我的应用程序(路由不起作用)

Kubernetes 无法在本地访问我的应用程序(路由不起作用),kubernetes,devops,kubernetes-ingress,nginx-ingress,Kubernetes,Devops,Kubernetes Ingress,Nginx Ingress,我已经建立了一个部署、服务和入口,但是我无法在我的本地web上访问它(使用minikube) 我刚刚向我的github用户上传了一个随机HTML,而这个“应用程序”只是将包含HTML文件的git repo克隆到容器中,我尝试在本地访问它 我的部署文件:(我的容器100%工作正常)。 apiVersion: apps/v1 kind: Deployment metadata: name: test-deployment labels: app: nginx spec: r

我已经建立了一个部署、服务和入口,但是我无法在我的本地web上访问它(使用minikube)

我刚刚向我的github用户上传了一个随机HTML,而这个“应用程序”只是将包含HTML文件的git repo克隆到容器中,我尝试在本地访问它

我的部署文件:(我的容器100%工作正常)。

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: test-deployment
  labels: 
    app: nginx
spec: 
  replicas: 1
  selector: 
    matchLabels:
      app: nginx
  template: 
    metadata: 
      labels: 
        app: nginx
    spec:
      containers: 
      - name: ourapp
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: cdn
          mountPath: /usr/share/nginx/html
      initContainers:
      - name: init
        image: alpine/git:latest
        volumeMounts:
        - name: cdn
          mountPath: /home/cdn
        command: ["/bin/sh", "-c"]
        args: ['git clone $GIT_CLONE /home/cdn']
        env:
          - name: GIT_CLONE
            valueFrom:
              configMapKeyRef:
                name: config-map
                key: kvsource
      volumes:
      - name: cdn
        emptyDir: {}
我的服务文件:

apiVersion: v1
kind: Service
metadata:
  name: my-cip-service
spec:
  selector:
    app: nginx
  type: ClusterIP
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service: 
            name: my-cip-service
            port:
              number: 80 
我已经在Minikube上应用了ingress插件,并生成了以下ingress文件:

apiVersion: v1
kind: Service
metadata:
  name: my-cip-service
spec:
  selector:
    app: nginx
  type: ClusterIP
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service: 
            name: my-cip-service
            port:
              number: 80 
但是,我仍然无法通过localhost/address访问我的应用程序。。
我被困在这上面很长时间了。。您看到这里有什么问题吗?

首先,我认为您需要将您的服务
targetPort
更改为80。其次,在使用minikube时,您需要在系统的
hosts
文件中设置一个条目,其中入口主机(
localhost
)指向minikube节点ip


在您刚刚进行测试时,为了清晰起见,请将入口主机更改为类似于
example.com
。只需键入
minikube-ip
即可获得节点ip。因此,为
example.com
创建一个条目,指向
minikube ip
,并尝试在使用minikube时点击
example.com
,,这并不意味着可以在本地主机上访问您的应用程序

当为minikube启用ingress插件时,您可以在minikube VM ip下访问它(ingress controller)

您可以通过运行
minikube ip
命令来获取此ip

如果您想在本地使用自己的域(假设您在linux或mac上运行),请运行:

它将向主机文件添加一行,将domain.test指向minikube ip

现在,您可以在入口文件中使用此域:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: domain.test    # <- HERE ->
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service: 
            name: my-cip-service
            port:
              number: 80 
apiVersion:networking.k8s.io/v1 种类:入口 元数据: 名称:入口 规格: 规则: -主持人:domain.test# http: 路径: -路径:/ 路径类型:前缀 后端: 服务: 姓名:我的cip服务 端口: 电话:80
此外,您还有一个配置错误的服务。TargetPort字段应设置为应用程序正在侦听的端口号。据我所知,nginx image默认监听端口80,所以相关服务的targetPort应该设置为这个值


从现在起,您应该可以在下访问本地网站。

非常感谢您的帮助!我正在将Windows操作系统与WSL结合使用。。所以在ubuntu WSL上添加minikube IP可以让我接触到或者我必须在我的Windows OS主机文件上配置一些额外的东西?我不太了解WSL,但我发现如果你要在WSL中打开
/etc/hosts
文件,你应该会看到:
“此文件由WSL基于Windows主机文件自动生成:%WINDIR%\System32\drivers\etc\hosts。对该文件的修改将被覆盖。“
。因此,只需在windows中编辑
%WINDIR%\System32\drivers\etc\hosts
文件,并在其中添加域。这很奇怪,在我的hosts文件中添加域后,我仍然无法访问domain.test。。