A Service gives changing Pods a stable virtual address. This lab creates one Deployment, proves selector-based EndpointSlices, and compares internal ClusterIP with NodePort and provider-backed LoadBalancer exposure.
1. Prepare and execute
Confirm the active kubeconfig context and namespace before applying changes. Save commands and manifests in version control without credentials.
kubectl create namespace service-lab
kubectl -n service-lab create deployment web --image=nginx:1.28-alpine --replicas=2
kubectl -n service-lab expose deployment web --name web-clusterip --port=80 --target-port=80
2. Declarative configuration
Save the following example in a clearly named YAML file, review it, then use server-side dry-run when the API is available.
apiVersion: v1
kind: Service
metadata: {name: web-nodeport, namespace: service-lab}
spec:
type: NodePort
selector: {app: web}
ports: [{port: 80, targetPort: 80}]
3. Verify the result
A successful command is not enough. Inspect resource state, conditions, events, endpoints, logs, and an end-to-end request where applicable.
kubectl apply -f nodeport.yaml
kubectl -n service-lab get service,endpointslices -o wide
kubectl -n service-lab run curl --rm -it --restart=Never --image=curlimages/curl:8.14.1 -- http://web-clusterip
NODE_PORT=$(kubectl -n service-lab get svc web-nodeport -o jsonpath='{.spec.ports[0].nodePort}')
echo "$NODE_PORT"
4. Troubleshooting
A Service with no endpoints almost always has a selector/label mismatch or no Ready Pods. A LoadBalancer that stays Pending means the environment has no load-balancer implementation or provider integration.
5. Rollback and cleanup
Review the exact target before deleting resources, especially namespaces, claims, Secrets, and cluster-wide add-ons.
kubectl delete namespace service-lab
Production checklist
- The active context, namespace, and target version were verified.
- Manifests passed client or server-side validation.
- Resource conditions and recent events show no unresolved error.
- Access, network exposure, resource limits, persistence, and rollback were reviewed.
- Commands and expected output were recorded for the operating team.
References: topic documentation and Kubernetes documentation.