我在minikube上部署angular应用程序时遇到问题。我无法在浏览器中显示正在运行的角度容器。
下面是我的设置文件。
$ minikube start --driver=docker
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
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
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
$ minikube service shop-cart-service --url
答案 0 :(得分:0)
在使用nodePort时,您将可以从整个集群中访问该服务。
要执行此操作,请首先从命令行获取minikube ip:
minikube ip
它将返回一个ipX.X.X.X。之后,从浏览器访问您的服务:http://X.X.X.X:31000
答案 1 :(得分:0)
问题似乎出在使用docker
驱动程序:minikube IP是容器IP,无法在docker主机之外访问。
向外部公开nodePort的唯一方法是在运行微型容器上发布端口(仅公开此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
)
一种做到这一点的方法是(即使很丑):
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}