This specification creates a new Service object named "my-service", which targets TCP port 9376 on any Pod with the app=MyApp label.
Kubernetes assigns this Service an IP address (sometimes called the 'cluster IP')
The controller for the Service selector continuously scans for Pods that match its selector, and then POSTs any updates to an Endpoint object also named "my-service"
DNS
You can (and almost always should) set up a DNS service for your Kubernetes cluster using an add-on. In microk8s, you can microk8s enable dns
A cluster-aware DNS server, such as CoreDNS, watches the Kubernetes API for new Services and creates a set of DNS records for each one. If DNS has been enabled throughout your cluster then all Pods should automatically be able to resolve Services by their DNS name.
DNS
For example, if you have a Service called my-service in a Kubernetes namespace my-ns, the control plane and the DNS Service acting together create a DNS record for my-service.my-ns. Pods in the my-ns namespace should be able to find it by simply doing a name lookup for my-service (my-service.my-ns would also work).
Service Types
Kubernetes ServiceTypes allow you to specify what kind of Service you want. The default is ClusterIP. ServiceTypes allow you to expose a service onto an external IP address.
ServiceType - ClusterIP
Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType.
ServiceType - NodePort
Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using <NodeIP>:<NodePort>. Superset of ClusterIP.
ServiceType - LoadBalancer
LoadBalancer: Exposes the Service externally using a cloud provider's load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.
ServiceType - ExternalName
Maps the Service to the contents of the externalName field (for example, to the hostname api.foo.bar.example). The mapping configures your cluster's DNS server to return a CNAME record with that external hostname value. No proxying of any kind is set up.
From any node in the cluster we should be able to curl those ip's and get a response
Summary of previous example
Remember:
a pod gets its' own IP address
the containers in the pod were running nginx (listening on port 80)
When you curl the ip, since that is the only thing listening on port 80, we get a response
Also note that nothing is listening on port 80 on the NODE. (I.e. do a curl localhost) Nothing should be listening, unless you are using a previous node that you have installed a webserver on.
Some examples
Remember that these pods can be created and destroyed. The deployment will create new ones with new IP's. How can we make sure that things get routed correctly to our pods? Services
Run kubectl expose deployment/my-nginx, then kubectl get svc.
Some examples
The 'expose' command that we ran previously, does the same as if you ran kubectl apply -f on the following file:
The spec will create a service that targets port 80 on ANY pod with the matching run: my-nginx label.
Look at kubctl describe svc my-nginx
Note the endpoints.
When a user now accesses the clusterip (the ip given in the service), port 8888, the service knows that it should forward traffic to pods of the given endpoint.
Some examples
Delete services with kubectl delete svc [name]
Some examples
To make applications accessible outside of the cluster use either: