|
| 1 | +# Deploying Apps In Kubernetes |
| 2 | + |
| 3 | +From skillshare session 22/10/2025 |
| 4 | + |
| 5 | +## Namespaces |
| 6 | + |
| 7 | +- Namespaces are logical partitions within a Kubernetes cluster. |
| 8 | + |
| 9 | +- They allow you to group related resources and apply policies |
| 10 | + (role based authentication RBAC, resource quotas, network policies). |
| 11 | + |
| 12 | +```bash |
| 13 | +kubectl create namespace oam |
| 14 | +``` |
| 15 | + |
| 16 | +- While it's possible to run a namespace per deployment |
| 17 | + environment - dev/stage/prod - it's a bit cleaner to |
| 18 | + have a separate cluster per environment. |
| 19 | + |
| 20 | +- Namespaces can be used to easily organise logical |
| 21 | + application units, e.g. a `oam`, or `imagery` namespace. |
| 22 | + |
| 23 | +## Anatomy of manifests |
| 24 | + |
| 25 | +- Every Kubernetes manifest follows the same high-level structure: |
| 26 | + |
| 27 | +```yaml |
| 28 | +apiVersion: apps/v1 |
| 29 | +kind: Deployment |
| 30 | +metadata: |
| 31 | + name: nginx-deployment |
| 32 | + labels: |
| 33 | + app: nginx |
| 34 | +spec: |
| 35 | + replicas: 2 |
| 36 | + selector: |
| 37 | + matchLabels: |
| 38 | + app: nginx |
| 39 | + template: |
| 40 | + metadata: |
| 41 | + labels: |
| 42 | + app: nginx |
| 43 | + spec: |
| 44 | + containers: |
| 45 | + - name: nginx |
| 46 | + image: nginx:1.27-alpine |
| 47 | +``` |
| 48 | +
|
| 49 | +| Field | Purpose | |
| 50 | +| -------------- | ------------------------------------------------------------ | |
| 51 | +| **apiVersion** | Defines which API group/version the resource uses. | |
| 52 | +| **kind** | The type of resource (`Pod`, `Service`, `Deployment`, etc.). | |
| 53 | +| **metadata** | Identifiers: name, labels, annotations, namespace. | |
| 54 | +| **spec** | The desired configuration (replicas, template, ports, etc.). | |
| 55 | + |
| 56 | +## Networking: Services & Ingress |
| 57 | + |
| 58 | +### Services |
| 59 | + |
| 60 | +- A Service defines how to reach a set of Pods inside the cluster. |
| 61 | + |
| 62 | +- Each Service has a stable virtual IP (ClusterIP) and DNS name, |
| 63 | + and routes traffic to all matching Pods via labels. |
| 64 | + |
| 65 | +- Service types: |
| 66 | + - **ClusterIP**: internal only (default) |
| 67 | + - **NodePort**: expose a port on each node |
| 68 | + - **LoadBalancer**: integrate with cloud load balancers |
| 69 | + - **ExternalName**: DNS alias for external resources |
| 70 | + |
| 71 | +Example: |
| 72 | + |
| 73 | +```yaml |
| 74 | +apiVersion: v1 |
| 75 | +kind: Service |
| 76 | +metadata: |
| 77 | + name: nginx-service |
| 78 | + namespace: oam |
| 79 | +spec: |
| 80 | + selector: |
| 81 | + app: nginx |
| 82 | + ports: |
| 83 | + - port: 80 |
| 84 | + targetPort: 80 |
| 85 | + type: ClusterIP |
| 86 | +``` |
| 87 | + |
| 88 | +- Typically ClusterIP will be used for most apps, with |
| 89 | + an Ingress defined for the actual external access. |
| 90 | + |
| 91 | +### Ingress |
| 92 | + |
| 93 | +- An Ingress defines external access to Services, typically via HTTP/HTTPS. |
| 94 | + |
| 95 | +- It acts as a router or reverse proxy, mapping domain names and paths to Services. |
| 96 | + |
| 97 | +- Requires an **Ingress Controller** (e.g., NGINX, AWS ALB, Traefik). |
| 98 | + |
| 99 | +Example: |
| 100 | + |
| 101 | +```yaml |
| 102 | +apiVersion: networking.k8s.io/v1 |
| 103 | +kind: Ingress |
| 104 | +metadata: |
| 105 | + name: nginx-ingress |
| 106 | + namespace: oam |
| 107 | + annotations: |
| 108 | + nginx.ingress.kubernetes.io/rewrite-target: / |
| 109 | +spec: |
| 110 | + rules: |
| 111 | + - host: nginx.local |
| 112 | + http: |
| 113 | + paths: |
| 114 | + - path: / |
| 115 | + pathType: Prefix |
| 116 | + backend: |
| 117 | + service: |
| 118 | + name: nginx-service |
| 119 | + port: |
| 120 | + number: 80 |
| 121 | +``` |
| 122 | + |
| 123 | +### Practical: make Nginx accessible |
| 124 | + |
| 125 | +- From previous example, add the Service and Ingress definitions |
| 126 | + above to the same `nginx.yaml`, with each divided by `---` between. |
| 127 | + |
| 128 | +```bash |
| 129 | +# Apply over the top |
| 130 | +kubectl apply -f nginx.yaml -n oam |
| 131 | +
|
| 132 | +# Verify |
| 133 | +kubectl get ingress -n oam |
| 134 | +kubectl get svc -n oam |
| 135 | +``` |
| 136 | + |
| 137 | +- Access on `http://nginx.local`. |
| 138 | + |
| 139 | +- Alternatively, we can do a port forward to access the internal |
| 140 | + service: |
| 141 | + |
| 142 | +```bash |
| 143 | +kubectl port-forward svc/nginx-service 8080:80 |
| 144 | +``` |
| 145 | + |
| 146 | +## Probes |
| 147 | + |
| 148 | +- Probes let Kubernetes know whether your container is healthy |
| 149 | + and ready for traffic. |
| 150 | + |
| 151 | +- **Liveness Probe**: checks if the container is still running |
| 152 | + properly. If it fails repeatedly, Kubernetes restarts the |
| 153 | + container. |
| 154 | + |
| 155 | +- **Readiness Probe**: checks if the app is ready to serve traffic. |
| 156 | + If it fails, the pod is temporarily removed from Service endpoints. |
| 157 | + |
| 158 | +### Practical: add probes to Nginx |
| 159 | + |
| 160 | +- Add the following to the deployment spec in |
| 161 | + `nginx.yaml`: |
| 162 | + |
| 163 | +```yaml |
| 164 | + readinessProbe: |
| 165 | + httpGet: |
| 166 | + path: / |
| 167 | + port: 80 |
| 168 | + initialDelaySeconds: 3 |
| 169 | + periodSeconds: 5 |
| 170 | + livenessProbe: |
| 171 | + httpGet: |
| 172 | + path: / |
| 173 | + port: 80 |
| 174 | + initialDelaySeconds: 10 |
| 175 | + periodSeconds: 10 |
| 176 | +``` |
| 177 | + |
| 178 | +```bash |
| 179 | +# Apply over the top |
| 180 | +kubectl apply -f nginx.yaml -n oam |
| 181 | +
|
| 182 | +# View pod health |
| 183 | +kubectl get pod -n oam |
| 184 | +kubectl describe pod <pod-name> -n oam |
| 185 | +``` |
| 186 | + |
| 187 | + |
| 188 | +## Resource constraints |
| 189 | + |
| 190 | +- Resource requests and limits prevent a single container |
| 191 | + from consuming too many cluster resources. |
| 192 | + |
| 193 | +| Type | Purpose | |
| 194 | +| ------------ | -------------------------------------------------- | |
| 195 | +| **requests** | The *minimum* guaranteed CPU/memory the Pod needs. | |
| 196 | +| **limits** | The *maximum* it can consume. | |
| 197 | + |
| 198 | +### Practical: add resouce constraints to Nginx |
| 199 | + |
| 200 | +- Add the following to the deployment spec in |
| 201 | + `nginx.yaml`: |
| 202 | + |
| 203 | +```yaml |
| 204 | + resources: |
| 205 | + requests: |
| 206 | + cpu: 50m |
| 207 | + memory: 64Mi |
| 208 | + limits: |
| 209 | + cpu: 250m |
| 210 | + memory: 128Mi |
| 211 | +``` |
| 212 | + |
| 213 | +```bash |
| 214 | +# Apply over the top |
| 215 | +kubectl apply -f nginx.yaml -n oam |
| 216 | +
|
| 217 | +# View pod health |
| 218 | +kubectl get pod -n oam |
| 219 | +kubectl describe pod <pod-name> -n oam |
| 220 | +kubectl top pods -n oam |
| 221 | +``` |
| 222 | + |
| 223 | +!!! note |
| 224 | + |
| 225 | + 1 CPU = 1 vCPU core, 1000m = 1 core. |
| 226 | + Kubernetes schedules pods based on **requests**. |
| 227 | + **Limits** enforce hard caps. |
| 228 | + |
| 229 | +## Rolling updates and rollbacks |
| 230 | + |
| 231 | +- Deployments automatically perform rolling updates, |
| 232 | + replacing pods gradually to avoid downtime. |
| 233 | + |
| 234 | +```bash |
| 235 | +# Upgrade the container version using rolling update |
| 236 | +kubectl set image deployment/nginx-deployment nginx=nginx:1.28-alpine -n oam |
| 237 | +
|
| 238 | +# Monitor update |
| 239 | +kubectl rollout status deployment/nginx-deployment -n oam |
| 240 | +
|
| 241 | +# Rollback |
| 242 | +kubectl rollout undo deployment/nginx-deployment -n oam |
| 243 | +``` |
0 commit comments