将kubernetes pod暴露给互联网

时间:2018-04-20 16:19:42

标签: kubernetes google-kubernetes-engine

我使用yml文件在kuberneters中创建了一个带有api和web docker容器的pod(见下文)。

listOfActionsToTake

它显示我的pod已启动并正在运行

apiVersion: v1
kind: Pod
metadata: 
  name: test
  labels:
    purpose: test
spec:
  containers:
  - name: api
    image: gcr.io/test-1/api:latest
    ports:
      - containerPort: 8085
        name: http
        protocol: TCP
  - name: web
    image: gcr.io/test-1/web:latest
    ports:
      - containerPort: 5000
        name: http
        protocol: TCP

但我不知道如何从这里公开它。

看起来很奇怪我必须再次运行kubectl run ....因为pod已经运行了。但它没有显示部署。

如果我尝试类似

的话
NAME       READY     STATUS    RESTARTS   AGE
test   2/2       Running   0          5m

它抱怨deployments.extensions“test”未找到。从这里部署最简洁的方法是什么?

2 个答案:

答案 0 :(得分:5)

要将部署公开到公共互联网,您需要使用服务。服务类型LoadBalancer处理得很好,因为你可以在yaml文件中使用pod选择器。

所以如果我的deployment.yaml看起来像这样:

kind: Deployment
apiVersion: apps/v1beta2
metadata:
  name: test-dply
spec:
  selector:
    # Defines the selector that can be matched by a service for this 
deployment
    matchLabels:
       app: test_pod
  template:
    metadata:
      labels:
        # Puts the label on the pod, this must match the matchLabels 
selector
        app: test_pod
    spec:
      # Our containers for training each model
      containers:
      - name: mycontainer
        image: myimage
        imagePullPolicy: Always
        command: ["/bin/bash"]
        ports:
        - name: containerport
          containerPort: 8085

然后链接到它的服务是:

kind: Service
apiVersion: v1
metadata:
  # Name of our service
  name: prodigy-service
spec:
  # LoadBalancer type to allow external access to multiple ports
  type: LoadBalancer
  selector:
    # Will deliver external traffic to the pod holding each of our containers
    app: test_pod
  ports:
    - name: sentiment
      protocol: TCP
      port: 80
      targetPort: containerport

您可以使用kubectl create -f /path/to/dply.yamlkubectl create -f /path/to/svc.yaml部署这两项。快速注意:该服务将分配一个公共IP地址,您可以使用kubectl get services找到以下输出:

NAME                    TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)                      AGE
carbon-relay            ClusterIP      *.*.*.*    <none>           2003/TCP                     78d
comparison-api          LoadBalancer   *.*.*.*    *.*.*.*     80:30920/TCP                 15d

分配ip可能需要几分钟,只是预警。但是LoadBalancer的ip是固定的,你可以删除它指向的pod并重新旋转它而不会产生任何后果。因此,如果我想编辑我的test.dply,我可以不用担心我的服务受到影响。你应该很少放弃服务

答案 1 :(得分:1)

您已创建了一个pod,而不是部署。 然后你暴露了一个部署(而不是你的pod)。 尝试:

kubectl揭露pod test --type = NodePort --port = 80 --target-port = 5000