DevOps - Kubernetes Part 4
DevOps - Kubernetes Tutorial - Part 4
Update a Java API and explore configuration, storage, networking, and deployment tools.
1Workload Architecture
Part 3 introduced a Java API, Deployment, and Service. We will use that example to learn updates and configuration. This diagram is a general architecture, not an automatically installed solution.
A ConfigMap and Secret can supply configuration; a PersistentVolumeClaim requests storage. Ingress or Gateway must be configured separately in the cluster.
2Live Example Prerequisites
The real commands require your own local cluster and the a143mk-k8s-demo namespace, a143mk-api Deployment and Service from Part 3. If you removed them, recreate the Part 3 example first.
kubectl config current-context
kubectl -n a143mk-k8s-demo get deployment,service
kubectl -n a143mk-k8s-demo rollout status deployment/a143mk-api --timeout=300s
kubectl -n a143mk-k8s-demo set env deployment/a143mk-api --list
Confirm the context is your local cluster and the resources are your Part 3 demo. Stop if DEMO_RELEASE or APP_GREETING already exists; the exercises use those names. Run commands one at a time and continue only after each succeeds. The environment list may contain sensitive values; do not publish it.
3Rolling Update and Rollback
Changing a Deployment's Pod template starts a new rollout. Kubernetes gradually brings up new Pods and removes old ones; readiness and available capacity affect the result.
Interactive diagram: This browser simulation illustrates stages; it does not change a cluster.
Real local update
After the prerequisite checks, add a non-sensitive environment variable only to your demo Deployment. This creates a new Pod template, but does not change the Java API response or image.
kubectl -n a143mk-k8s-demo set env deployment/a143mk-api DEMO_RELEASE=v2
kubectl -n a143mk-k8s-demo rollout status deployment/a143mk-api --timeout=300s
kubectl -n a143mk-k8s-demo rollout history deployment/a143mk-api
kubectl -n a143mk-k8s-demo get pods -l app=a143mk-api
If an earlier revision is present, inspect it with kubectl -n a143mk-k8s-demo rollout history deployment/a143mk-api --revision=N, replacing N with its revision number. Roll back your demo only if it is the intended previous template. Rollback does not automatically restore external data or ConfigMaps.
kubectl -n a143mk-k8s-demo rollout undo deployment/a143mk-api
kubectl -n a143mk-k8s-demo rollout status deployment/a143mk-api --timeout=300s
kubectl -n a143mk-k8s-demo set env deployment/a143mk-api --list
4Practical ConfigMap
A ConfigMap holds non-sensitive configuration. Here we add a demo variable to the container environment. The Part 3 Java API does not read it, so its JSON response will not change.
First check that the name is unused. Stop if you get an error other than NotFound or the ConfigMap already exists. Then create and attach your ConfigMap:
kubectl -n a143mk-k8s-demo get configmap a143mk-app-config
Only if the result is NotFound, create it; attach it to the Deployment only after creation succeeds:
kubectl -n a143mk-k8s-demo create configmap a143mk-app-config --from-literal=APP_GREETING=Hello
kubectl -n a143mk-k8s-demo set env deployment/a143mk-api --from=configmap/a143mk-app-config
kubectl -n a143mk-k8s-demo rollout status deployment/a143mk-api --timeout=300s
kubectl -n a143mk-k8s-demo exec deployment/a143mk-api -- printenv APP_GREETING
The final command should print Hello. Environment variables are supplied when a container starts; changing the ConfigMap does not automatically change an already running container's environment.
Remove only your demo configuration
If you created the variable and ConfigMap above and have finished the exercise, first remove the variable from the Deployment. Delete your ConfigMap only after the rollout succeeds:
kubectl -n a143mk-k8s-demo set env deployment/a143mk-api APP_GREETING-
kubectl -n a143mk-k8s-demo rollout status deployment/a143mk-api --timeout=300s
Only after the rollout succeeds, delete only the ConfigMap you created:
kubectl -n a143mk-k8s-demo delete configmap a143mk-app-config
5Secrets and Security
Do not put passwords, tokens, or keys in a ConfigMap, Git, image, or blog example. A Kubernetes Secret is a separate resource for sensitive data, but base64 is not encryption.
- Protect access: Configure least-privilege RBAC, encryption at rest in etcd, and appropriate auditing.
- Manage lifecycle: Use your platform's approved secret manager and rotation process; keep secrets out of command history, logs, and screenshots.
- Limit exposure: Give a Secret only to workloads that need it; design applications to handle rotation and errors.
Read more: Kubernetes Secrets.
6Persistent Storage
A container's writable layer is not suitable for persistent data. A PersistentVolumeClaim (PVC) requests storage from the cluster; a suitable StorageClass or existing PersistentVolume is required.
This YAML is illustrative; do not apply it yet. Select the storage class, access mode, capacity, permissions, and backups for your cluster. The Java API does not mount this PVC.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: example-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
ReadWriteOnce means read-write mounting on one node, not necessarily one Pod. Deleting a PVC or backing storage can cause data loss; check reclaim policy and backups.7Service and Ingress
The Part 3 ClusterIP Service is normally reachable inside the cluster. Local testing uses kubectl port-forward; external HTTP access needs additional components such as an Ingress controller or Gateway.
This HTTP Ingress YAML is illustrative. It requires an appropriate nginx IngressClass and controller, DNS, and the Part 3 Service in the same namespace. It does not configure TLS or authentication; do not expose it directly to the public.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-api
spec:
ingressClassName: nginx
rules:
- host: api.example.test
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: a143mk-api
port:
number: 80
The hostname api.example.test is only a placeholder. Gateway API is another option and also requires a supported controller and configuration.
8Autoscaling and Observability
A Horizontal Pod Autoscaler (HPA) can adjust replicas based on metrics. CPU-based HPA needs resource metrics and CPU requests for relevant containers; node capacity does not automatically increase.
This YAML is for learning; verify Metrics Server, the target Deployment, policy, and capacity before applying it. The Part 3 demo already has a CPU request.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: a143mk-api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: a143mk-api
minReplicas: 2
maxReplicas: 4
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
The 70% target is average utilization relative to CPU requests, not 70% of a CPU limit. HPA and manual replica changes can affect each other.
- Logs: Inspect application logs and Pod events; keep sensitive data out of logs.
- Metrics: With Metrics Server available, use
kubectl top pods. Complete monitoring needs separate metrics collection and alerts. - Traces and alerts: Observe latency, error rates, and request flow; test alerts for the application's needs.
9Tools and CI/CD Flow
This is a conceptual flow, not a configured pipeline. The Part 3 demo runs source from a ConfigMap; for real CI/CD, first prepare a testable application image and secure registry access.
- kubectl: CLI for inspecting rollouts, logs, and resources.
- Helm or Kustomize: Tools to package or customize Kubernetes configuration; review rendered changes before applying.
- Prometheus and Grafana: Common tools for metrics collection/querying and dashboards, respectively; configure security and alerts separately.
- CI/CD platforms: GitHub Actions, GitLab CI/CD, or Jenkins can automate tests and deployments; restrict cluster credentials.
Documentation: Deployments, Kustomize, and Prometheus.
10Troubleshooting and Checklist
For inspection only, in your Part 3 demo namespace:
kubectl -n a143mk-k8s-demo get deployment,pods,service
kubectl -n a143mk-k8s-demo describe deployment a143mk-api
kubectl -n a143mk-k8s-demo logs deployment/a143mk-api --tail=50
kubectl -n a143mk-k8s-demo rollout history deployment/a143mk-api
- Rollout stalled: Check Pod events, image pulls, available resources, and readiness probes.
- ConfigMap value missing: Check the name, key, and Deployment environment; wait for a new Pod to become ready.
- API call fails: Check ready Pods and the Service. A rollout can remove the Pod selected by port-forward; restart it if needed.
- PVC Pending: Check StorageClass, available PersistentVolumes, access mode, and provisioner events.
11Quick Quiz
12What to Learn Next
Next: package a Java application into an image and learn build, test, scan, and Kubernetes deployment in a secure CI/CD pipeline.
No comments :
Post a Comment
Please Write a Message for Programming or something else 🙏