'Kubernetes pod unable to download from npm registry EAI_AGAIN

I have a minikube setup where a container needs to access npm registry when starting up. But the pod can not connect to npm registry. I was under the impression that all outgoing connections are open from K8s.

Log from the pod

kubectl logs <pod_name>

npm ERR! code EAI_AGAIN
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/sequelize-cli failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-04-14T12_16_23_892Z-debug.log
Install for [ 'sequelize-cli@latest' ] failed with code 1

Dockerfile

FROM node:14.15.4-alpine
WORKDIR /app
COPY package.json .
RUN yarn install && yarn global add npx
COPY . .
EXPOSE 3000
ENTRYPOINT ["./entrypoint.sh"]
CMD [ "npm", "run", "start" ]

Entrypoint.sh

#!/bin/sh
set -e           # Stop on any error
npx sequelize-cli db:migrate:all  # Run migrations
npx sequelize-cli db:seed:all     # Preload initial data
exec "$@"        # Run the command as the main container process

Deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-auth-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: service-auth
  template:
    metadata:
      labels:
        component: service-auth
    spec:
      containers:
      - name: service-auth
        image: REMOVED:1.0.0
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 3000
        env:
          - name: SESSION_SECRET
            value: REMOVED
          - name: DB_USER
            value: auth
          - name: DB_PASS
            value: secret
          - name: DB_NAME
            value: users
          - name: DB_HOST
            value: mysql
      imagePullSecrets:
        - name: dockerhub-secret

Service-Cluster-Ip.yml

apiVersion: v1
kind: Service
metadata:
  name: service-auth-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: service-auth
  ports: 
    - port: 3000
      targetPort: 3000

If more information is needed, let me know. I am new to k8s.



Solution 1:[1]

reason: getaddrinfo EAI_AGAIN exactly indicates there is a network issue in your env. Since using K8s, I change dnsPolicy from ClusterFirst to Default

For example,

spec:
  selector:
    matchLabels:
      app: node-red
  template:
    metadata:
      labels:
        app: node-red
    spec:
      dnsPolicy: Default

Reference > https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 j3ffyang