简体   繁体   中英

expose kubernetes pod to internet

I created a pod with an api and web docker container in kube.neters using a yml file (see below).

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

It show my pod is up and running

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

but I don't know how to expose it from here.

it seems odd I would have to run kubectl run.... again as the pod is already running. It does not show a deployment though.

if I try something like

kubectl expose deployment test --type="NodePort"--port 80 --target-port 5000

it complains about deployments.extensions "test' not found. What is the cleanest way to deploy from here?

To expose a deployment to the public internet, you will want to use a Service. The service type LoadBalancer handles this nicely, as you can just use pod selectors in the yaml file.

So if my deployment.yaml looks like this:

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

Then the service that would link to it is:

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

You can deploy these two items by using kubectl create -f /path/to/dply.yaml and kubectl create -f /path/to/svc.yaml . Quick note: The service will allocate a public IP address, which you can find using kubectl get services with the following output:

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

It can take several minutes to allocate the ip, just a forewarning. But the LoadBalancer's ip is fixed, and you can delete the pod that it points to and re-spin it without consequence. So if I want to edit my test.dply, I can without worrying about my service being impacted. You should rarely have to spin down services

You have created a pod, and not a deployment. Then you have exposed a deployment (and not your pod). Try:

kubectl expose pod test --type=NodePort --port=80 --target-port=5000

kubectl expose pod test --type=LoadBalancer --port=XX --target-port=XXXX

If you already have pod and service running, you can create an ingress for the service you want to expose to the inte.net.

If you want to create it through console, Google Cloud provides really easy way to create an ingress from an existing service. Go to Services and Ingress tab, select the service, click on create ingress, fill the name and other mandatory fields.

or you can create using yaml file

apiVersion: "networking.k8s.io/v1"
kind: "Ingress"
metadata:
  name: "example-ingress"
  namespace: "default"
spec:
  defaultBackend:
    service:
      name: "example-service"
      port:
        number: 8123
status:
  loadBalancer: {}

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