<p>I'm trying to connect two containers using localhost within the Kubernetes pod. But I'm not able to do so</p>
<h3>kubernetes.yml</h3>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      type: db
      service: db-service
  template:
    metadata:
      labels:
        type: db
        service: db-service
        db: mysql
    spec:
      containers:
      - name: db
        image: mysql:8.0
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: &quot;qwertyuiop&quot;
        - name: MYSQL_USER
          value: &quot;iiitbspring&quot;
        - name: MYSQL_PASSWORD
          value: &quot;iiitbspring&quot;
        - name: MYSQL_DATABASE
          value: &quot;foody&quot;
        ports:
        - containerPort: 3306


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: apiserver
spec:
  replicas: 1
  selector:
    matchLabels:
      type: server-microservice
      service: api-server-service
  template:
    metadata:
      labels:
        type: server-microservice
        service: api-server-service
        framework: spring-boot
        language: java
    spec:
      containers:
      - name: app
        image: #################
      - name: api-server
        image: #################
        env:
        - name: username
          value: &quot;iiitbspring&quot;
        - name: password
          value: &quot;iiitbspring&quot;
        - name: DB_URL
          value: &quot;jdbc:mysql://db-service:3306?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;serverTimezone=UTC&quot;
        # ports:
        # - containerPort: 80
        # - containerPort: 8686
        # livenessProbe:
        #   httpGet:
        #     path: /api/foods
        #     port: 8686
        # readinessProbe:
        #   httpGet:
        #     path: /actuator/health
        #     port: 8686
        #   periodSeconds: 1

---

apiVersion: v1
kind: Service
metadata:
  name: db-service
spec:
  ports:
  - port: 3306
  selector:
    type: db
    service: db-service

---


apiVersion: v1
kind: Service
metadata:
  name: api-server
spec:
  type: LoadBalancer
  ports:
  # - name: api
  #   port: 8686
  #   targetPort: 8686
  - name: http
    port: 3000
    targetPort: 80
  selector:
    type: server-microservice
    service: api-server-service
</code></pre>
<p>I can easily do the same using docker compose :</p>
<pre><code>version: &quot;3.3&quot;
services: 
  db:
    image: mysql:8.0
    restart: always
    networks:
      - database
    environment:
      - MYSQL_ROOT_PASSWORD=qwertyuiop
      - MYSQL_DATABASE=foody
      - MYSQL_USER=iiitbspring
      - MYSQL_PASSWORD=iiitbspring
    volumes:
      - db-data:/var/lib/mysql
    healthcheck:
      test: [&quot;CMD&quot;, &quot;mysqladmin&quot; ,&quot;ping&quot;, &quot;-h&quot;, &quot;localhost&quot;]
      timeout: 20s
      retries: 10

  server:
    build: ./backend/
    image: #################
    depends_on: 
      - db
    networks: 
      - database
      - server
    environment: 
      - username=iiitbspring
      - password=iiitbspring
      - DB_URL=jdbc:mysql://db:3306?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;serverTimezone=UTC
    expose:
      - &quot;8686&quot;
    ports:
      - &quot;8686:8686&quot;
    # healthcheck:
    #   test: [&quot;CMD-SHELL&quot;, &quot;curl -f http://localhost:4444/actuator/health || exit 1&quot;]
    #   timeout: 10s
    #   retries: 6

  app:
    build: ./foodyFrontEnd/
    image: #################
    networks: 
      - server
    ports:
      - &quot;3000:80&quot;
    depends_on: 
      - server

volumes:
  db-data:

networks:
  database:
    driver: bridge
  server:
</code></pre>
<p>A bit about both the containers that I want to run: A backend container which is running springboot application at port 8686, other is an frontend container running angular using nginx on port 80.</p>
<p>The error I'm getting :
on visiting the URL : http://remote-url:3000/ I'm having my angular page but on the console I can see the error : GET http://localhost:8686/api/foods net::ERR_CONNECTION_REFUSED, where my webapp was trying to establish the connection to backend.</p>
<p>I tried curl inside the container but I cannot run it due to alpine.</p>

vivek gupta
<p>As @David Maze said:</p>
<blockquote>
<p>The problem is that angular is running at client side, so when I was
sending request to localhost then this is trying to connect to port on
the desktop system.</p>
</blockquote>
<p>Thanks @David Maze I just missed this point.</p>
