> For the complete documentation index, see [llms.txt](https://asafahmadov.gitbook.io/cloud-and-devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://asafahmadov.gitbook.io/cloud-and-devops/devops-bootcamp/container-orchestration-with-kubernetes/services.md).

# Services

&#x20;   ▪ If a pod needs to communicate with another pod, it needs a way to know the IP address of the other pod. Kubernetes services provide a mechanism for locating other pods.

&#x20;   ▪ According to the Kubernetes networking model, pod IPs are ephemeral; if a pod crashes or is deleted and a new pod is created in its place, it most likely receives a new IP address. Kubernetes services allows you to select a mechanism for locating other pods.

&#x20;   ▪ Services select Pods based on their labels. When a network request is made to the service, it selects all Pods in the cluster matching the service's selector, chooses one of them, and forwards the network request to it.

### What does ClusterIP, NodePort, and LoadBalancer mean?

The `type` property in the Service's spec determines how the service is exposed to the network. It changes where a Service is able to be accessed from. The possible `type`s are ClusterIP, NodePort, and LoadBalancer

* `ClusterIP` – The default value. The service is only accessible from within the Kubernetes cluster – you can’t make requests to your Pods from outside the cluster!
* `NodePort` – This makes the service accessible on a static port on each Node in the cluster. This means that the service can handle requests that originate from outside the cluster.
* `LoadBalancer` – The service becomes accessible externally through a cloud provider's load balancer functionality. GCP, AWS, Azure, and OpenStack offer this functionality. The cloud provider will create a load balancer, which then automatically routes requests to your Kubernetes Service.

### Example YAML code that shows you how to use a NodePort service in Kubernetes.

```yaml
kind: Service 
apiVersion: v1 
metadata:
  name: hostname-service 
spec:
  # Expose the service on a static port on each node
  # so that we can access the service from outside the cluster 
  type: NodePort

  # When the node receives a request on the static port (30163)
  # "select pods with the label 'app' set to 'echo-hostname'"
  # and forward the request to one of them
  selector:
    app: echo-hostname 

  ports:
    # Three types of ports for a service
    # nodePort - a static port assigned on each the node
    # port - port exposed internally in the cluster
    # targetPort - the container port to send requests to
    - nodePort: 30163
      port: 8080 
      targetPort: 80
```

### ClusterIP Service & its subtypes

ClusterIP is an internal service, not accessible from outside the cluster

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FiCxfujNWtM29owPhLNVH%2Fimage.png?alt=media\&token=4180eca2-e35b-4960-b628-3d6cb05f2fcc)

### NodePort Service

&#x20;   ▪ Unlike internal Service, is accessible directly from outside cluster&#x20;

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2Fe0RxjwLsqQurzWiXXpEF%2Fimage.png?alt=media\&token=828bd6a1-4502-4b06-a23f-32f2a899f05c)

&#x20;   ▪ A ClusterIP Service, to which the NodePort Service routes, is automatically created

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FDVOuy7YUUKpVKeIKBPiU%2Fimage.png?alt=media\&token=96fb9fd0-80cc-47f2-9bec-02978e0d5992)

&#x20;   ▪ Exposes the Service on each Node's IP at a  port

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FlPggHUqQhAI34s6BXiuk%2Fimage.png?alt=media\&token=21e70637-d7fa-4cab-b6b4-0b153019a307)

### Loadbalancer Service

Exposes the Service externally using a cloud provider's load balancer&#x20;

NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FuqEMEwGFhIwN4KRWOJk3%2Fimage.png?alt=media\&token=37bcb98b-9775-48c1-8ec7-c4d077a453a2)
