简体   繁体   中英

Ubuntu: Cannot access Kubernetes service from outside cluster

I am new to K8s, and I am facing issues trying to connect to K8s NodePort service from outside the cluster.

I am unable to load the nginx default page when I try accessing it from my local machine using the URL: http://localhost:31008

I understand this is a common problem and I referred the below solutions,

Cannot access NodePort service outside Kubernetes cluster

Cannot access Microk8s service from browser using NodePort service

However none of them are working for me.

Any guidance on this issue would be really appreciated. Thank you.

Setup:

Server OS: Ubuntu Server on AWS

K8s: minikube

Below is my deployment YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: front-end
      name: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: front-end
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx

Below is my Service YAML:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: front-end
    name: nginx
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 31008

Below is the output of the command kubectl get all ,

在此处输入图片说明

There is an issue in the target port config as Nginx run on default port 80

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: front-end
    name: nginx
  ports:
  - port: 8080
    targetPort: 80
    nodePort: 31008

The target port should be 80

Config of Nginx :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80

Ref document : https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/

对于使用 docker + minikube 并面临上述问题的人,这里提供了解决方案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM