简体   繁体   中英

Kubernetes Service Nodeport is not working

I have created two PODs running in my minikube environment in windows 10. One POD with Spring boot application container and another POD with mysql container. For Spring boot application POD with service type is nodePort and for MYSQL pod, service with type is clusterIP. Means Mysql pod needs to communicate inside the cluster only. But for Spring boot application needs to access from browser so i configured NodePort.

i configured 30096 for NodePort. But i checked in my browser (minikube ip:nodePort).Its not working. So i try to execute the command "minikube service service-name" and output shows one different port(59870)

在此处输入图像描述

MYSQL_DEPLOYMENT_YML:

    apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: bcp-mysql
  labels:
    app: bcp-mysql
spec:
  selector:
    matchLabels:
      app: bcp-mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: bcp-mysql
    spec:
      containers:
      - image: <myrepo>/mysql:5.7
        name: mysql
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: password
          - name: MYSQL_DATABASE
            value: database
          - name: MYSQL_USER
            value: root
          - name: MYSQL_PASSWORD
            value: password
        ports:
        - containerPort: 3306
          name: mysql
        imagePullPolicy: Always
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
      imagePullSecrets:
        - name: regcred
---
apiVersion: v1
kind: Service
metadata:
  name: bcp-mysql
spec:
  selector:
    app: bcp-mysql
  ports:
    - port: 3306
      targetPort: 3306
  type: ClusterIP
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: bcp-mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

Spring_boot_deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bcpdashboard
  labels: 
    app: bcpdashboard
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bcpdashboard
  template:
    metadata:
      labels:
        app: bcpdashboard
    spec:
      containers:
        - name: app
          image: <myrepo>/bcpdashboard:latest
          ports:
            - containerPort: 9097
          imagePullPolicy: Always
          env:
          - name: SPRING_DATASOURCE_URL
            value: jdbc:mysql://bcp-mysql:3306/bcp?autoReconnect=true&useSSL=false
      imagePullSecrets:
        - name: regcred
---

apiVersion: v1  
kind: Service  
metadata:  
  name: bcpdashboard  
spec:
  selector:
    app: bcpdashboard
  ports:
    - port: 9097
      targetPort: 9097
      nodePort: 30096
  type: NodePort
---

在此处输入图像描述

My minikube ip is localhost IP only. So i try to run ( http://127.0.0.1:30096 ). Its not working. So i try to execute "minikube service bcpdashboard"

在此处输入图像描述

And http://127.0.0.1:59870/ is working fine and i dont know about the port number 59870. How it is assigned automatically and start point to spring boot application POD even i configured nodeport. 在此处输入图像描述

在此处输入图像描述 在此处输入图像描述

Anyone face the same issue & please let me know how to make it work with nodePort and also is there any issues in deployment yaml configurations.

Seems like the behavior is specific to your setup of minikube, using your deployment yaml in my setup, everything functions as it's stated, ie only curl http://127.0.0.1:30096/ responds w/ 200.

I used helloworld-http as the image, and k8s 16 w/ docker-desktop 2.3. The deployment yaml looks something this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bcpdashboard
  labels:
    app: bcpdashboard
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bcpdashboard
  template:
    metadata:
      labels:
        app: bcpdashboard
    spec:
      containers:
        - name: app
          image: strm/helloworld-http
          ports:
            - containerPort: 80
          imagePullPolicy: Always
          env:
          - name: SPRING_DATASOURCE_URL
            value: jdbc:mysql://bcp-mysql:3306/bcp?autoReconnect=true&useSSL=false
      imagePullSecrets:
        - name: regcred
---

apiVersion: v1
kind: Service
metadata:
  name: bcpdashboard
spec:
  selector:
    app: bcpdashboard
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30096
  type: NodePort
---

Have you tried using docker-windows-desktop-kubernetes ? You could give that a try, it will atleast isolate the problem for you.

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