简体   繁体   中英

Deploying Angular App on minikube and expose via port

I have issue with deploying angular app on minikube. I am not able to expose the running angular container on browser.

Below are my setup files.

Minikube start command

$ minikube start --driver=docker

Dockerfile

FROM node:10-alpine AS node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build-prod
FROM nginx:alpine
COPY --from=node /app/dist/shopping-wepapp /usr/share/nginx/html

Deployment configuration file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop-cart
spec:
  replicas: 2
  selector:
    matchLabels:
      app: shop-cart
  template:
    metadata:
      labels:
        app: shop-cart
        version: v1
    spec:
      containers:
        - name: shop-cart
          image:  kavin1995/development:shop-cart-app-07-04-2020-14-00
          imagePullPolicy: Always
          ports:
            - containerPort: 80

Service configuration file

apiVersion: v1
kind: Service
metadata:
  name: shop-cart-service
spec:
  selector:
    app: shop-cart
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 31000
  type: NodePort

Port exposing command

$ minikube service shop-cart-service --url 

As you use nodePort, you will be available to access the service from outisde the cluster.

To do it, first get the minikube ip from your command line:

minikube ip

It will return an ip XXXX After that, access your service from your browser: http://XXXX:31000

The problem seems to be the use of the docker driver: the minikube IP is a container IP and cannot be accessed outside of the docker host.

The only way to expose the nodePort outside would be to publish the port on the running minikube container (which only expose this: 127.0.0.1:32771->22/tcp, 127.0.0.1:32770->2376/tcp, 127.0.0.1:32769->5000/tcp, 127.0.0.1:32768->8443/tcp )

One way to do this would be (even if ugly):

CONTAINER_IP=`minikube ip`
SERVICE_NAME=shop-cart-service
SERVICE_NODE_PORT=`kubectl get service ${SERVICE_NAME} --output jsonpath='{.spec.ports[0].nodePort}'`

iptables -t nat -A DOCKER -p tcp --dport ${SERVICE_NODE_PORT} -j DNAT --to-destination ${CONTAINER_IP}:${SERVICE_NODE_PORT}
iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp --source ${CONTAINER_IP} --destination ${CONTAINER_IP} --dport ${SERVICE_NODE_PORT}
iptables -A DOCKER -j ACCEPT -p tcp --destination ${CONTAINER_IP} --dport ${SERVICE_NODE_PORT}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM