docker - Make ngnix image accessible in kubernetes pod in localhost - Stack Overflow

admin2025-04-16  3

I have the following deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deploy
spec:
  replicas: 10
  selector:
    matchLabels:
      app: hello-world
  revisionHistoryLimit: 5
  progressDeadlineSeconds: 300
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-pod
        image: nginx
        ports:
        - containerPort: 8080
        resources:
          limits:
            memory: 128Mi
            cpu: 0.1  

I check and all the pods are running:

$ kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
hello-deploy-7c547fdb4-2tpnc   1/1     Running   0          18m
hello-deploy-7c547fdb4-4kjtt   1/1     Running   0          18m
hello-deploy-7c547fdb4-6cvmt   1/1     Running   0          18m
hello-deploy-7c547fdb4-9gvj6   1/1     Running   0          18m
hello-deploy-7c547fdb4-ctjxx   1/1     Running   0          18m
hello-deploy-7c547fdb4-ggl6n   1/1     Running   0          18m
hello-deploy-7c547fdb4-m528n   1/1     Running   0          18m
hello-deploy-7c547fdb4-mg9gf   1/1     Running   0          18m
hello-deploy-7c547fdb4-t9srq   1/1     Running   0          18m
hello-deploy-7c547fdb4-zcjkt   1/1     Running   0          18m

I also have the following service defined:

apiVersion: v1
kind: Service
metadata:
  name: lb-svc
  labels:
    app: hello-world
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: hello-world

I check and it is also running:

$ kubect get svc lb-svc
NAME     TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
lb-svc   LoadBalancer   10.100.237.251   localhost     8080:31527/TCP   10m

But when I do in my chrome browser http://localhost:8080 I get This page isn't working ERR_EMPTY_RESPONSE

Kubernetes version

$ kubectl version
Client Version: v1.32.1
Kustomize Version: v5.5.0
Server Version: v1.30.5
WARNING: version difference between client (1.32) and server (1.30) exceeds the supported minor version skew of +/-1

What am I doing wrong?

I have the following deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deploy
spec:
  replicas: 10
  selector:
    matchLabels:
      app: hello-world
  revisionHistoryLimit: 5
  progressDeadlineSeconds: 300
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-pod
        image: nginx
        ports:
        - containerPort: 8080
        resources:
          limits:
            memory: 128Mi
            cpu: 0.1  

I check and all the pods are running:

$ kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
hello-deploy-7c547fdb4-2tpnc   1/1     Running   0          18m
hello-deploy-7c547fdb4-4kjtt   1/1     Running   0          18m
hello-deploy-7c547fdb4-6cvmt   1/1     Running   0          18m
hello-deploy-7c547fdb4-9gvj6   1/1     Running   0          18m
hello-deploy-7c547fdb4-ctjxx   1/1     Running   0          18m
hello-deploy-7c547fdb4-ggl6n   1/1     Running   0          18m
hello-deploy-7c547fdb4-m528n   1/1     Running   0          18m
hello-deploy-7c547fdb4-mg9gf   1/1     Running   0          18m
hello-deploy-7c547fdb4-t9srq   1/1     Running   0          18m
hello-deploy-7c547fdb4-zcjkt   1/1     Running   0          18m

I also have the following service defined:

apiVersion: v1
kind: Service
metadata:
  name: lb-svc
  labels:
    app: hello-world
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: hello-world

I check and it is also running:

$ kubect get svc lb-svc
NAME     TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
lb-svc   LoadBalancer   10.100.237.251   localhost     8080:31527/TCP   10m

But when I do in my chrome browser http://localhost:8080 I get This page isn't working ERR_EMPTY_RESPONSE

Kubernetes version

$ kubectl version
Client Version: v1.32.1
Kustomize Version: v5.5.0
Server Version: v1.30.5
WARNING: version difference between client (1.32) and server (1.30) exceeds the supported minor version skew of +/-1

What am I doing wrong?

Share edited Feb 2 at 10:44 Jim asked Feb 1 at 19:38 JimJim 4,4354 gold badges30 silver badges60 bronze badges 6
  • localhost is a very context-sensitive name; if you're typing it into your browser, it refers to the machine the browser is running on, not any of the Kubernetes Pods or the Nodes they're running on, each of which separately believes they are localhost. What sort of Kubernetes installation and load balancer provider are you using? – David Maze Commented Feb 1 at 19:59
  • @DavidMaze: I used localhost because that is what is mentioned in the EXTERNAL-IP of the kybectl get svc. Since I am using Docker Engine with Kubernetes enabled. How do I check what load balancer provider am I using? I am on Kubuntu btw – Jim Commented Feb 1 at 20:31
  • Why vote to close? I have provided what I have used so this setup is reproducable – Jim Commented Feb 1 at 21:08
  • by default nginx image is running on port 80 not 8080 – shadowlegend Commented Feb 2 at 16:36
  • @shadowlegend: so my configuration in the deployment for containerport has no effect? – Jim Commented Feb 2 at 20:41
 |  Show 1 more comment

1 Answer 1

Reset to default 0

by default nginx image is running on port 80 not 8080

转载请注明原文地址:http://anycun.com/QandA/1744817829a88034.html