> ## 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.

# Lab Solution Services optional

> Lab solution explaining Kubernetes Services, Endpoints, inspecting deployments and Pod images, and creating a NodePort Service to expose a web application with kubectl commands.

This lesson walks through the solution for the Services lab. You'll inspect existing Services, interpret a Service's configuration and Endpoints, confirm deployments and Pod images, and create a NodePort Service to expose an application.

## Inspect Services

List Services in the cluster:

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

Example output:

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

* Observation: There is only the default `kubernetes` Service.
* Type: `ClusterIP` — created by the cluster to expose the API server within the cluster network.

To view details for the Kubernetes Service:

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

Truncated relevant output:

```text theme={null}
Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP:                10.43.0.1
Port:              https    443/TCP
TargetPort:        6443/TCP
Endpoints:         10.53.180.9:6443
Session Affinity:  None
Events:            <none>
```

Question: What is the `targetPort` configured on the Kubernetes Service?\
Answer: `6443` (shown as `TargetPort: 6443/TCP`).

## Labels on the Kubernetes Service

From the `describe` output:

```text theme={null}
Labels: component=apiserver
        provider=kubernetes
```

There are two labels: `component=apiserver` and `provider=kubernetes`.

Note: This Service maps to the Kubernetes API server — the cluster control plane endpoint.

## Endpoints attached to the Kubernetes Service

From the `describe` output:

```text theme={null}
Endpoints: 10.53.180.9:6443
```

This shows a single endpoint (one IP:port). So: one endpoint.

### What are Endpoints?

Endpoints are the concrete Pod IPs and ports a Service routes to based on the Service selector. Quick flow:

* Create Pods with labels (e.g., `app=FE`).
* Create a Service with a `selector` that matches those labels.
* Kubernetes finds Pods that match the selector and lists their IP:port under the Service's Endpoints.
* If a Pod matches the selector accidentally, it will also become an endpoint — always check labels/selectors when troubleshooting.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/-XczbL3T0rqckZCZ/images/Certified-Kubernetes-Administrator-CKA/Core-Concepts/Lab-Solution-Services-optional/blue-handdrawn-triangle-arrows-app-fefc.jpg?fit=max&auto=format&n=-XczbL3T0rqckZCZ&q=85&s=9aca199a4f59e28c288584089a18a0bd" alt="A blue hand-drawn diagram showing a triangular node at the top with three arrows pointing down to three circular nodes. Handwritten labels on the right read &#x22;app: FE&#x22; and &#x22;app: FC.&#x22;" width="1920" height="1080" data-path="images/Certified-Kubernetes-Administrator-CKA/Core-Concepts/Lab-Solution-Services-optional/blue-handdrawn-triangle-arrows-app-fefc.jpg" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  Endpoints list the actual Pod IPs and ports that a Service routes to; they are derived from Pod labels and the Service selector.
</Callout>

## Deployments in the default namespace

List Deployments:

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

Example output:

```bash theme={null}
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
simple-webapp-deployment  4/4     4            4           15s
```

Answer: one deployment — `simple-webapp-deployment`.

To inspect which image the deployment uses, describe it:

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

Relevant excerpt:

```text theme={null}
Pod Template:
  Labels:  name=simple-webapp
  Containers:
    simple-webapp:
      Image:        kodekloud/simple-webapp:red
      Port:         8080/TCP
```

Answer: The Pods use the image `kodekloud/simple-webapp:red`.

## Accessing the web app UI

The UI returned a "Bad Gateway" error initially. Reason: the application Pods are running, but no Service exposes the app to the node network yet — so the UI is not reachable externally.

To expose the app to nodes, create a NodePort Service. Use the provided template and save it (example: `service-definition-1.yaml`):

```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
```

Create the Service:

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

Expected response:

```bash theme={null}
service/webapp-service created
```

After creation, access the web app at:

http\://\<node-ip>:30080

<Callout icon="warning" color="#FF6B6B">
  NodePort values must generally be within the default range `30000-32767` unless your cluster allows a different range. Choose `nodePort` carefully to avoid conflicts.
</Callout>

## Helpful kubectl imperative commands (generate YAML with dry-run)

Use these to generate manifests for inspection or editing:

```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 save and apply with `kubectl apply -f`.

## Quick reference

| Topic | Command / Example |
| - | - |
| List Services | `kubectl get svc` |
| Describe a Service | `kubectl describe svc kubernetes` |
| List Deployments | `kubectl get deploy` |
| Describe Deployment | `kubectl describe deploy simple-webapp-deployment` |
| Create Service from file | `kubectl create -f service-definition-1.yaml` |
| Generate Service YAML (dry-run) | `kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml` |

## Links and references

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

***

This completes the Services lab solution.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/c6d2ac7d-8192-4cff-aa54-e36d888c5bd9/lesson/ca46fc81-bc60-48ad-9590-5ae15d90954b" />
</CardGroup>


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