Master Kubernetes Services for stable networking - ClusterIP, NodePort, LoadBalancer, and ExternalName. Learn DNS, service discovery, and network policies.
Services are a fundamental Kubernetes abstraction that provides stable networking for Pods. Since Pods are ephemeral and their IP addresses change when they're recreated, Services provide a stable endpoint that abstracts away the underlying Pod dynamics.
In this lesson, you'll learn about different Service types, understand the Kubernetes networking model, and master service discovery and network policies.
Kubernetes networking is built on several key principles that ensure containers can communicate efficiently.
Fundamental Requirements:
Network Components:
IP Address Allocation:
ClusterIP is the default Service type, providing internal-only access within the cluster.
Characteristics:
Use Cases:
How It Works:
NodePort and LoadBalancer Services expose applications to external traffic.
NodePort:
LoadBalancer:
Comparison:
| Feature | ClusterIP | NodePort | LoadBalancer |
|---------|-----------|----------|--------------|
| Internal access | Yes | Yes | Yes |
| External access | No | Yes | Yes |
| Cloud LB | No | No | Yes |
| Port range | Any | 30000-32767 | Any |
Special Service types for specific use cases.
Headless Services (clusterIP: None):
ExternalName Services:
Use Cases:
Kubernetes provides built-in DNS for service discovery. CoreDNS runs as a deployment in kube-system namespace.
DNS Records:
DNS Search Domains:
Best Practices:
Network Policies control traffic flow between Pods. By default, all Pods can communicate with each other. Network Policies add firewall-like rules.
Default Behavior:
Network Policy Effects:
Policy Types:
Requirements:
Create a complete networking setup with frontend, backend, and database Services.
1#!/bin/bash
2# Kubernetes Services Lab
3# =======================
4
5set -e
6
7echo "========================================"
8echo " Kubernetes Services Lab"
9echo "========================================"
10
11# Create namespace
12kubectl create namespace services-lab --dry-run=client -o yaml | kubectl apply -f -
13
14# 1. Deploy backend with ClusterIP
15echo -e "
16[1] Deploying backend service..."
17cat <<EOF | kubectl apply -n services-lab -f -
18apiVersion: apps/v1
19kind: Deployment
20metadata:
21 name: backend
22spec:
23 replicas: 2
24 selector:
25 matchLabels:
26 app: backend
27 template:
28 metadata:
29 labels:
30 app: backend
31 spec:
32 containers:
33 - name: backend
34 image: hashicorp/http-echo
35 args:
36 - "-text=Backend Response"
37 ports:
38 - containerPort: 5678
39---
40apiVersion: v1
41kind: Service
42metadata:
43 name: backend
44spec:
45 type: ClusterIP
46 selector:
47 app: backend
48 ports:
49 - port: 80
50 targetPort: 5678
51EOF
52
53# 2. Deploy frontend with NodePort
54echo -e "
55[2] Deploying frontend with NodePort..."
56cat <<EOF | kubectl apply -n services-lab -f -
57apiVersion: apps/v1
58kind: Deployment
59metadata:
60 name: frontend
61spec:
62 replicas: 2
63 selector:
64 matchLabels:
65 app: frontend
66 template:
67 metadata:
68 labels:
69 app: frontend
70 spec:
71 containers:
72 - name: frontend
73 image: nginx:alpine
74 ports:
75 - containerPort: 80
76---
77apiVersion: v1
78kind: Service
79metadata:
80 name: frontend
81spec:
82 type: NodePort
83 selector:
84 app: frontend
85 ports:
86 - port: 80
87 targetPort: 80
88 nodePort: 30080
89EOF
90
91# Wait for deployments
92kubectl rollout status deployment/backend -n services-lab
93kubectl rollout status deployment/frontend -n services-lab
94
95# 3. Test DNS resolution
96echo -e "
97[3] Testing DNS resolution..."
98kubectl run dns-test --rm -it --image=busybox -n services-lab --restart=Never -- nslookup backend.services-lab.svc.cluster.local || true
99
100# 4. Test service connectivity
101echo -e "
102[4] Testing service connectivity..."
103kubectl run curl-test --rm -it --image=curlimages/curl -n services-lab --restart=Never -- curl -s http://backend || true
104
105# 5. Show services
106echo -e "
107[5] Services in the namespace..."
108kubectl get svc -n services-lab -o wide
109
110# 6. Show endpoints
111echo -e "
112[6] Service endpoints..."
113kubectl get endpoints -n services-lab
114
115# 7. Apply network policy
116echo -e "
117[7] Applying network policy..."
118cat <<EOF | kubectl apply -n services-lab -f -
119apiVersion: networking.k8s.io/v1
120kind: NetworkPolicy
121metadata:
122 name: backend-policy
123spec:
124 podSelector:
125 matchLabels:
126 app: backend
127 policyTypes:
128 - Ingress
129 ingress:
130 - from:
131 - podSelector:
132 matchLabels:
133 app: frontend
134 ports:
135 - port: 5678
136EOF
137
138kubectl get networkpolicy -n services-lab
139
140echo -e "
141========================================"
142echo " Lab Complete!"
143echo "========================================"
144echo ""
145echo "Services:"
146kubectl get svc -n services-lab
147echo ""
148echo "Access frontend via NodePort: http://<node-ip>:30080"
149echo ""
150echo "Cleanup: kubectl delete namespace services-lab"Your progress is saved in this browser, on this device only.