This lesson covers inspecting and creating services in a Kubernetes cluster to expose a web application.
In this lesson, we will walk through the solution for the lab on Services. You will learn how to inspect different aspects of services in a Kubernetes cluster and create a service to expose a web application. This guide is designed to help you understand Kubernetes Services, their types, endpoints, and best practices for deployment.
Next, inspect the default Kubernetes service to review its type and configuration. Running the following commands reveals that the service type is ClusterIP:
Copy
Ask AI
controlplane ~ ➜ kubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.43.0.1 <none> 443/TCP 10mcontrolplane ~ ➜ kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.43.0.1 <none> 443/TCP 10mcontrolplane ~ ➜
To inspect the target port configured on this service, use the kubectl describe command as shown below:
The Kubernetes service is labeled with key-value pairs that identify the API server:
component=apiserver
provider=kubernetes
These labels help in managing and identifying the components of the cluster. Endpoints, on the other hand, define the pods that receive traffic from the service. Even if multiple pods match the selector criteria, the service lists all endpoints.In our case, the Kubernetes service shows one endpoint:
Endpoints represent the set of pod IP addresses and ports that receive traffic from the service. If the service’s selector mistakenly does not match any pods, the endpoints list will be empty.
An illustrative diagram outlines how endpoints connect to pods:
If you attempt to access the web application UI without first exposing it through a service, you may encounter a “Bad Gateway” error. This occurs because no service directs traffic to your pods.
Copy
Ask AI
controlplane ~ -> kubectl get deployNAME READY UP-TO-DATE AVAILABLE AGEsimple-webapp-deployment 0/4 4 0 15scontrolplane ~ -> kubectl describe deploy simple-webapp-deploymentName: simple-webapp-deploymentNamespace: defaultCreationTimestamp: Fri, 15 Apr 2022 20:35:47 +0000Labels: <none>Annotations: deployment.kubernetes.io/revision: 1Selector: name=simple-webappReplicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailableStrategyType: RollingUpdateMinReadySeconds: 0RollingUpdateStrategy: 25% max unavailable, 25% max surgePod Template: Labels: name=simple-webapp Containers: simple-webapp: Image: kodekloud/simple-webapp:red Port: 8080/TCP Host Port: 0/TCP Environment: <none> Mounts: <none>Conditions: Type Status Reason ---- ------ ------- Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailableOldReplicaSet: <none>NewReplicaSet: simple-webapp-deployment-7b59598d59 (4/4 replicas created)Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 63s deployment-controller Scaled up replica set simple-webapp-deployment-
To resolve this, create a service that exposes the web application.
For this lab, create a NodePort service named webapp-service that exposes your web application on port 8080. The final service definition is as follows:
Save this configuration as your service definition file (for example, service-definition-1.yaml) and then run the following commands to create the service:
In the subsequent sections, we will discuss additional imperative commands for creating services. Below are some sample commands that you might find useful for quickly generating service definitions:
Copy
Ask AI
kubectl expose pod redis --port=6379 --name=redis-service --dry-run=client -o yaml
Copy
Ask AI
kubectl create service clustertip redis --tcp=6379:6379 --dry-run=client -o yaml
Copy
Ask AI
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml
Copy
Ask AI
kubectl create service nodeport nginx --tcp=80:30080
This concludes the lab for this lesson. In upcoming lessons, we will dive deeper into imperative commands and additional details on managing Kubernetes services effectively.For further reading, check out these resources: