将kubernetes pod暴露给互联网

Mik*_*ike -1 kubernetes google-kubernetes-engine

我使用yml文件在kuberneters中创建了一个带有api和web docker容器的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
Run Code Online (Sandbox Code Playgroud)

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

NAME       READY     STATUS    RESTARTS   AGE
test   2/2       Running   0          5m
Run Code Online (Sandbox Code Playgroud)

但我不知道如何从这里揭露它.

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

如果我尝试类似的东西

kubectl expose deployment test --type="NodePort"--port 80 --target-port 5000
Run Code Online (Sandbox Code Playgroud)

它抱怨deployments.extensions"test"未找到.从这里部署的最干净的方法是什么?

C.N*_*ivs 5

要将部署公开到公共Internet,您将需要使用服务.服务类型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
Run Code Online (Sandbox Code Playgroud)

然后链接到它的服务是:

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
Run Code Online (Sandbox Code Playgroud)

您可以使用kubectl create -f /path/to/dply.yaml和部署这两个项目kubectl 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
Run Code Online (Sandbox Code Playgroud)

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