# 10. K8 Services

### Service

* Each pod has its own IP address
  * Pods are ephemeral - destoryed frequently
* Service gives stable IP address
* Provides load balancing

***

### Types

#### 1. ClusterIP

* default type
* pod gets ip address from node's range

**How Service Knows which Pod to Forward Request to**

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2Faa9oBb2zLiqsDpbO1fFW%2Fimage.png?alt=media&#x26;token=a989d1b8-b301-494c-8195-58496f94a069" alt=""><figcaption></figcaption></figure>

**How Service Knows which Port to Forward Request to**

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FHpXlbZVSCvXR1XDzMpYB%2Fimage.png?alt=media&#x26;token=a5329b89-515c-483a-aaf1-e571a5b0d63e" alt=""><figcaption></figcaption></figure>

**Service Endpoints**

* K8 create endpoints object
* have same name as service
* keeps track of which pods are members/endpoint of the service

**Multiple Ports Service**

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FKwpNS6iLabiahlx0EoRT%2Fimage.png?alt=media&#x26;token=6cfd6897-b494-4a6f-81b4-dcfce151e40a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FJGH8B8uaAcjG8jphMmEe%2Fimage.png?alt=media&#x26;token=7ab44a64-3785-40a5-a537-753a4e2a108d" alt=""><figcaption></figcaption></figure>

#### 2. Headless Service

* clients want to communicate with 1 specific pod directly
* pod wants to talk directly with specific pod not a randomly selected pod
* Use Case: Stateful applications like databases
  * pod replicas are not identical
* Client needs to figure out IP address of each pod
  * Option 1 - API call to K8 API Server (X)
  * Option 2 - DNS Lookup (Recommended)

    * DNS lookup for service - returns single ip address
    * If ClusterIP is set to None - returns pod ip address instead
    *

    ```
    <figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FBSDvVZKMdCElC8J8YL9w%2Fimage.png?alt=media&#x26;token=1d39bac7-a412-43ef-97f7-d994fc2bcff2" alt=""><figcaption></figcaption></figure>
    ```

***

### Service Type Attributes

1. ClusterIP
   * Only accessible within cluster
2. NodePort

   * extension of clusterip service
   * External traffic has access to fixed port on each worker node
   *

   ```
   <figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FLmyb8jb4LLbYIBqKF27b%2Fimage.png?alt=media&#x26;token=c43b2c8e-555e-49d9-8d6c-beb4b490f381" alt=""><figcaption></figcaption></figure>
   ```

   *

   ```
   <figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2F6XBC6qUuhiJiG6F82sXz%2Fimage.png?alt=media&#x26;token=0bfe9320-49e3-4b13-9b16-e4e7fe0362bc" alt=""><figcaption></figcaption></figure>
   ```

   * not secure and efficient since port is directly accessible externally
   * not used in production environments
3. Load Balancer

   * extension of nodeport service
   * becomes accessible externally through cloud provider loadbalancers
   *

   ```
   <figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FUwhlOBpIQSjVSh3BS3Uf%2Fimage.png?alt=media&#x26;token=04eba77b-b53a-4274-ba1f-bd17d21860c6" alt=""><figcaption></figcaption></figure>
   ```

   * secure and efficient since port is not accessible directly but via load balancer
   * used in production environments

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.nomanaziz.me/devops/orchestration/kubernetes/10.-k8-services.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
