expose kubernetes pod to internet

4/20/2018

I created a pod with an api and web docker container in kuberneters 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?

-- Mike
google-kubernetes-engine
kubernetes

2 Answers

4/20/2018

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

-- C.Nivs
Source: StackOverflow

4/20/2018

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

-- Mick Mahoney
Source: StackOverflow