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

# services

Certainly! In Kubernetes, services play a crucial role in enabling communication between different components within a cluster. Here are some common usages of services in Kubernetes along with examples:

#### 1. **Cluster IP Service:**

* **Usage:**
  * Exposing a set of pods to other pods within the cluster.
* **Example:**

  ```yaml
  apiVersion: v1
  kind: Service
  metadata:
    name: my-cluster-ip-service
  spec:
    selector:
      app: my-app
    ports:
      - protocol: TCP
        port: 80
        targetPort: 8080
  ```

#### 2. **NodePort Service:**

* **Usage:**
  * Exposing a set of pods to the outside world (on a specific port) through each node.
* **Example:**

  ```yaml
  apiVersion: v1
  kind: Service
  metadata:
    name: my-nodeport-service
  spec:
    selector:
      app: my-app
    ports:
      - protocol: TCP
        port: 80
        targetPort: 8080
    type: NodePort
  ```

#### 3. **LoadBalancer Service:**

* **Usage:**
  * Exposing a set of pods to the outside world through a cloud provider's load balancer.
* **Example:**

  ```yaml
  apiVersion: v1
  kind: Service
  metadata:
    name: my-loadbalancer-service
  spec:
    selector:
      app: my-app
    ports:
      - protocol: TCP
        port: 80
        targetPort: 8080
    type: LoadBalancer
  ```

#### 4. **ExternalName Service:**

* **Usage:**
  * Providing external DNS names for services.
* **Example:**

  ```yaml
  apiVersion: v1
  kind: Service
  metadata:
    name: my-external-service
  spec:
    type: ExternalName
    externalName: my.external.service.com
  ```

#### 5. **Headless Service:**

* **Usage:**
  * Disabling the creation of a cluster IP, useful for StatefulSets.
* **Example:**

  ```yaml
  apiVersion: v1
  kind: Service
  metadata:
    name: my-headless-service
  spec:
    clusterIP: None
    selector:
      app: my-app
    ports:
      - protocol: TCP
        port: 80
        targetPort: 8080
  ```

#### 6. **Ingress Resource:**

* **Usage:**
  * Exposing HTTP and HTTPS routes to services.
* **Example:**

  ```yaml
  apiVersion: networking.k8s.io/v1
  kind: Ingress
  metadata:
    name: my-ingress
  spec:
    rules:
      - host: myapp.example.com
        http:
          paths:
            - path: /
              pathType: Prefix
              backend:
                service:
                  name: my-app-service
                  port:
                    number: 80
  ```

#### 7. **Endpoint Slices (for large scale):**

* **Usage:**
  * Efficiently managing endpoints for services at scale.
* **Example:**

  ```yaml
  apiVersion: discovery.k8s.io/v1
  kind: EndpointSlice
  metadata:
    name: my-endpoint-slice
  addressType: IPv4
  ports:
    - name: my-port
      protocol: TCP
      port: 80
  endpoints:
    - addresses:
        - 192.168.1.1
      conditions:
        ready: true
      hostname: pod-1
  ```

#### 8. **Service Discovery:**

* **Usage:**
  * Discovering services dynamically within the cluster.
* **Example:**
  * Utilizing DNS names like `my-app-service.namespace.svc.cluster.local`.

#### 9. **External DNS Integration:**

* **Usage:**
  * Automatically updating external DNS records based on services.
* **Example:**
  * Integrating with tools like ExternalDNS to manage DNS records.

#### 10. **Service Mesh Integration:**

```
- **Usage:**
  - Enabling advanced communication and observability between services.
- **Example:**
  - Integrating with Istio, Linkerd, or other service mesh solutions.
```

These examples showcase various ways services are used in Kubernetes to facilitate communication and expose applications both within and outside the cluster. Each type of service caters to specific requirements and use cases.
