Kubernetes services are not accessible through nodeport with Desktop Docker setup

拜拜、爱过 提交于 2021-02-05 10:56:35

问题


I am using Docker Desktop on windows 10. And I generate kubernetes NodePort Service to access from client web browser (http://10.110.201.24:30008/hello/praveen) but service is not accessible.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  selector:
    matchLabels:
      run: spring-app
  replicas: 1
  template:
    metadata:
      labels:
        run: spring-app
    spec:
      containers:
        - name: spring-boot
          image: praveen1233/spring-boot:v1
          ports:
            - containerPort: 80

service

apiVersion: v1
kind: Service
metadata:
  name: service
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30008

DockerFile

FROM openjdk:8u212-jdk-slim
LABEL maintainer="praveen.ambati33@gmail.com"
VOLUME /tmp
EXPOSE 5001
ARG JAR_FILE=target/spring-boot-app.jar
ADD ${JAR_FILE} spring-boot-app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/spring-boot-app.jar"]

I have wrote simple web based spring boot application as below

@RestController
public class HelloController {
    @GetMapping( value = "hello/{input}")
    public String getMessage(@PathVariable String input){
        return "Hi "+input;
    }
}

application.properties

server.port=5000

I could see everything looks good in terms of Deployment, Pod and Services status. However I am not able to access the application.

kubectl get all
NAME                            READY   STATUS             RESTARTS   AGE
pod/my-app-7b77675f79-wwbfl     1/1     Running            0          32m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        6d1h
service/service      NodePort    10.110.201.24   <none>        80:30008/TCP   23m

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/my-app     1/1     1            1           32m

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/my-app-7b77675f79     1         1         1       32m

I am guessing their could be different way of accessing ip in Docker Hub which I am not able figure it out! Could you please help. Appreciated !

Docker and k8s versions as below

Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:23:10 2020
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:29:16 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

回答1:


Your port setting is not straight.

You have port 80 in deployment yml, 5001 in DockerFile and 5000 in application properties. They should match.

Update

The other problem is that your Service doesn't have a selector. So the Service doesn't know to which pod the traffic should be routed.

spec:
  type: NodePort
  ports:
    - port: 5000
      targetPort: 5000
      nodePort: 30008
  selector:
    app: my-app

The Deploment has to have a corresponding label.

metadata:
  name: my-app-v1
  labels:
    app: my-app
    version: v1


来源:https://stackoverflow.com/questions/61762219/kubernetes-services-are-not-accessible-through-nodeport-with-desktop-docker-setu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!