PermalinkWhat are Services in K8s
In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.
PermalinkTask-1:
Create a Service for your todo-app Deployment from Day-32
Create a Service definition for your todo-app Deployment in a YAML file.
The YAML manifest defines a Kubernetes Service of type NodePort named "todo-service" for exposing your application.
spec
: Defines the specification for the Service.
selector
: Specifies the set of Pods that this Service will route traffic to. It matches Pods with the label "app: todo," which is typically the label used in your Deployment.ports
: Defines the ports that the Service should listen on and forward traffic to.protocol
: The network protocol to use (TCP in this case).port
: The port on which the Service should listen within the cluster (usually 80 for HTTP).targetPort
: The port to which traffic should be forwarded in the Pods (8000, which is the container port in your Deployment).
type
: Specifies the type of Service. In this example, it's "NodePort," which exposes the Service on a high-numbered port on each node in the cluster. Clients can access the Service using any node's IP address and the assigned NodePort.Apply the Service definition to your K8s (minikube) cluster using the
kubectl apply -f service.yml -n <namespace-name>
command.
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 Service is working by accessing the todo-app using the Service's IP and Port in your Namespace.
minikube service <service_name> -n=<namespace> --url
Curl request to the Service using its IP address:
- Access the todo-app using the Service’s IP and Port:
curl -L <service-ip>:<service-port>
curl -L http://192.168.49.2:30080
If the NodePort Service is working correctly, you should see the response from the todo-app.
PermalinkTask-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.
Note the name of another Pod in the same Namespace.
Exec into the Pod: go inside container
PermalinkTask-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.
In this example, the Service is named todo-lb-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 (minikube) cluster using the kubectl apply -f 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.
Get the LoadBalancer service:
kubectl get services -n <namespace>
The Minikube service command is used to interact with services in a Minikube cluster. The --url option will return the URL that you can use to access the Service in your browser.
Note the lb ip and port
Happy Learning!