<p>I am learning k8s. My question is that how to let k8s get service url as minikube command "minikube get service xxx --url" do?
Why I ask is because that when pod is down and up/created/initiated again, there is no need to change url by visiting service url. While
I deploy pod as NodePort, I could access pod with host IP and port, but if it is reinitiated/created again, the port changes. </p>

<p>My case is illustrated below: I have </p>

<pre><code>one master(172.16.100.91) and 
one node(hostname node3, 172.16.100.96) 
</code></pre>

<p>I create pod and service as below, helllocomm deployed as NodePort, and helloext deployed as ClusterIP. hellocomm and helloext are both 
spring boot hello world applications. </p>

<pre><code>docker build -t jshenmaster2/hellocomm:0.0.2 .
kubectl run hellocomm --image=jshenmaster2/hellocomm:0.0.2 --port=8080
kubectl expose deployment hellocomm --type NodePort

docker build -t jshenmaster2/helloext:0.0.1 .
kubectl run helloext --image=jshenmaster2/helloext:0.0.1 --port=8080
kubectl expose deployment helloext --type ClusterIP


[root@master2 shell]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
hellocomm    NodePort    10.108.175.143   &lt;none&gt;        8080:31666/TCP   8s        run=hellocomm
helloext     ClusterIP   10.102.5.44      &lt;none&gt;        8080/TCP         2m        run=helloext

[root@master2 hello]# kubectl get pods -o wide
NAME                         READY     STATUS    RESTARTS   AGE       IP              NODE
hellocomm-54584f59c5-7nxp4   1/1       Running   0          18m       192.168.136.2   node3
helloext-c455859cc-5zz4s     1/1       Running   0          21m       192.168.136.1   node3
</code></pre>

<p>In above, my pod is deployed at node3(172.16.100.96), so I could access hellocomm by 172.16.100.96:31666/hello, 
With this scenario, one could see easily that when node3 is down, a new pod is created/initiated, the port changes also. 
so that my client lost connection. I do not want this solution.</p>

<p>My current question is that as helloext is deployed as ClusteriP and it is also a service as shown above. does that mean ClusterIP
10.102.5.44 and port 8080 would be service url, <a href="http://10.102.5.44:8080/hello" rel="noreferrer">http://10.102.5.44:8080/hello</a>? </p>

<p>Do I need to create service by yaml file again? What is the difference from service created by command against by yaml file? How to write
following yaml file if I have to create service by yaml?</p>

<p>Below is yaml definition template I need to fill, How to fill?</p>

<pre><code>apiVersion: v1
kind: Service
matadata:
  name: string         helloext      
  namespace: string    default
  labels:              
    - name: string     helloext
  annotations:
    - name: string     hello world   
spec:
  selector: []            ?
  type: string            ?
  clusterIP: string       anything I could give?  
  sessionAffinity: string  ? (yes or no) 
  ports:
  - name: string          helloext  
    protocol: string      tcp  
    port: int             8081? (port used by host machine)
    targetPort: int       8080? (spring boot uses 8080)
    nodePort: int         ? 
  status:                 since I am not using loadBalancer in deploymennt, I could forget this.  
    loadBalancer:
      ingress:
        ip: string
        hostname: string
</code></pre>

<p>NodePort, as the name suggests, opens a port directly on the node (actually on all nodes in the cluster) so that you can access your service. By default it's random - that's why when a pod dies, it generates a new one for you. However, you can specify a port as well (3rd paragraph <a href="https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport" rel="nofollow noreferrer">here</a>) - and you will be able to access on the same port even after the pod has been re-created.</p>

<p>The clusterIP is only accessible inside the cluster, as it's a private IP. Meaning, in a default scenario you can access this service from another container / node inside the cluster. You can <code>exec</code> / <code>ssh</code> into any running container/node and try it out.</p>

<p>Yaml files can be version controlled, documented, templatized (<a href="https://www.helm.sh/" rel="nofollow noreferrer">Helm</a>), etc. </p>

<p>Check <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#servicespec-v1-core" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#servicespec-v1-core</a> for details on each field.</p>

<p><strong>EDIT</strong>:
More detailed info on services here: <a href="https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0" rel="nofollow noreferrer">https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0</a></p>
