How to use kubectl port-forward from a manifest file (and generally a manifest file vs kubectl run)

4/26/2018

I am trying to run my first app with Kubernetes locally (or i should say minikube).

I have a pretty basic web server (one local docker image), and the official mongodb (that i would like to pull ideally from dockerhub) image.

I am not trying to deploy a mongodb cluster, just the minimum stuff to get my app running locally would be a great start!

First, i succeed to run my web server alone with kubectl run <MY_APP> --image=<MY_IMAGE> --port 3030 --image-pull-policy=IfNotPresent, then kubectl port-forward <MY_POD> 3030:80 and it works fine, i can hit the app from the 3030 port (the app is listening and the container expose the port 80).

But i would like to translate that into a manifest file to describe all the containers i need to run easily.

My first issue is i can't find how kubectl port-forward is supposed to be translated into a manifest file. I was thinking about targetPortbut i have a validation error when trying that, it looks like we can't use targetPort in pods containers ports description.

My second issue is that i am not really sure about what i am doing by trying to run that stack by describing a pod only. It may need other pieces, service is i think optional for my need, i am not sure about deployment, but i have seen an endpoint kind, and i could ignore other ones...

I am a little bit confuse since kubectl run seems to create a pod, a deployment, and a replica sets, i am not sure if i have to create all of that from my manifest file.

I just want to run my both containers locally to work on the code and refresh it everytime i make a change, and test it.

So my question have some sub questions due to my lack of knowledges about Kubernetes, but basically, i would like to know how to translate my kubectl run <MY_APP>and kubectl port-forward <MY_POD> 3030:80into a manifest file so i can add the mongodb container and start the whole stack with a single kubectl create -f ./local.yaml command line.

Here is a first draft:

apiVersion: v1
kind: Pod
metadata:
  name: my_app
spec:
  containers:
    - name: web-server
      image: my_app
      imagePullPolicy: IfNotPresent
      ports:
        - name: my_app
          containerPort: 3030
          targetPort: 80
          protocol: TCP
    - name: mongodb
      image: mongodb
      ports:
        - name: mongodb
          containerPort: 27017
          protocol: TCP
-- Ludo
kubernetes

1 Answer

4/27/2018

Yes, you are right. You can expose your app as a service with Type NodePort (though it's not very clear in documentation), and your service's yaml will look like:

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  type: NodePort
  ports:
    - port: 3030
      targetPort: 80
  selector:
    app: app-server

Your deployment's yaml will look like:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-server
  labels:
    app: app-server
spec:
  selector:
    matchLabels:
      app: app-server
  template:
    metadata:
      labels:
        app: app-server
    spec:
      containers:
      - name: web-server
        image: my_app
        ports:
        - containerPort: 80
      - name: mongodb
        image: mongodb
        ports:
        - containerPort: 27017

As you see, I exposed only your web server. Now, to get access to Mongo from outside the Kubernetes, you need to expose it too.

You can deploy your app as a command:

kubctl apply -f ./file_with_service.yaml ./file_with_deployment.yaml

And you can use all that as an example to begin, and read more documentation to understand it clearly.

-- Nick Rak
Source: StackOverflow