> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Solution Services optional

> Walks through a hands on solution showing kubectl commands and explanations to inspect and expose a Deployment using Kubernetes Services, including creating a NodePort Service

In this lesson we walk through the solution for the lab on Kubernetes Services. Each step shows the commands and outputs you would run in the cluster, explains the result, and demonstrates how to expose a Deployment using a Service.

## 1) List Services in the current namespace

Run:

```bash theme={null}
kubectl get svc
```

Example output:

```bash theme={null}
controlplane ~ ➜ kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.43.0.1     <none>        443/TCP    10m
```

* There is only one Service in the default namespace: the built-in Kubernetes API server Service.
* The TYPE column shows this Service is `ClusterIP`.

## Quick answers

| Question | Answer |
| - | - |
| How many Services exist? | 1 (the default `kubernetes` Service) |
| Type of default Service | `ClusterIP` |
| TargetPort of default Service | `6443/TCP` |
| Labels on the Service | `component=apiserver`, `provider=kubernetes` |
| Number of endpoints attached | 1 (`10.53.180.9:6443`) |

## 2) Inspect the default Kubernetes Service (details)

Describe the Service to view port and endpoint details:

```bash theme={null}
kubectl describe svc kubernetes
```

Example describe output:

```bash theme={null}
controlplane ~ ➜ kubectl describe svc kubernetes
Name:                     kubernetes
Namespace:                default
Labels:                   component=apiserver
                          provider=kubernetes
Annotations:              <none>
Selector:                 <none>
Type:                     ClusterIP
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.43.0.1
IPs:                      10.43.0.1
Port:                     https   443/TCP
TargetPort:               6443/TCP
Endpoints:                10.53.180.9:6443
Session Affinity:         None
Events:                   <none>
```

* TargetPort is `6443/TCP`.
* Endpoints show `10.53.180.9:6443`, which is a single endpoint for this Service.
* Labels are listed under Labels in the describe output.

## What are Endpoints?

Endpoints are the network addresses (IP:port) of Pods that a Service routes to. Kubernetes automatically maintains Endpoints to reflect the pods matched by the Service's selector. If no Pods match, the Endpoints list is empty; if multiple Pods match, Endpoints contains multiple entries.

<Callout icon="lightbulb" color="#1CB2FE">
  Endpoints are not objects you normally edit. Kubernetes dynamically manages Endpoints to match the set of Pods selected by a Service's selector.
</Callout>

Here is a simple diagram showing a Service selecting multiple Pods by label:

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/NJcyyp7W6GrcG6D4/images/Certified-Kubernetes-Application-Developer-CKAD/Core-Concepts/Solution-Services-optional/blue-handdrawn-triangle-to-three-apps.jpg?fit=max&auto=format&n=NJcyyp7W6GrcG6D4&q=85&s=a122c1b40b2aae3b12d8b5366e2f5461" alt="A simple blue hand-drawn diagram showing one triangular node at the top branching with arrows down to three round nodes (like apples) below. Handwritten annotations on the right read something like &#x22;app: FE&#x22; and &#x22;app: FC.&#x22;" width="1920" height="1080" data-path="images/Certified-Kubernetes-Application-Developer-CKAD/Core-Concepts/Solution-Services-optional/blue-handdrawn-triangle-to-three-apps.jpg" />
</Frame>

In the `kubernetes` Service example above, only one endpoint is present.

## 3) How many Deployments are present?

List Deployments:

```bash theme={null}
kubectl get deploy
```

Example output:

```bash theme={null}
controlplane ~ ➜ kubectl get deploy
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
simple-webapp-deployment     0/4     4            0           15s
```

* There is one Deployment: `simple-webapp-deployment`.

## 4) Which image does the Deployment use?

Describe the Deployment:

```bash theme={null}
kubectl describe deploy simple-webapp-deployment
```

Example describe output:

```bash theme={null}
controlplane ~ ➜ kubectl describe deploy simple-webapp-deployment
Name:                   simple-webapp-deployment
Namespace:              default
CreationTimestamp:      Fri, 15 Apr 2022 20:35:47 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               name=simple-webapp
Replicas:               4 desired | 4 updated | 4 total | 0 available | 4 unavailable
StrategyType:           RollingUpdate
Pod Template:
  Labels:  name=simple-webapp
  Containers:
   simple-webapp:
    Image:      kodekloud/simple-webapp:redis
    Port:       8080/TCP
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  63s   deployment-controller  Scaled up replica set simple-webapp-deployment-7b59598d59 to 4
```

* Container image: `kodekloud/simple-webapp:redis`.
* Container port: `8080/TCP`.

## 5) Is the web UI accessible?

Attempting to access the app (for example via a NodePort on the node IP) produced a "Bad Gateway" error. The UI was not reachable because there was no Service routing external traffic to the Deployment's Pods yet. You must create a Service that selects the Deployment's Pods.

## 6) Create a Service to expose the web application

A NodePort Service manifest that exposes the Deployment's Pods on port 8080 looks like this:

```yaml theme={null}
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  ports:
  - targetPort: 8080
    port: 8080
    nodePort: 30080
  selector:
    name: simple-webapp
```

Save as `service-definition-1.yaml` and create it:

```bash theme={null}
kubectl create -f service-definition-1.yaml
```

Example output:

```bash theme={null}
controlplane ~ ➜ kubectl create -f service-definition-1.yaml
service/webapp-service created
```

After this Service is created, access the app using the node IP and NodePort, e.g.:

```text theme={null}
http://<node-ip>:30080
```

This should route traffic to the Pods on port 8080.

## 7) Tip: generate skeleton manifests with kubectl

You can use kubectl's imperative commands with `--dry-run=client -o yaml` to produce skeleton YAML you can edit before applying. Examples:

```bash theme={null}
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
kubectl expose pod redis --port=6379 --name=redis-service --dry-run=client -o yaml
```

These commands output YAML that you can modify and apply with:

```bash theme={null}
kubectl apply -f <file>
```

## References

* [Kubernetes Services concept](https://kubernetes.io/docs/concepts/services-networking/service/)
* [kubectl cheat sheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-kubernetes-application-developer-ckad/module/eae8cedf-d483-471f-8796-49f69baec6cf/lesson/a4070e88-eff8-4b94-9178-2a11441737ca" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.