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

<p>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.</p>

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

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

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

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

<p>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, <code>service</code> is i think optional for my need, i am not sure about <code>deployment</code>, but i have seen an <code>endpoint</code> kind, and i could ignore other ones...</p>

<p>I am a little bit confuse since <code>kubectl run</code> 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.</p>

<p>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.</p>

<p>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 <code>kubectl run &lt;MY_APP&gt;</code>and <code>kubectl port-forward &lt;MY_POD&gt; 3030:80</code>into a manifest file so i can add the mongodb container and start the whole stack with a single <code>kubectl create -f ./local.yaml</code> command line.</p>

<p>Here is a first draft:</p>

<pre><code>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
</code></pre>

<p>Yes, you are right. You can expose your app as a service with <a href="https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport" rel="noreferrer">Type NodePort</a>  (though it's not very clear in documentation), and your service's yaml will look like:</p>

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

<p>Your deployment's yaml will look like:</p>

<pre><code>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
</code></pre>

<p>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. </p>

<p>You can deploy your app as a command:</p>

<pre><code>kubctl apply -f ./file_with_service.yaml ./file_with_deployment.yaml
</code></pre>

<p>And you can use all that as an example to begin, and read <a href="https://kubernetes.io/docs/concepts/" rel="noreferrer">more documentation</a> to understand it clearly. </p>
