<pre><code>docker run -it -p 80:80 con-1

docker run -it -p hostport:containerport
</code></pre>
<p>Lets say i have this yaml definition, does below where it says 80 <code>ports -&gt;  containerPort: 80</code> sufficent? In other words how do i account for -p 80:80 the hostport and container port in kubernetes yaml definition?</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: &quot;http-server&quot;
      volumeMounts:
        - mountPath: &quot;/usr/share/nginx/html&quot;
          name: task-pv-storage
</code></pre>

dedpo
<p>Exposing ports of applications with k8s is different than exposing it with docker.</p>
<p>For pods, <code>spec.containers.ports</code> field isn't used to expose ports. It mostly used for documenting purpouses and also to name ports so that you can reference them later in service object's target-port with their name, and not a number (<a href="https://stackoverflow.com/a/65270688/12201084">https://stackoverflow.com/a/65270688/12201084</a>).</p>
<p>So how do we expose pods to the outside?</p>
<p>It's done with <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">service objects</a>. There are 4 types of service: ClusterIP, NodePort, LoadBalancer and ExternalName.</p>
<p>They are all well explained in k8s documentation so I am not going to explain it here. Check out <a href="https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types" rel="nofollow noreferrer">K8s docs on types of servies</a></p>
<p>Assuming you know what type you want to use you can now use kubectl to create this service:</p>
<pre><code>kubectl expose pod &lt;pod-name&gt; --port &lt;port&gt; --target-port &lt;targetport&gt; --type &lt;type&gt;

kubectl expose deployment &lt;pod-name&gt; --port &lt;port&gt; --target-port &lt;targetport&gt; --type &lt;type&gt;
</code></pre>
<p>Where:</p>
<ul>
<li>--port - is used to pecify the port on whihc you want to expose the application</li>
<li>--target-port - is used to specify the port on which the applciation is running</li>
<li>--type - is used to specify the type of service</li>
</ul>
<p>With docker you would use <code>-p &lt;port&gt;:&lt;target-port&gt;</code></p>
<p>OK, but maybe you don't want to use kubeclt to create a service and you would like to keep the service in git or whatever as a yaml file. You can check out <a href="https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service" rel="nofollow noreferrer">the examples in k8s docs</a>, copy it and write you own yaml or do the following:</p>
<pre><code>$ kubectl expose pod my-svc --port 80 --dry-run=client -oyaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: my-svc
  name: my-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: my-svc
status:
  loadBalancer: {}
</code></pre>
<p>Note: notice that if you don't pass a value for <code>--target-port</code> it defaults to the same value as <code>--port</code>
Also notice the selector filed, that has the same values as the labels on a pod. It will forward the traffic to every pod with this label (within the namespace).</p>
<p>Now, if you don't pass the value for <code>--type</code>, it defaults to ClisterIP so it means the service will be accessible only from within the cluster.
If you want to access the pod/application from the outside, you need to use either NodePort or LoadBalancer.</p>
<p>Nodeport opens some random port on every node and connecting to this port will forward the packets to the pod. The problem is that you can't just pick any port to open, and often you dont even get to pick the port at all (it's randomly assigned).</p>
<p>In case of type LoadBalancer you can use whateever port you'd like, but you need to run in cloud and use cloud provisioner to create and configure external loadbalancer for you and point it to your pod. If you are running on bare-metal you can use projects like <a href="https://metallb.universe.tf/" rel="nofollow noreferrer">MetalLB</a> to make use of LoadBalancer type.</p>
<hr />
<p>To summarize, exposing containers with docker is totally different than exposing them with kubernetes. Don't assume k8s will work the same way the docker works just with different notation, because it won't.</p>
<p>Read the docs and blogs about k8s services and learn how they work.</p>
