Working with Services in Kubernetes????
Nikita Parmar
Frontend Developer ???? | Competitive programmer | DevOps Learner ?? | Linux ???? | Git & Github ?? | GSsoc'23 contributor |
What are Services in Kubernetes?
In Kubernetes, a service is an abstraction layer that represents a logical set of pods and provides a stable IP address and DNS name for accessing them. Services enable decoupling of the application layer from the infrastructure layer, providing a consistent way to access pods regardless of their location or number.
Services act as a load balancer for a set of pods and provide a single entry point for accessing the pods. They also provide features such as load balancing, service discovery, and health checking.
Service Types in Kubernetes
Kubernetes supports four types of services, each with a different purpose:
How do Services work in Kubernetes?
When you create a service in Kubernetes, it creates a virtual IP address and DNS name for accessing the pods. The service then selects a set of pods based on labels and forwards traffic to them using a load balancing algorithm.
When a pod is added or removed from the set, the service automatically updates its list of endpoints. Services also provide health checking by periodically sending requests to the pods to ensure they are still running.
Task 1 : Create and Apply a Service Definition in Kubernetes
Create a Service definition for your todo-app Deployment in a YAML file. Apply the Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command. Verify that the Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
Step 1: Create a Service definition
To create a Service definition, you will need to define the following in a YAML file:
Here's an example of what your Service definition YAML file might look like:
apiVersion: v1
kind: Service
metadata:
name: todo-service
labels:
app: todo-app
spec:
type: NodePort
selector:
app: todo-app
ports:
- name: http
port: 80
targetPort: 8000
Step 2: Apply the Service definition
Once you have created your Service definition YAML file, you can apply it to your K8s (minikube) cluster using the kubectl apply -f service.yml -n <namespace-name> command. Make sure to replace <namespace-name> with the name of your Namespace.
COPY
COPY
kubectl apply -f service.yml -n todo-namespace
Step 3: Verify the Service is working
To verify that your Service is working, you can access the todo-app using the Service's IP and Port in your Namespace. You can find the IP and Port of your Service by running the following command:
COPY
COPY
minikube service todo-service -n todo-namespace --url
This will return the URL you can use to access the todo-app. Copy the URL and paste it into your web browser. If everything is working correctly, you should see the todo-app running in your web browser.Or else use belo command to access
领英推荐
That's it! You now know how to create and apply a Service definition in Kubernetes, and how to verify that your Service is working correctly. With this knowledge, you can take your Kubernetes deployment to the next level and build robust, scalable, and secure applications.
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. Apply the ClusterIP Service definition to your K8s (minikube) cluster using the kubectl apply -f cluster-ip-service.yml -n <namespace-name> command. Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
Here's an example of what the YAML file for a ClusterIP Service might look like:
COPY
COPY
apiVersion: v1
kind: Service
metadata:
name: todo-service-cluster-ip
labels:
app: todo-app
spec:
selector:
app: todo-app
ports:
- name: http
port: 80
targetPort: 8000
In this example, the metadata section specifies the name and labels of the Service, and the spec section specifies the selector and port configuration. The selector field is used to match the Service to the Pods that it should route traffic to, and the ports field specifies the port configuration for the Service.
To apply the Service definition to your K8s cluster, save the YAML file with a filename (e.g. cluster-ip-service.yml) and use the kubectl apply command:
kubectl apply -f cluster-ip-service.yml -n todo-namespace
To verify that the ClusterIP Service is working, you can create another Pod in the same namespace and try accessing the todo-app using the Service's name as the hostname. For example, if your ClusterIP Service is named todo-service-cluster-ip, you can try running the following command in another Pod
curl https://todo-service-cluster-ip
If the Service is working correctly, you should see the response from the todo-app.
Task 3 : Create a LoadBalancer Service for accessing the todo-app from outside the cluster
Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file. Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the kubectl apply -f load-balancer-service.yml -n <namespace-name> command.Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.
Create YAML file for a LoadBalancer Service that you can use to access your todo-app Deployment from outside the cluster:
apiVersion: v1
kind: Service
metadata:
name: todo-service-load-balancer
labels:
app: todo-app
spec:
type: LoadBalancer
selector:
app: todo-app
ports:
- name: http
port: 80
targetPort: 8000
In this example, the metadata section specifies the name and labels of the Service, and the spec section specifies the selector and port configuration. The type field is set to LoadBalancer, which creates a load balancer that can route external traffic to the Service. The ports field specifies the port configuration for the Service.
To apply the Service definition to your K8s cluster, save the YAML file with a filename (e.g. load-balancer-service.yml) and use the kubectl apply command
kubectl apply -f load-balancer-service.yml -n todo-namespace
Once the Service is created, you can verify that it's working by accessing the todo-app from outside the cluster using the load balancer's IP address and port. If you're using Minikube, you can get the load balancer's IP address by running the following command:
minikube service todo-service-load-balancer -n todo-namespace --url
This command will output the URL that you can use to access the todo-app from outside the cluster.
If you're not using Minikube, the process for accessing the todo-app from outside the cluster will depend on your specific environment and network setup. However, you can typically access the Service using the load balancer's IP address and port.
Conclusion
In conclusion, services are a critical component of Kubernetes that enable decoupling of the application layer from the infrastructure layer. They provide a consistent way to access pods and provide features such as load balancing, service discovery, and health checking. Kubernetes supports four types of services, each with a different purpose, and services work by creating a virtual IP address and DNS name for accessing the pods and forwarding traffic to them using a load balancing algorithm.
By understanding what services are in Kubernetes and how they work, you can effectively manage and scale your distributed applications on Kubernetes.