Table of contents
What are Services in K8s
In Kubernetes, a Service is a method for exposing a network application that is running as one or more
In Kubernetes (K8s), a service is an abstract way to expose an application running on a set of pods as a network service. It provides a stable network endpoint that other applications or services can access, regardless of the individual pod instances or their underlying IP addresses. Services enable loose coupling between applications and decouple the way a service is implemented from how it is consumed.
Here are some key points about services in Kubernetes:
Stable network endpoint: A service is assigned a stable IP address (known as a cluster IP) that remains consistent even if the underlying pods or their IP addresses change.
Load balancing: A service distributes network traffic evenly across all the pods associated with it, ensuring efficient load balancing and high availability. This allows applications to scale horizontally by adding or removing pod instances without affecting the accessibility of the service.
Service discovery: Services provide a way to discover and connect to other services or applications running within the Kubernetes cluster. Clients can use the service name and port to access the service, and Kubernetes handles the routing to the appropriate pods.
Multiple service types: Kubernetes supports different types of services to cater to various use cases. Some common service types include ClusterIP (default), NodePort, LoadBalancer, and ExternalName.
ClusterIP: Exposes the service internally within the cluster, making it accessible only from within the cluster.
NodePort: Exposes the service on a specific port of each cluster node, making it accessible externally by using the node's IP address.
LoadBalancer: Integrates with the cloud provider's load balancer to expose the service externally with a public IP address. It distributes traffic across the nodes running the service.
ExternalName: Maps the service to an external DNS name without proxying the traffic. It enables services in the cluster to access external services by name.
Labels and selectors: Services use labels and selectors to determine which pods they should route traffic to. By matching labels, services can dynamically adjust the set of pods they target, allowing for easy scalability and deployment updates.
Task-1:
Create a Service for your todo-app Deployment from Day-32
Day-32 Deployment file
#todo-app Deployment from Day-32 apiVersion: apps/v1 kind: Deployment metadata: name: todo-app labels: app: todo namespace: todo-app spec: replicas: 2 selector: matchLabels: app: todo template: metadata: labels: app: todo spec: containers: - name: todo image: dwarika9167/todoapp:app1 ports: - containerPort: 8001
Apply the deployment file to run the pod.
kubectl apply -f deployment.yaml kubectl get pods -n=todo-app
Create a Service definition for your todo-app Deployment in a YAML file.
To create services for the above app deployment, you can define a Kubernetes Service manifest file.
apiVersion: v1 kind: Service metadata: name: todo-app-service namespace: todo-app spec: type: NodePort selector: app: todo ports: - port: 80 targetPort: 8001 # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767) nodePort: 30007
In this example, the Service type is set to NodePort, and a nodePort is defined as 30007. This will make the Service accessible from outside the cluster by using the IP address of any node in the cluster and the nodePort defined in the Service.
Apply the Service definition to your K8s cluster using the
kubectl apply -f service.yml
command.kubectl apply -f service.yaml
Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
To access the
todo-app
using the service's IP and port within your namespace, you can follow these steps:Check the IP address of the service by running the following
command:kubectl get service -n todo-app
Look for the
todo-service
service in the output and note the value under theCLUSTER-IP
column.Once you have the service's IP address, you can access the
todo-app
using the service's IP and port.the service's IP is
10.106.71.188
and the port is30007
, you can access it usinghttp://<service-ip>:<port>
in your web browser or by using tools likecurl
orwget
.Cluster Ip is Private Ip so we can access inside the worker Node
To access an Application outside Of the worker Node we have to use public IP with service Node-port In my case it is http://13.232.3.58:30007/
Task-2:
Create a ClusterIP Service for accessing the todo-app from within the cluster
Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.
apiVersion: v1 kind: Service metadata: name: todo-app-service labels: app: todo namespace: todo-app spec: type: ClusterIP selector: app: todo ports: - protocol: TCP port: 80 targetPort: 8001
In the above example, the Service is named todo-cluster and is of type ClusterIP. The selector app: todo is used to determine which Pods to associate with the Service. The Service has a single port, 80, which is mapped to the target port 8001 on the Pods.
Apply the ClusterIP Service definition to your K8s (kuebadm) cluster using
kubectl apply -f service.yml -n <namespace-name> # -n use when namspace in not mention in service file
The kubectl get svc command is used to list Services in a Kubernetes cluster.
The -n option is used to specify the namespace where the Service is located.
Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
Task-3:
Create a LoadBalancer Service for accessing the Django-todo from outside the cluster
Create a LoadBalancer Service definition for your Django-todo Deployment in a YAML file.
apiVersion: v1 kind: Service metadata: name: todo-app-service namespace: todo-app spec: type: LoadBalancer selector: app: todo ports: - protocol: TCP port: 80 targetPort: 8001
In this example, the Service is named todo-app-service and is of type LoadBalancer. The selector app: todo is used to determine which Pods to associate with the Service.
Apply the LoadBalancer Service definition to your K8s (kubeadm) cluster.
Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.
Check if the LoadBalancer Service has been provisioned and obtain the external IP. You can use the following command to get information about the Service:
kubectl get svc -n todo-app
If it shows in the pending stage use Ec2 Instance PublicIp Open Provided Port in my case 30437 in Security Group.
Open a web browser or use a tool
curl or wget
to access the application
Note: - This type of service is typically heavily dependent on the cloud provider.
Thank you for reading!! I hope you find this article helpful......