如何获取运行Kubernet Services的VM的外部IP

时间:2019-12-12 12:09:22

标签: azure docker kubernetes ip virtual-machine

我已将Docker映像托管在Azure的VM中,并且正在尝试在VM外部访问服务。由于该服务未生成外部IP,因此无法正常工作。

构建Docker映像后,我已将yml文件应用于创建Deployment and Service。我的yml文件如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: planservice-deployment
  labels: 
    app: planservice-deploy
spec:
  selector:
    matchLabels:
      run: planservice-deploy
  replicas: 2
  template:
    metadata:
      labels:
        run: planservice-deploy
    spec:
      containers:
      - name: planservice-deploy
        image: planserviceimage
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8086
---
apiVersion: v1
kind: Service
metadata:
  name: planservice-service
  labels: 
    app: planservice-deploy
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8086
  selector:
    run: planservice-deploy
---

运行以下命令以查看正在运行的服务后:

kubectl get pods --output=wide

此命令返回所有正在运行的服务及其外部IP信息。但是,当我看到列表时,所有服务都是使用空白外部IP生成的。

如何为所有服务设置外部IP,以便我可以在VM外部访问我的Web服务?

1 个答案:

答案 0 :(得分:2)

您需要将type更改为LoadBalancer

apiVersion: v1
kind: Service
metadata:
  name: planservice-service
  labels: 
    app: planservice-deploy
spec:
  type: LoadBalancer
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8086
  selector:
    run: planservice-deploy

https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer