Project projet11-kubernetes-services · all commands to deploy, expose, and test Services.
From the project folder (projet11-kubernetes-services):
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 Runningkubectl get svc # TYPE, CLUSTER-IP, EXTERNAL-IP, PORT(S)
kubectl get svc -o wide
kubectl describe svc demo-clusterip # details + EndpointsHow to read kubectl get svc:
| Column | Meaning |
|---|---|
TYPE | ClusterIP / NodePort / LoadBalancer |
CLUSTER-IP | Internal IP (always present) |
EXTERNAL-IP | External IP (LoadBalancer) or <none> |
PORT(S) | Service port (and :nodePort for NodePort) |
A ClusterIP is not reachable from the host: you call it from a Pod, by its name.
# Disposable client Pod (--rm: deletes on exit)
kubectl run client --rm -it --image=curlimages/curl -- shThen, in the Pod shell:
curl http://demo-clusterip # call BY NAME (resolved by CoreDNS)
curl http://demo-clusterip # run again: the Pod name changes
exitTest DNS resolution:
kubectl run dnstest --rm -it --image=busybox:1.36 -- sh
# in the Pod:
nslookup demo-clusterip
wget -qO- http://demo-clusteripkubectl get svc demo-nodeport
# Open in the browser:
start http://localhost:30082
# or test from the command line:
curl http://localhost:30082kubectl get svc demo-lb # EXTERNAL-IP must become "localhost"
start http://localhost:8090
curl http://localhost:8090If
EXTERNAL-IPstays<pending>, wait a few seconds (Docker Desktop provisions the LB). On a kind cluster, the LoadBalancer type stays<pending>without MetalLB: usekubectl port-forward(see appendix).
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 selectorSee the distribution (10 requests via port-forward):
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 }kubectl delete -f k8s/
# or by object:
kubectl delete svc demo-clusterip demo-nodeport demo-lb
kubectl delete deployment demo-back| Symptom | Likely cause | Solution |
|---|---|---|
curl http://demo-clusterip fails | Not from a Pod, or missing service | Call from a Pod; check kubectl get svc |
| Empty Endpoints | The selector matches no Pod label | Align selector (Service) and labels (Pods) |
EXTERNAL-IP in <pending> | No load balancer (kind, unconfigured cloud) | Docker Desktop: wait; otherwise kubectl port-forward |
localhost:30082 unreachable | NodePort not created / port busy | kubectl get svc demo-nodeport; change nodePort |
ImagePullBackOff | Image demo-k8s:1.0 missing from the cluster | Build it; on kind: kind load docker-image demo-k8s:1.0 |
# Universal temporary access solution to any Service:
kubectl port-forward svc/demo-clusterip 8091:80 # -> http://localhost:8091Course created by Dr. Haythem REHOUMA — Development and Deployment of Data Solutions