LoadBalancer service not reachable

2019-07-25 21:50发布

问题:

I have a very simple web app based on HTML, javascript + little bit jquery, angularjs. It is tested locally on eclipse Jee and on Tomcat and working fine. And its image is working fine on docker locally.

I can access on browser using localhost:8080/xxxx, 127.0.0.1:8080/xxxx, 0.0.0.0:8080. But when I deploy to google Kubernetes, I'm getting "This site can not be reached" if I use the external IP on the browser. I can ping my external IP, but curl is not working. It's not a firewall issue because sample voting app from dockerhub is working fine on my Kubernetes.

my Dockerfile:

FROM tomcat:9.0
ADD GeoWebv3.war /usr/local/tomcat/webapps/GeoWeb.war
expose 8080

my pod yaml

apiVersion: v1
kind: Pod
metadata:   
   name: front-app-pod  
labels:      
   name: front-app-pod     
   app: demo-geo-app
spec:   
   containers:   
      - name: front-app     
      image: myrepo/mywebapp:v2    
   ports:    
     - containerPort: 80

my service yaml

apiVersion: v1
kind: Service
metadata:   
   name: front-service  
labels:     
   name: front-service     
   app: demo-geo-app
spec:  
   type: LoadBalancer   
ports:  
  - port: 80   
  targetPort: 80  
selector:     
   name: front-app-pod      
   app: demo-geo-app

回答1:

Change your yamls like this

apiVersion: v1
kind: Service
metadata:   
   name: front-service  
labels:     
   name: front-service     
   app: demo-geo-app
spec:  
   type: LoadBalancer   
   ports:  
   - port: 80   
     targetPort: 8080
   selector:     
     name: front-app-pod      
     app: demo-geo-app
apiVersion: v1
kind: Pod
metadata:   
   name: front-app-pod  
labels:      
   name: front-app-pod     
   app: demo-geo-app
spec:   
   containers:   
      - name: front-app     
      image: myrepo/mywebapp:v2    
   ports:    
     - containerPort: 8080

You expose the port 8080 in the docker image. Hence in the service you have to say that the targetPort: 8080 to redirect the traffic coming to load balancer on port 80 to the container's port 8080