Kubernetes is Not Scary, You're Just Not Declarative Enough

Kubernetes is Not Scary, You're Just Not Declarative Enough

  1. kubernetes
  2. 2025-06-24 09:00
  3. 15 min read

🚀 Kubernetes is Not Scary, You’re Just Not Declarative Enough

Deconstructing K8s Like a Pro With Best Practices and Gen Z Brains

嘿, dev friend! 🤘 Kubernetes has straight-up dominated the cloud-native universe. No cap. It’s the default boss-level platform for deploying and managing containerized apps. Да, оно сложное. Но только если ты не знаешь, как им правильно пользоваться.

So let’s break it down like it’s your fav open-source beat — full of declarative vibes, GitOps sauce, and autoscaling groove.


🧬 The Declarative Foundation: You Describe It, K8s Makes It So

apiVersion: v1
kind: Pod
metadata:
  name: chill-app
spec:
  containers:
    - name: chill-container
      image: myapp:v1

Look familiar? That’s Kubernetes saying: “Tell me what you want, not how to do it.”

这叫 声明式配置,and it’s the GOAT of stability. You’re writing desired state, not scripts. This means:

  • Everything is self-documenting
  • Easy to version control
  • Git + YAML = 💍 (marriage of ops and dev)

💡 Pro tip: Manage config like code. Store YAMLs in Git, review them in PRs, roll ‘em through CI/CD. GitOps is life.


🧱 Core Resources: The Building Blocks of 🔥 App Management

🌀 Deployments – Your App’s Main Driver

Declarative, scalable, resilient.

replicas: 3
resources:
  requests:
    cpu: "250m"
    memory: "128Mi"
  limits:
    cpu: "500m"
    memory: "256Mi"

Ты должен указать ресурсы. Requests = guaranteed, Limits = cap. 设置它们像 boss,不然你的 app 会被其他服务吃掉资源。

Rolling updates? Built-in. Zero-downtime deploys? No stress. Stateful apps? Use StatefulSet, not Deployment. More on that below 👇


🌐 Services – Stable Networking, Internal & External

K8s 服务让你的 Pods 通信如丝般顺滑。

  • ClusterIP – 默认的, for internal service-to-service.
  • NodePort – 用于本地实验和 edge 访问.
  • LoadBalancer – 云原生 style,直接暴露服务到公网.
  • ExternalName – 当你需要访问外部服务但想保留 K8s 风格的 DNS.

🧠 Think of Services as: DNS + Load Balancer + Firewall rules = 1 object


🌍 Ingress – Smarter HTTP(S) Routing

You want /api to hit one backend and /images another? Say hello to Ingress.

rules:
  - host: app.cool.io
    http:
      paths:
        - path: /api
          backend:
            service:
              name: api-service
              port:
                number: 80

💥 Ingress 控制器必须安装,比如 NGINX 或 Traefik. And Gateway API 是它的新进化 – 更强大, 更灵活.


🔐 Configs and Secrets – Your App’s Memory and Mind

🔧 ConfigMaps – App Settings Without Code Changes

Store JSON configs, URLs, even whole files. But remember:

  • Don’t edit them in-place.
  • Version them (like v1, v2) and re-deploy.

如果你更新 ConfigMap,不会自动更新 Pod. 你得 roll 个新的 Deployment 才能生效。


🔐 Secrets – Sensitive AF

密码、令牌、TLS keys?Use Secret. Stored in etcd, encoded (base64, not encrypted). Encrypt at rest or plug into KMS. И не забудь:

  • Mount them as tmpfs volumes (不会写到磁盘上).
  • Restart Pods to apply updates.

Security game on fleek. 🧢


💾 State, Storage & Organisation

📦 StatefulSets + PersistentVolumes = 数据可靠性

Stateful apps (databases, queues, brokers) need:

  • Consistent identity: redis-0, redis-1
  • PersistentVolumeClaim: 请求磁盘空间
  • Pod ordinal: 生命周期稳定 predictable

🤓 PVs are backed by cloud disks or NFS. Kubernetes dynamically binds them to PVCs.


📁 Namespaces + ResourceQuota = Cluster Peace

  • 每个团队一个 namespace.
  • Use LimitRange to set default resource requests.
  • Apply ResourceQuota to prevent noisy neighbours.

🛡️ Rule: Never deploy to default or kube-system. Be civilised.


🔐 RBAC – Access Like a True Minimalist

控制谁能干啥:Roles, ClusterRoles, Bindings.

kind: Role
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list"]

最小权限原则 (least privilege) 不是说说而已。Use ServiceAccounts and external auth providers (OIDC, SSO) for serious ops.


🛠️ CI/CD, Dev Flows, Monitoring – Your Daily Grind

🧑‍💻 Developer Workflows

One big dev cluster > many small ones. Set up:

  • Namespaces per dev
  • Shared Prometheus, Loki, Grafana
  • Preview environments with ephemeral namespaces (自动创建, 自动删除)

🔄 GitOps & Pipelines

From commit to prod:

  1. Push to Git
  2. CI runs tests, builds image
  3. CD applies YAML to cluster
  4. Rollbacks? Just revert the commit.

自动化 > 复制粘贴. Simplicity > manual patching. Coffee > PagerDuty at 3am.


📊 Observability: Prometheus + Loki = Eyes Everywhere

Metrics: CPU, memory, response latency – use Prometheus. Logs: Structured, searchable – use Loki. Alerts: Tie them to SLOs, not every warning. Save your devs’ sanity.


🔚 Conclusion – Embrace the Kube

Kubernetes 不是神秘的. 它只是需要 正确定义和理解. Learn its language – YAML, Services, Ingress, RBAC – and you’re not just deploying containers. You’re running a microservice kingdom. 👑

🌍 English: K8s gives you power, but demands clarity. 🇷🇺 Русский: Это как джунгли — если знаешь карту, доберешься быстро. 🇨🇳 中文: 学会了这些核心概念,你就能如鱼得水,稳定如山。

So go out, declarative warrior. Git commit your destiny. Kubernetes awaits. 🐳⚙️🔥

Deep dive about the book.

Learn the core principles of Kubernetes

Boom 💥! Let’s level up and test what you just learned with a few quizzes – quick, sharp, and solid for reinforcement.

These are mixed difficulty:

  • 🧠 Basics to Intermediate
  • 🧠💥 Advanced Ops and Architecture

Answers included at the bottom of each section (no cheating 👀).


📚 Quiz 1: Kubernetes Fundamentals (🧠 Basics)

1. What does “declarative configuration” mean in Kubernetes? A. You write scripts with exact steps to run B. You define the desired state and Kubernetes makes it so C. You install Helm charts manually D. You use kubectl run every time

Answer: B – Declarative means defining what you want, not how to get there.


2. Which resource is used to create replicas of your app and manage rolling updates? A. Pod B. ConfigMap C. Deployment D. CronJob

Answer: C – Deployments handle replicas, rollouts, and versioning.


3. What’s the default Service type in Kubernetes? A. LoadBalancer B. NodePort C. ClusterIP D. ExternalName

Answer: C – ClusterIP is used for internal communication.


4. In Kubernetes, which resource is best suited for managing sensitive data like API keys? A. ConfigMap B. PersistentVolume C. Secret D. VolumeMount

Answer: C – Secrets handle sensitive data, with base64 (⚠️ not encryption by default).


5. What must you do for a pod to receive updated Secret data? A. Use kubectl apply B. Edit the Secret in-place C. Restart the pod D. Nothing, it happens automatically

Answer: C – You must restart the pod; Secrets don’t auto-refresh.


⚙️ Quiz 2: Ops, Storage & Networking (🧠💥 Intermediate-Advanced)

1. What’s the difference between a Deployment and a StatefulSet? A. Deployment is for system services, StatefulSet is for stateless apps B. StatefulSet supports sticky identity, Deployment doesn’t C. StatefulSet doesn’t allow replicas D. They are identical

Answer: B – StatefulSet ensures consistent naming (app-0, app-1), useful for databases.


2. What does LimitRange do inside a Namespace? A. Enforces RBAC policies B. Specifies max number of pods C. Sets default CPU/memory requests and limits D. Configures auto-scaling rules

Answer: C – It defines default resource usage per container/pod to prevent overuse.


3. Which type of Kubernetes Service would you use for exposing an app to the public internet on AWS? A. ClusterIP B. LoadBalancer C. NodePort D. ExternalName

Answer: B – LoadBalancer provisions a cloud-native LB (e.g., ELB on AWS).


4. What does a PersistentVolumeClaim (PVC) do? A. Creates disk storage B. Deletes unused volumes C. Requests storage from available PVs D. Mounts a container image

Answer: C – PVC is how a pod asks for storage; K8s binds it to a matching PV.


5. What’s the best practice for handling ConfigMap version updates? A. Edit the existing ConfigMap B. Restart the kubelet C. Create a new ConfigMap version and update the Deployment D. Wait for K8s to refresh automatically

Answer: C – Always version ConfigMaps and roll a new Deployment.


🔐 Bonus Quiz: Security & Access Control (Z3 level 💥👨‍💻)

1. Which component enforces RBAC policies in Kubernetes? A. etcd B. kubelet C. API server D. kube-proxy

Answer: C – The API server checks if the caller has the right Role/ClusterRole.


2. What’s the recommended principle for granting RBAC permissions? A. Allow all for devs B. Grant access based on namespaces C. Least privilege D. One Role per user

Answer: C – Least privilege = only what you need, nothing more.


3. How should Secrets ideally be stored for production-grade security? A. In plain YAML B. Base64 in etcd C. Encrypted via KMS or encryption providers D. Mounted as files in /tmp

Answer: C – Integrate with KMS (like AWS KMS or Vault) for encryption-at-rest.


4. What’s the main purpose of a ServiceAccount in K8s? A. Store passwords B. Track API usage C. Grant a pod specific API access D. Act as a DNS endpoint

Answer: C – Pods use ServiceAccounts to authenticate with the API server.


🏁 Wrap-up

💥 You crushed it if you got 80%+! If you didn’t – rewatch those YAMLs like Netflix re-runs. Your cluster deserves it. 🤓 Kubernetes has emerged as the de facto standard for cloud native development. While often perceived as complex, understanding its fundamental concepts empowers developers and operators to build, deploy, and manage applications more easily, quickly, and reliably. It simplifies application management with an application-oriented API, self-healing properties, and powerful tools like Deployments for seamless rollouts. This guide will walk through the core aspects of Kubernetes, drawing insights from “Kubernetes Best Practices.”

The Declarative Foundation

At its heart, Kubernetes operates on a declarative approach. This means that instead of providing a series of commands, you define the desired state of your application and its components, typically in YAML or JSON files. This declarative model is highly advantageous as it makes the cluster’s state easier to understand, replicate, and recover from issues. While both YAML and JSON are supported, YAML is generally preferred due to its human-editable nature and reduced verbosity.

These declarative configuration files become the single source of truth for your application. Consequently, their management is paramount. Best practices advocate for treating these configurations like source code:

  • Store them in Git for version control, enabling change tracking, validation, auditing, and easy rollbacks.
  • Implement code review processes for configurations, just as you would for application code.
  • Adopt a GitOps approach, which ensures that your source control always matches your production environment through automated Continuous Integration/Continuous Delivery (CI/CD) pipelines.

Core Application Building Blocks

Kubernetes offers several foundational resources that serve as the building blocks for your applications:

  • Deployments: Replicating Your Applications

    • Most services in Kubernetes are deployed as Deployment resources.
    • Deployments automatically create identical replicas of your application for redundancy and scale.
    • They are designed to manage the lifecycle of your application, combining the replication capabilities of a ReplicaSet with built-in versioning and staged rollout functionality. This allows for rolling out new versions without downtime.
    • For stateless applications, it’s a best practice to run at least two replicas to handle unexpected crashes or facilitate new version deployments without service interruption.
    • For optimal performance and stability, it is crucial to specify Request and Limit resource allocations for CPU and memory within your containers. Request defines the guaranteed reservation on the host, while Limit sets the maximum allowable usage. Setting Request equal to Limit often leads to the most predictable application behavior.
  • Services: Exposing Your Applications

    • To make your deployed applications accessible, Kubernetes uses Services, which act as internal load balancers. They provide a stable IP address and port that forwards traffic to your application’s containers.
    • Common Service types include:
      • ClusterIP: This is the default service type. It assigns an IP address from a designated range within the cluster, making it discoverable via a stable DNS name (e.g., <service_name>.<namespace_name>.svc.cluster.local) for internal communication between services.
      • LoadBalancer: This type exposes your service externally by provisioning a cloud provider’s load balancer, making your application publicly accessible.
      • NodePort: This type exposes the service on a high-level port across all nodes in the cluster (e.g., 30000-32767 range). It’s often used in on-premises environments where cloud load balancers aren’t available.
      • ExternalName: This type maps a cluster-internal service name to an external DNS name (acting as a CNAME record), allowing services within your cluster to discover external resources using familiar internal names.
  • Ingress: HTTP(S) Routing at the Edge

    • While Services handle basic Layer 3/4 load balancing, Ingress provides more sophisticated HTTP(S) routing, allowing you to direct traffic based on HTTP paths (e.g., /api, /images) or hostnames.
    • An Ingress controller (such as NGINX or HAProxy) must be running in your cluster to fulfill the Ingress rules. This enables common patterns like separating a static file server from a dynamic API backend.
    • The Gateway API is an emerging, more extensible alternative to the traditional Ingress API, offering enhanced capabilities for traffic management.

Configuration and Sensitive Data Management

Kubernetes provides dedicated resources for managing both general configuration and sensitive data:

  • ConfigMaps: For Non-Sensitive Data

    • ConfigMaps store non-sensitive configuration information, which can be simple key/value pairs or entire configuration files (like JSON or XML).
    • This information can be delivered to containers as environment variables or mounted as files within a volume.
    • A key best practice: when updating configurations, version your ConfigMaps (e.g., frontend-config-v1, frontend-config-v2) and update the Deployment to reference the new version. Directly modifying a ConfigMap in place will not trigger a rollout to existing pods, and those pods will not automatically receive the new configuration until restarted.
  • Secrets: For Sensitive Data

    • For sensitive data like passwords, API keys, or TLS certificates, Kubernetes offers the Secret resource.
    • Secrets promote separation of concerns by decoupling sensitive information from your application’s source code or container images. This allows the same application code and images to be used across different environments (development, staging, production) with different credentials.
    • It’s important to note that, by default, Secrets are stored in the etcd data store as base64-encoded, not encrypted. For enhanced security, integration with external Key Management Services (KMS) or configuring Kubernetes’ own encryption providers for etcd is highly recommended.
    • Secrets are typically mounted into containers as read-only tmpfs (RAM-backed) volumes, ensuring they are not persisted on disk even if the host is compromised. Similar to ConfigMaps, changes to Secrets require a pod restart to take effect.

Managing Persistent Data and Cluster Organization

For applications that need to maintain state, or for organizing your cluster effectively, Kubernetes offers specialized resources:

  • StatefulSets and PersistentVolumes: Managing Persistent Data

    • While Deployments are great for stateless applications, StatefulSets are designed for stateful applications like databases. They provide stronger guarantees, such as stable, unique network identifiers and consistent ordering for scaling operations (e.g., pods are named redis-0, redis-1, and scale down in reverse order).
    • Stateful applications in Kubernetes must use PersistentVolumes (PVs) to manage their storage, ensuring that data persists even if a pod migrates to a different node or restarts.
    • Applications request storage using PersistentVolumeClaims (PVCs), which act as a “request for resources”. Kubernetes then dynamically provisions or binds an available PV that matches the claim’s requirements.
  • Namespaces and Resource Management: Organizing Your Cluster

    • Namespaces provide a logical separation of resources within a single Kubernetes cluster. They act as scopes for resource naming, Role-Based Access Control (RBAC), resource quotas, and network policies. This enables a form of soft multitenancy.
    • A common best practice in shared clusters is to allocate a dedicated namespace to each team or project. It is advised to avoid using the default namespace for your applications, as it lacks inherent constraints, and to keep out of the kube-system namespace, which is reserved for internal Kubernetes components.
    • To ensure fair resource distribution among different teams or applications sharing a cluster, ResourceQuota resources can be applied to namespaces. They limit the total amount of CPU, memory, storage, or even the number of specific objects (like pods or Deployments) that a namespace can consume.
    • LimitRange resources are used to set default resource requests and limits for containers within a namespace, automatically applying these if a pod specification doesn’t define them. This prevents deployments from being rejected if ResourceQuota is enabled but individual pods don’t specify their needs.
  • RBAC: Controlling Access and Permissions

    • Role-Based Access Control (RBAC) is a fundamental security mechanism in Kubernetes that governs who (a “Subject”) can perform what actions (“Rules”) on which resources within the cluster.
    • It involves defining Roles (namespace-scoped permissions) or ClusterRoles (cluster-wide permissions) and then using RoleBindings or ClusterRoleBindings to associate these roles with users, groups, or Service Accounts.
    • A core security principle is “least privilege”, meaning that users and workloads (via Service Accounts) should only be granted the minimum necessary permissions to perform their functions. Using external identity systems for user authentication is also a recommended best practice over managing certificates directly within Kubernetes.

Beyond Deployment: Operational Basics

While the above covers the core deployment aspects, successful Kubernetes operation also relies on other foundational practices:

  • Developer Workflows: Crucial for productivity, these focus on efficient onboarding of new developers, rapid iteration during development, and effective testing. In multi-developer environments, a single large development cluster is generally recommended over individual clusters for better efficiency and shared services like monitoring and logging.
  • Continuous Integration and Delivery (CI/CD): Starts with robust version control (Git) for both application code and Kubernetes configurations. Continuous Integration (CI) emphasizes frequent, small code commits and automated testing for quick feedback loops. Continuous Deployment (CD) then automates the release of successfully tested, immutable container images through various environments, right up to production, minimizing manual intervention and configuration drift.
  • Monitoring and Logging: These are complementary for gaining full visibility into your applications and cluster. Metrics (numerical data over time, like CPU utilization or HTTP request rates) are collected by tools such as Prometheus, often leveraging Kubernetes-native components like cAdvisor and kube-state-metrics. Logs (records of events and errors) provide detailed context and can be aggregated using solutions like Loki. Effective alerting should be tied to Service-Level Objectives (SLOs), ensuring that only actionable issues notify on-call engineers, preventing alert fatigue.

Conclusion

Kubernetes’ modularity and generality enable it to host nearly any type of application imaginable. Understanding its core APIs and components is not just beneficial, but critical to unlocking its full potential for application development, management, and deployment. By embracing these foundational best practices—from declarative configurations and robust building blocks to diligent resource management and comprehensive observability—you can significantly optimize performance, enhance security, and confidently navigate your Kubernetes journey.

Kubernetes CloudNative DevOps GenZTech K8sLife TechTips DeveloperLife InfraAsCode CI/CD PlatformEngineering Containers Microservices YamlNotYelling SREGoals SelfHealingApps GitOps