Kubernetes,k8s how to make service url?

5/23/2018

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.

My case is illustrated below: I have

one master(172.16.100.91) and 
one node(hostname node3, 172.16.100.96) 

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.

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   <none>        8080:31666/TCP   8s        run=hellocomm
helloext     ClusterIP   10.102.5.44      <none>        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

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.

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, http://10.102.5.44:8080/hello?

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?

Below is yaml definition template I need to fill, How to fill?

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
-- user84592
kubernetes
kubernetes-ingress

2 Answers

5/23/2018

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 here) - and you will be able to access on the same port even after the pod has been re-created.

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 exec / ssh into any running container/node and try it out.

Yaml files can be version controlled, documented, templatized (Helm), etc.

Check https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#servicespec-v1-core for details on each field.

EDIT: More detailed info on services here: https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0

-- Amrit Bera
Source: StackOverflow

5/23/2018

What about creating a ingress and point it to the service to access it outside of the cluster?

-- Nathan Ogden
Source: StackOverflow