kubernetes Clusterip+port is not reachable in some cases

3/25/2019

l tried to set up a dvwa environment in k8s, l found it not work as usual when l exposed the dvwa pods's port.

l tried exposing a nginx sample to make sure my k8s env is working. And yes nginx works well in my local machine

Here is some information

# dvwa.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: dvwa
spec:
  replicas: 2
  selector:
    app: dvwa
  template:
    metadata:
      labels:
        app: dvwa
    spec:
      containers:
        - name: dvwa
          image: citizenstig/dvwa:latest
          ports:
          - containerPort: 3306


# dvwa_service.yaml
apiVersion: v1
kind: Service
metadata:
  name: dvwa
spec:
  ports:
  - port: 3306
  selector:
    app: dvwa
  type: NodePort


$ kubectl get svc -o wide
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          
AGE     SELECTOR
dvwa              NodePort    10.98.238.130   <none>        
3306:32393/TCP   7m15s   app=dvwa
kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP          
35h     <none>
nginx             NodePort    10.97.143.32    <none>        
80:31961/TCP     5m51s   app=nginx

When l ssh into one of my k8s machine and typed curl 10.97.143.32:80, it returns the nginx page, but curl 10.98.238.130:3306 not works, the docker image is citizenstig/dvwa which expose 80 and 3306, l also tried using 80 port in dvwa_service.yaml but still not work.

Can anyone help me??? Thanks in advance!

-- Rosmee
docker
kubernetes

1 Answer

3/25/2019

targetPort is missing in your service definition

Try below service definition

apiVersion: v1
kind: Service
metadata:
  name: dvwa
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    app: dvwa
  type: NodePort

can you get into the dvwa pod and run localhost:3306 do you get any response?

-- P Ekambaram
Source: StackOverflow