https://drive.google.com/file/d/1Z0gn4y0JFBx2YRg4a-zB9AsIr6RSfeTk/view?usp=sharing
# Service
apiVersion: v1
kind: Service
metadata:
name: nnweb-svc
namespace: learning
labels:
app: hello-nn
spec:
type: NodePort # โ Exposes on ALL node IPs
ports:
- port: 80 # โ Service port (internal)
nodePort: 30003 # โ Fixed external port (30000-32767)
protocol: TCP
selector:
app: hello-nn # โ Must match Deployment labels
---
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-deploy
namespace: learning
spec:
replicas: 2
selector:
matchLabels:
app: hello-nn
template:
metadata:
labels:
app: hello-nn
spec:
containers:
- name: webserver-pod
image: lovelearnlinux/webserver:v1
ports:
- containerPort: 80
๐ Key Insight:
- NodePort Service opens port
30003on every node in the cluster- Traffic to
http://<ANY-NODE-IP>:30003โ load-balanced to Pods withapp: hello-nn
| Component | Role |
|---|---|
type: NodePort |
Tells Kubernetes to open a port on all nodes |
nodePort: 30003 |
Fixed external port (optional; if omitted, k3s assigns random 30000-32767) |
port: 80 |
Internal Service port (used by ClusterIP) |
selector |
Links to Pods (app: hello-nn) |
| kube-proxy | Configures iptables rules on each node to forward :30003 โ Pods |
๐ฏ Access Methods:
- Internal:
curl http://nnweb-svc.learning.svc:80(from inside cluster)- External:
curl http://<NODE-IP>:30003(from laptop, browser, etc.)
๐ก k3s Advantage:
No cloud provider needed โ NodePort works out-of-the-box on bare metal!
# Create namespace
kubectl create namespace learning
# Apply Deployment + Service
kubectl apply -f service-nodeport.yml
# Verify
kubectl get deploy,svc,pods -n learning
# Check Pod IPs linked to Service
kubectl get endpoints nnweb-svc -n learning
# โ
Expected:
# NAME ENDPOINTS AGE
# nnweb-svc 10.42.0.10:80,10.42.1.15:80 10s
# From a debug Pod
kubectl run debug -n learning --image=curlimages/curl -it --rm -- sh
# Inside shell:
curl <http://nnweb-svc:80>
# โ
"Welcome to CrackOne!"
exit
๐ก Get k3s node IPs:
kubectl get nodes -o wide
# NAME STATUS INTERNAL-IP
# k3s-master Ready 192.168.1.10
# k3s-node1 Ready 192.168.1.11