Day 3 of 5
⏱ ~60 minutes
Kubernetes in 5 Days — Day 3

Services and Networking

Expose Pods with ClusterIP, NodePort, and LoadBalancer services. Set up an Ingress for HTTP routing.

Why Services Exist

Pods get new IP addresses every time they restart. Services provide a stable IP and DNS name that routes to the current healthy Pods. Services are how Pods find each other.

service.yaml
# ClusterIP: internal-only, for pod-to-pod communication
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  type: ClusterIP  # or NodePort or LoadBalancer
  selector:
    app: web-app  # routes to pods with this label
  ports:
  - port: 80       # port the service listens on
    targetPort: 80  # port the pod listens on

---
# NodePort: accessible from outside the cluster on a high port
# LoadBalancer: provisions a cloud load balancer (AWS, GCP, Azure)
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080  # 30000-32767 range
Ingress
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: api-service
            port:
              number: 80
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web-service
            port:
              number: 80
ℹ️
Ingress is a single entry point that routes traffic to multiple services based on hostname and path. It requires an Ingress Controller (nginx is the most common) installed in the cluster. On minikube: minikube addons enable ingress.
📝 Day 3 Exercise
Expose Your App
  1. C
  2. r
  3. e
  4. a
  5. t
  6. e
  7. a
  8. D
  9. e
  10. p
  11. l
  12. o
  13. y
  14. m
  15. e
  16. n
  17. t
  18. +
  19. S
  20. e
  21. r
  22. v
  23. i
  24. c
  25. e
  26. .
  27. U
  28. s
  29. e
  30. N
  31. o
  32. d
  33. e
  34. P
  35. o
  36. r
  37. t
  38. t
  39. o
  40. a
  41. c
  42. c
  43. e
  44. s
  45. s
  46. i
  47. t
  48. f
  49. r
  50. o
  51. m
  52. y
  53. o
  54. u
  55. r
  56. b
  57. r
  58. o
  59. w
  60. s
  61. e
  62. r
  63. v
  64. i
  65. a
  66. m
  67. i
  68. n
  69. i
  70. k
  71. u
  72. b
  73. e
  74. .
  75. E
  76. n
  77. a
  78. b
  79. l
  80. e
  81. t
  82. h
  83. e
  84. I
  85. n
  86. g
  87. r
  88. e
  89. s
  90. s
  91. a
  92. d
  93. d
  94. o
  95. n
  96. a
  97. n
  98. d
  99. c
  100. r
  101. e
  102. a
  103. t
  104. e
  105. a
  106. n
  107. I
  108. n
  109. g
  110. r
  111. e
  112. s
  113. s
  114. r
  115. e
  116. s
  117. o
  118. u
  119. r
  120. c
  121. e
  122. r
  123. o
  124. u
  125. t
  126. i
  127. n
  128. g
  129. /
  130. a
  131. p
  132. p
  133. t
  134. o
  135. y
  136. o
  137. u
  138. r
  139. s
  140. e
  141. r
  142. v
  143. i
  144. c
  145. e
  146. .

Day 3 Summary

  • Services give Pods a stable network identity. Select Pods by label.
  • ClusterIP = internal only. NodePort = external on a high port. LoadBalancer = cloud LB.
  • Ingress routes HTTP/HTTPS traffic to services based on hostname and URL path.
  • kubectl get svc and kubectl describe svc are your debugging tools for networking.
Finished this lesson?