Commands Reference — Kubernetes Services

2 min

Project projet11-kubernetes-services · all commands to deploy, expose, and test Services.

Table of contents


1. Build the image and deploy

From the project folder (projet11-kubernetes-services):

powershell
kubectl config use-context docker-desktop
docker build -t demo-k8s:1.0 ./app          # (if not already built in project 10)
kubectl apply -f k8s/                        # deployment + 3 services
kubectl get pods                             # 3 Pods Running

2. Observe the Services

powershell
kubectl get svc                              # TYPE, CLUSTER-IP, EXTERNAL-IP, PORT(S)
kubectl get svc -o wide
kubectl describe svc demo-clusterip          # details + Endpoints

How to read kubectl get svc:

ColumnMeaning
TYPEClusterIP / NodePort / LoadBalancer
CLUSTER-IPInternal IP (always present)
EXTERNAL-IPExternal IP (LoadBalancer) or <none>
PORT(S)Service port (and :nodePort for NodePort)

3. ClusterIP: service discovery (DNS)

A ClusterIP is not reachable from the host: you call it from a Pod, by its name.

powershell
# Disposable client Pod (--rm: deletes on exit)
kubectl run client --rm -it --image=curlimages/curl -- sh

Then, in the Pod shell:

sh
curl http://demo-clusterip           # call BY NAME (resolved by CoreDNS)
curl http://demo-clusterip           # run again: the Pod name changes
exit

Test DNS resolution:

powershell
kubectl run dnstest --rm -it --image=busybox:1.36 -- sh
# in the Pod:
nslookup demo-clusterip
wget -qO- http://demo-clusterip

4. NodePort: external access

powershell
kubectl get svc demo-nodeport
# Open in the browser:
start http://localhost:30082
# or test from the command line:
curl http://localhost:30082

5. LoadBalancer: external IP

powershell
kubectl get svc demo-lb                      # EXTERNAL-IP must become "localhost"
start http://localhost:8090
curl http://localhost:8090

If EXTERNAL-IP stays <pending>, wait a few seconds (Docker Desktop provisions the LB). On a kind cluster, the LoadBalancer type stays <pending> without MetalLB: use kubectl port-forward (see appendix).


6. Endpoints and selectors

powershell
kubectl get endpoints demo-clusterip         # IPs of the Pods behind the Service
kubectl get pods -o wide --show-labels       # Pod labels
kubectl get pods --selector app=demo-back    # same Pods as the Service selector

See the distribution (10 requests via port-forward):

powershell
kubectl port-forward svc/demo-clusterip 8091:80
# in ANOTHER terminal:
1..10 | ForEach-Object { (Invoke-WebRequest -Headers @{Accept="application/json"} http://localhost:8091).Content }

7. Cleanup

powershell
kubectl delete -f k8s/
# or by object:
kubectl delete svc demo-clusterip demo-nodeport demo-lb
kubectl delete deployment demo-back

Appendix — troubleshooting

SymptomLikely causeSolution
curl http://demo-clusterip failsNot from a Pod, or missing serviceCall from a Pod; check kubectl get svc
Empty EndpointsThe selector matches no Pod labelAlign selector (Service) and labels (Pods)
EXTERNAL-IP in <pending>No load balancer (kind, unconfigured cloud)Docker Desktop: wait; otherwise kubectl port-forward
localhost:30082 unreachableNodePort not created / port busykubectl get svc demo-nodeport; change nodePort
ImagePullBackOffImage demo-k8s:1.0 missing from the clusterBuild it; on kind: kind load docker-image demo-k8s:1.0
powershell
# Universal temporary access solution to any Service:
kubectl port-forward svc/demo-clusterip 8091:80   # -> http://localhost:8091

Course created by Dr. Haythem REHOUMA — Development and Deployment of Data Solutions