1

I have a simple application composed with Express.js as backend API and React.js as Frontend client.

I create a singles image container with frontend and backend Application repo: https://github.com/vitorvr/list-users-kubernetes

Dockerfile:

FROM node:13

WORKDIR /usr/app/listusers

COPY . .

RUN yarn
RUN yarn client-install
RUN yarn client-build

EXPOSE 8080

CMD ["node", "server.js"]

server.js

const express = require('express');
const cors = require('cors');
const path = require('path');

const app = express();
const ip = process.env.IP || '0.0.0.0';
const port = process.env.PORT || 8080;

app.use(express.json());
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));

app.get('/users', (req, res) => {
  res.json([
    { name: 'Jhon', id: 1 },
    { name: 'Ashe', id: 2 }
  ]);
});

app.listen(port, ip, () =>
  console.log(`Server is running at http://${ip}:${port}`)
);

React call:

const api = axios.create({
  baseURL: 'http://0.0.0.0:8080'
});

useEffect(() => {
  async function loadUsers() {
    const response = await api.get('/users');
    if (response.data) {
      setUsers(response.data);
    }
  }
  loadUsers();
}, []);

To deploy and run this image in minikube I use these follow commands:

kubectl run list-users-kubernetes --image=list-users-kubernetes:1.0 --image-pull-policy=Never
kubectl expose pod list-users-kubernetes --type=LoadBalancer --port=8080
minikube service list-users-kubernetes

The issue occurs when the front end try to access the localhost:

enter image description here

I don't know where I need to fix this, if I have to do some fix in React, or do some settings in Kubernetes or even this is the best practice to deploy small applications as a Container Image at Kubernetes.

Thanks in advance.

1 Answer 1

3

Your Kubernetes node, assuming it is running as a virtual machine on your local development machine, would have an IP address assigned to it. Similarly, when an IP address would be assigned to your pod where the "list-user-kubernetes" service is running. You can view the IP address by running the following command: kubectl get pod list-users-kubernetes, and to view more information add -o wide at the end of the command, eg. kubectl get pod list-users-kubernetes -o wide.

Alternatively, you can do port forwarding to your localhost using kubectl port-forward pod/POD_NAME POD_PORT:LOCAL_PORT. Example below:

kubectl port-forward pod/list-users-kubernetes 8080:8080 Note: You should run this as a background service or in a different tab in your terminal, as the port forwarding would be available as long as the command is running.

I would recommend using the second approach, as your external IP for the pod can change during deployments, but mapping it to localhost would allow you to run your app without making code changes.

Link to port forwarding documentation

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer. The port-forward! But just a question. I thought if the frontend calls for localhost, the will search for the API inside the POD as a localhost. If I want to deploy this same image in GCP for example, what I have to do to don't need run the port-forward? I don't know if this make sense, but let know so I try to explain more.
That’s a great question. So, you have your API running inside the pod as a docker container. The docker container exposes the port 8080 to the pod. Now anything else running in the pod can access it on localhost. Kubernetes gives you an IP address for the pod, and you can access your application on that IP address on port 8080, but only on your local machine. This is an example of a Node IP. Now when you run kubernetes on GKE (GCP), EKS(AWS), you would want an external IP assigned to it, a Cluster IP. On the note of best practices, you should be using services which manage the pods for you.
Just to clarify, If I run kubectl get pod list-users-kubernetes -o wide I get an IP Address, in this case is 172.17.0.6. I did a minikube ssh and execute curl http://172.17.0.6:8080/users thats returned the expected result. So to "fix" the problem I need a way to configure my frontend call for the Pod IP instead localhost. Am I right? Thanks!
Yes, that will work for local development. You would run into this issue again if/when you decide to deploy your application to a cloud cluster. I would recommend you to look into kubernetes services.
Ok! I will, but something still weird to me (Sorry if I'm being redundant), I have only one image container that contains my frontend and my backend. I do expose the port 8080 and my frontend is served by static content in my root path e.g: "172.23.2.3:8080". Inside in this same container I have more APIs that my frontend do the calls internally e.g: "172.23.2.3:8080/users".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.