Search code examples
kubernetesminikubekubernetes-service

Kubernetes Service NodePort not connected


I am using minikube on windows 10. And I generate kubernetes NodePort Service to access from client web browser. First the below codes are resources configuration file.

apiVersion: v1
kind: Pod
metadata:
  name: blog-system 
  labels:
    app: blog-pod 
spec:
  containers:
  - name: blog-app
    image: app:latest 
    imagePullPolicy: Never 
    ports:
      - containerPort: 8080
    args: ["-t", "-i"]
  - name: blog-mysql
    image: mysql:latest 
    env:
      - name: MYSQL_ROOT_PASSWORD
        value: password
      - name: MYSQL_PASSWORD
        value: password
      - name: MYSQL_DATABASE
        value: test
    ports:
      - containerPort: 3306

---
apiVersion: v1
kind: Service
metadata:
  name: blog-app-svc
spec:
  type: NodePort   
  selector:
    app: blog-pod  
  ports:
  - name: http
    port: 8080
    protocol: TCP
    targetPort: 80
    nodePort: 31000

The Kubernetes pods and services are generated successfully.

> kubectl get services
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
blog-app-svc   NodePort    10.100.32.119   <none>        8080:31000/TCP   79s
kubernetes     ClusterIP   10.96.0.1       <none>        443/TCP          5m54s

> minikube service blog-app-svc --url
http://192.168.5.25:31000

However the access to this kubernetes nodeport service is rejected even with all of these urls.

http://10.100.32.119:31000
http://10.100.32.119:8080
http://192.168.5.25:31000

I have no idea where the errors are on the resources configuration file, service part or pod part. Any idea, please.


Solution

  • The targetPort of your Service doesn't match the containerPort of your Pod.

    Your Service declaration says that nodePort 31000 on any node in the cluster, or port 8080 on the special host name blog-app-svc.default.svc.cluster.local within the cluster, forwards to targetPort 80 in some pod with an app: blog-pod label. You have that pod, but it's listening to ports 8080 and 3306 and not port 80.

    If you swap port and targetPort then the default HTTP port 80 on the Service will forward to port 8080 in your pod, which is probably what you're after.