rinormaloku / k8s-mastery

Repository for the article "Learn Kubernetes in Under 3 Hours"
https://medium.com/p/114ff420e882/
897 stars 839 forks source link

SA_LOGIC_API_URL #25

Open gokcedilek opened 4 years ago

gokcedilek commented 4 years ago

Hi! I'm following the tutorial on FreeCodeCamp, and I've been having trouble with setting up the connection between the sa-logic container and the sa-webapp container (likely a problem with the SA_LOGIC_API_URL environment variable). What happens right now is, after building and running the three containers without a problem, when I go to "localhost:80", I can only see the "Sentiment Analysis" textbox, and when I type a sentence and send, nothing happens. As it is described in the tutorial, I ran "docker container list", and then "docker inspect " with the id of the sa-logic container, and then used the id when running the sa-webapp container as follows: "docker run -d -p 8080:8080 -e SA_LOGIC_API_URL='http://172.17.0.2:5000' gokcedilek/sentiment-analysis-web-app". However, even though these three containers seem to be up and running, I cannot get a response back when I submit a sentence. Do you know what could be the reason for my problem? I would appreciate any suggestions very much! Thanks!

rinormaloku commented 4 years ago

Hi Gokcedilek,

Does the request get up to sa-webapp?

gokcedilek commented 4 years ago

Hi, everything works before running the applications from the containers. I ran sudo nginx, java -jar sentiment-analysis-web-0.0.1-SNAPSHOT.jar --sa.logic.api.url=http://localhost:5000, and python3 sentiment_analysis.py from the directories and I am able to get the polarity of sentences. It's just that when I kill those processes and try to run from the containers, it doesn't work. How can I check if the request gets up to sa-webapp? I guess I need to add something to SentimentController.java?

rinormaloku commented 4 years ago

Yeah, add some logs and see if requests come in. I think by default you get logs of incoming requests.

karanmitroo commented 4 years ago

Hi All,

As I was following this tutorial from freeCodeCamp, I got stuck with the same issue. Once all the apps got containerized, the frontend was not able to connect to the webapp. The request always timed out. While looking for a fix online I got to know about this issue with docker in MacOS Link Here.

I am not yet sure on how to fix. Looking for a fix to this issue to proceed further with the tutorial.

Thanks

karanmitroo commented 4 years ago

Hi,

I have tried it this way to make this work.

sa-logic, sa-webapp, sa-frontend are the name of the images I have built for each service.

docker run -d -p 5050:5000 --name sa-logic sa-logic
docker run -d -p 8080:8080 --link sa-logic -e SA_LOGIC_API_URL='http://sa-logic:5000' --name sa-webapp sa-webapp
docker run -d -p 80:80 --name sa-frontend sa-frontend

In App.js I am making the call to localhost as it is running the browser and not the container, so requests are going from my machine and they should go to the webapp container which have been exposed on port 5050 as per above commands

analyzeSentence() {
        fetch('http://localhost:8080/sentiment', {
            method: 'POST',
...

Let me know if this is actually a correct way of doing things. I was playing around with docker trying to make this work and could find this one way.

bokhanych commented 6 months ago

The problem still persists.

services:

  sa-frontend:
    build:
      dockerfile: ./sa-frontend/Dockerfile
    container_name: sa-frontend
    restart: unless-stopped
    tty: true
    volumes:
      - ./sa-frontend/nginx/conf.d/:/etc/nginx/conf.d/
    ports:
        - "80:80"
    networks:
      sa-network:
        ipv4_address: 172.31.0.100
    links:
      - sa-webapp

  sa-webapp:
    build:
      dockerfile: ./sa-webapp/Dockerfile
    container_name: sa-webapp
    restart: unless-stopped
    environment:
      SA_LOGIC_API_URL: http://sa-logic:5000
    tty: true
    ports:
        - "8080:8080"
    networks:
      sa-network:
        ipv4_address: 172.31.0.101
    links:
      - sa-logic

  sa-logic:
    build:
      dockerfile: ./sa-logic/Dockerfile
    container_name: sa-logic
    restart: unless-stopped
    tty: true
    ports:
        - "5000:5000"
    networks:
      sa-network:
        ipv4_address: 172.31.0.102

networks:
  sa-network:
    name: sa-network
    driver: bridge
    ipam:
      config:
        - subnet: 172.31.0.0/16
docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED       STATUS       PORTS                    NAMES
7dac8b995faf   k8s-mastery-sa-frontend   "/docker-entrypoint.…"   2 hours ago   Up 2 hours   0.0.0.0:80->80/tcp       sa-frontend
7147113f4e83   k8s-mastery-sa-webapp     "java -jar sentiment…"   2 hours ago   Up 2 hours   0.0.0.0:8080->8080/tcp   sa-webapp
d05d221a9771   k8s-mastery-sa-logic      "python3 sentiment_a…"   2 hours ago   Up 2 hours   0.0.0.0:5000->5000/tcp   sa-logic
cat /opt/k8s-mastery/sa-frontend/nginx/conf.d/nginx.conf
server {
    listen 80;

    location /sentiment {
        proxy_pass http://sa-webapp:8080;
    }

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

Browser:

App.js:23 
 POST http://sa-webapp:8080/sentiment net::ERR_NAME_NOT_RESOLVED

Please, help.