DEV Community

Mesrar
Mesrar

Posted on

Navigating Services in Kubernetes

In Kubernetes, services play a crucial role in enabling communication between different components within the cluster or exposing applications to external users. Let's explore key commands and concepts related to managing services.

Key Operations
Create a Service
Apply a service definition from a YAML file:

kubectl apply -f <svc.yaml>

Enter fullscreen mode Exit fullscreen mode

View Services
List all services in the current namespace:

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

Describe a Service
Get detailed information about a specific service, including ports and selectors:

kubectl describe svc <service-name>

Enter fullscreen mode Exit fullscreen mode

Delete a Service
To delete a specific service:

kubectl delete svc <service-name>

Enter fullscreen mode Exit fullscreen mode

Expose a Pod with a Service
Expose a pod by creating a ClusterIP service:

kubectl expose pod redis --port=6379 --name=redis-service --type=ClusterIP --dry-run=client -o yaml > svc.yaml

Enter fullscreen mode Exit fullscreen mode

Or expose a deployment with a NodePort service:

kubectl expose deploy <deploy-name> --port=<service-port>

Enter fullscreen mode Exit fullscreen mode

Edit a Service
Edit the configuration of a service:

kubectl edit svc <service-name>

Enter fullscreen mode Exit fullscreen mode

Get Endpoints for a Service
View the endpoints associated with a service:

kubectl get ep <svc-name>

Enter fullscreen mode Exit fullscreen mode

Additional Insights
Service Types:

NodePort: Exposes the service on each node's IP at a static port. Accessible externally.
ClusterIP: Exposes the service inside the cluster using a cluster-internal IP.
LoadBalancer: Creates an external IP and routes traffic to the service.

Ports in Services:
NodePort: Port on the node.
Port: Port on the service (exposed internally).
TargetPort: Port on the pod (container).

Service Selector:
Services, deployments, replica sets, and network policies use the selector property under spec to select pods using labels.

Accessing Services:
Pods can access services using the ClusterIP or service name.
Ensure proper configuration of services based on your use case, whether for internal communication or external access.

Happy Networking in Kubernetes!

Top comments (0)