Kubernetes CKAD — Chhota shortcut — Services, Ingress, ConfigMaps, and Secrets — Part 2

Abhishek Purohit
12 min readMay 7, 2024

--

Let’s break down these Kubernetes concepts — Services, Ingress, ConfigMaps, and Secrets — into easy-to-understand components. I’ll provide simple explanations, sophisticated details, and even some memorable mnemonics and memes to help you remember these crucial concepts.

Services: Your Kubernetes Switchboard Operator

Simple Explanation: Think of a Kubernetes Service as a switchboard operator in an old-timey telephone exchange. Pods in Kubernetes are like offices with telephones, but their phone numbers change frequently (since Pods can be destroyed and recreated by ReplicaSets and Deployments). A Service provides a stable phone number (i.e., a network address) so others in the cluster can always reach the Pods, no matter how often their numbers change.

Sophisticated Detail: Services in Kubernetes abstract the access to a logical set of Pods as a network service. They define a consistent access policy to Pods via a set of selectors that dynamically map to Pods. Services can be internal, allowing communication between different services within the cluster, or they can be external, exposing certain services to the outside world, often through a load balancer.

Examples and Mnemonic: Imagine you have three Pods running Nginx, each with its own IP. Rather than tracking each IP, a Service provides a single access point and routes traffic to the active Pods. A mnemonic could be “SIP on a Service” — like sipping on a drink, a Service offers a smooth, consistent way to access Pods.

Meme Concept: Picture a meme with a switchboard operator frantically connecting calls, captioned: “Kubernetes Services: Always keeping your Pods connected!”

Ingress: The Grand Gatekeeper of Kubernetes

Simple Explanation: Ingress in Kubernetes is like the head bouncer at a fancy club. It checks the rules (routing rules) and decides who gets to talk to whom inside the club (your cluster). It’s the gatekeeper that controls how external traffic reaches the Services inside your cluster.

Sophisticated Detail: Ingress manages external access to the Services within a Kubernetes cluster, typically through HTTP or HTTPS routes. It allows you to define rules for routing external traffic to different Services, enabling URL-based routing, SSL termination, and name-based virtual hosting.

Examples and Mnemonic: If you have a Service that should only be reached at https://example.com/api, the Ingress controls and routes external traffic to this URL to the right Service. Mnemonic: "Ingress Insists on Instructions" - Ingress requires specific rules to allow traffic in, much like instructions needed to get into a VIP section.

Meme Concept: An image of a medieval gatekeeper at a fortress gate, with the caption: “You shall not pass… without the correct URL!”

ConfigMaps and Secrets: The Safekeepers of Kubernetes

Simple Explanation: ConfigMaps and Secrets in Kubernetes are like the filing cabinets and safes in an office. ConfigMaps hold the non-confidential data (like the office Wi-Fi password posted on the wall), while Secrets store confidential data securely (like the keys to the company safe).

Sophisticated Detail: ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. Secrets offer a similar service but are designed to hold sensitive information, such as passwords, OAuth tokens, and ssh keys, providing a higher level of security.

Examples and Mnemonic: Imagine you need to configure an application with a database URL, which is non-sensitive and stored in a ConfigMap. A database password, however, is sensitive and stored in Secrets. Mnemonic: “Secrets are serious, ConfigMaps are casual.”

By understanding these foundational components as parts of a well-oiled machine, one can better appreciate how Kubernetes efficiently manages containerized applications, ensuring smooth operations and security within AKS environments.

Let’s dive into some advanced Terraform examples for managing Kubernetes resources like Services, Ingress, ConfigMaps, and Secrets. These examples will help you understand how to define and deploy these resources in your Azure Kubernetes Service (AKS) environments.

1. Kubernetes Service (LoadBalancer)

This example creates a Kubernetes Service of type LoadBalancer, which exposes the Service externally using a cloud provider’s load balancer.

resource "kubernetes_service" "example_lb" {
metadata {
name = "example-loadbalancer"
}

spec {
selector = {
app = "MyApp"
}

port {
port = 80
target_port = 80
}

type = "LoadBalancer"
}
}

Explanation: This service acts as a front door for your applications to access external traffic. By setting the type to LoadBalancer, AKS automatically creates and manages a cloud load balancer for your app, directing external traffic to the correct Pods.

2. Kubernetes Service (ClusterIP)

This example sets up a default Kubernetes Service type, ClusterIP, which makes a set of Pods reachable internally within the cluster.

resource "kubernetes_service" "example_clusterip" {
metadata {
name = "example-clusterip"
}

spec {
selector = {
app = "MyApp"
}

port {
port = 80
target_port = 80
}

type = "ClusterIP"
}
}

Explanation: ClusterIP is the default Kubernetes Service, providing a unique IP address for internal communications. It’s ideal for services that don’t need to be exposed outside the cluster.

3. Kubernetes Ingress

This example creates an Ingress resource, which manages external access to your services within the cluster, typically via HTTP.

resource "kubernetes_ingress" "example" {
metadata {
name = "example-ingress"
}

spec {
rule {
http {
path {
path = "/testpath"
backend {
service_name = "example-service"
service_port = 80
}
}
}
}
}
}

Explanation: The Ingress controls access to services by defining rules for routing external HTTP requests to the appropriate services based on the request URL.

4. Kubernetes ConfigMap

This example demonstrates how to create a ConfigMap to store non-sensitive configuration data that can be consumed by Pods.

resource "kubernetes_config_map" "example" {
metadata {
name = "example-configmap"
}

data = {
"example.property" = "example-value"
}
}

Explanation: ConfigMaps allow you to separate your configurations from your application code, making your applications easier to configure dynamically and deploy in different environments without changes.

5. Kubernetes Secret

This example shows how to create a Kubernetes Secret to store sensitive data such as passwords or API keys.

resource "kubernetes_secret" "example" {
metadata {
name = "example-secret"
}

data = {
"password" = base64encode("my-super-secret-password")
}
}

Explanation: Secrets are used to hold and manage sensitive information, protecting it from exposure. They are similar to ConfigMaps but are intended for confidential data.

6. Deploying an App with Environment Variables from ConfigMap and Secret

This Terraform script creates a deployment that uses environment variables stored in both a ConfigMap and a Secret.

resource "kubernetes_deployment" "example_app" {
metadata {
name = "example-app"
}

spec {
selector {
match_labels = {
app = "example-app"
}
}

template {
metadata {
labels = {
app = "example-app"
}
}

spec {
container {
name = "app"
image = "my-app-image"

env_from {
config_map_ref {
name = kubernetes_config_map.example.metadata[0].name
}
}

env {
name = "SECRET_PASSWORD"

value_from {
secret_key_ref {
name = kubernetes_secret.example.metadata[0].name
key = "password"
}
}
}
}
}
}
}
}

Explanation: This deployment pulls configuration data from both a ConfigMap and a Secret. It uses the ConfigMap for general settings and the Secret for sensitive data, demonstrating how to separate and manage application configurations securely and efficiently.

These examples provide a deeper insight into using Terraform to manage Kubernetes resources, helping you ensure that your applications are both flexible and secure.

Let’s simplify and differentiate the various Kubernetes Service types through a comprehensive table. This will help you understand when and why to use each type of Service in Kubernetes, specifically within the context of Azure Kubernetes Service (AKS). The table will outline the primary use, network accessibility, and typical use cases for each type of service:

Explanation and Details

  • ClusterIP: Think of it as the private line within your company, suitable for internal communication. It’s secure and secluded, perfect for backend services that need to communicate without exposing themselves to the external environment.
  • NodePort: This can be likened to having a direct line to each department within the company via a specific room number (port). Useful when you need a stable endpoint for specific internal or limited external access tasks.
  • LoadBalancer: Consider this the main customer service line advertised on your website; it’s designed to handle incoming traffic efficiently by distributing the load evenly across your services. Essential for high-availability applications accessible from anywhere.
  • ExternalName: This is like redirecting someone to an external partner; it doesn’t manage traffic itself but tells the system where to find the external resource. It’s a DNS-level redirection, used primarily for integrating with services managed outside Kubernetes.

Sophistication in Simplicity

The table and descriptions above provide a clear, structured view of how different Kubernetes services operate and their optimal use cases, making it easier for one to make informed decisions based on the application requirements and environment settings. This understanding is crucial for effectively deploying and managing services in AKS using Azure best practices.

Let’s delve into Kubernetes ConfigMaps and Secrets by creating a table that highlights their differences, usage, and characteristics. This comparison will aid in understanding the purposes and proper implementations of these resource types within Kubernetes, particularly in an Azure Kubernetes Service (AKS) environment.

Detailed Explanation and Context

  • ConfigMaps: Think of ConfigMaps as your open notebook where you jot down non-sensitive operational settings or configurations that your applications need to run — like a checklist or a reference book. It’s not the place for secrets or personal information. The ease of using ConfigMaps allows your applications to remain dynamic and adaptable without rebuilding your images, thus facilitating easier management and deployment.
  • Secrets: Secrets are like a safe. While the data inside is obfuscated and not plaintext, it’s not foolproof and could be deciphered if intercepted (base64 encoding is not encryption). It’s critical to handle Secrets with care, implementing additional encryption measures if needed, and restrict access using Kubernetes RBAC (Role-Based Access Control) policies to ensure that only authorized Pods and users can access this sensitive information.

Best Practices

For ConfigMaps:

  • Keep it clean: Only store non-sensitive data.
  • Version control: Keep ConfigMaps under version control to track changes and revert if necessary.
  • Dynamic updates: Use ConfigMaps for properties that might change, allowing your apps to adapt without redeployment.

For Secrets:

  • Access control: Always use Kubernetes RBAC to limit who and what can access Secrets.
  • Encrypt at rest: Use tools like Azure Key Vault when possible to manage and encrypt Secrets outside of Kubernetes, providing an extra layer of security.
  • Rotate frequently: Implement policies to rotate Secrets regularly to minimize the impact of potential leaks.

Sophistication in Simplicity

Understanding when and how to use ConfigMaps versus Secrets is fundamental for deploying secure, efficient, and maintainable applications in Kubernetes. Mastering these concepts allows you to design and implement cloud-native solutions that not only meet the operational requirements but also adhere to security best practices, particularly in sensitive or regulated environments. This knowledge is crucial in building robust systems on platforms like Azure AKS, where configuration and security management play critical roles in the overall architecture.

Let’s understand different types of Ingress in Kubernetes, which manage external access to your services. This comparison will be particularly helpful for working with Azure Kubernetes Service (AKS). We’ll look at common Ingress Controllers and their features, presenting the information in a structured table format.

Detailed Explanation and Context

  • NGINX Ingress: Think of it as a Swiss Army knife — versatile and reliable, offering powerful routing capabilities and flexibility through its extensive configuration options. It’s suitable for most use cases and provides detailed control over how traffic is handled, making it a favorite among developers.
  • Traefik: It’s like a smart car that’s easy to park and navigate through city traffic — ideal for environments where rapid changes are common. It’s particularly user-friendly for those new to Kubernetes, offering automatic SSL configuration and a user-friendly dashboard for real-time monitoring.
  • HAProxy Ingress: Consider this as the heavy-duty truck in high-demand scenarios, capable of handling high volumes of traffic without breaking a sweat. It’s especially well-suited for applications where detailed, fine-grained control over traffic is necessary.
  • Azure Application Gateway Ingress Controller (AGIC): This is like having a specialized security guard for your Azure-based applications, integrating tightly with Azure Application Gateway to provide native Kubernetes integration alongside enterprise-grade security features.
  • Kong Ingress: Perfect for managing microservices architectures, especially those with a heavy focus on API management. It’s like a specialist tool in your toolkit, ideal when you need more than just routing — such as authentication, rate-limiting, and other API-related services.

Sophistication in Simplicity

Each type of Ingress Controller offers unique advantages and might be suited for different scenarios based on specific requirements of performance, ease of use, security features, and integration capabilities. Understanding these differences helps in making informed choices that align with your application needs and organizational infrastructure, particularly within an AKS environment. This knowledge is essential for designing efficient and secure access to services deployed in Kubernetes.

A summary of all concepts related with Services, Ingress, ConfigMaps, and Secrets in simple, sophisticated and some witty “the wire” references so that one can understand and remember

Services: The Stringer Bell of Kubernetes

Summary: In Kubernetes, a Service is like Stringer Bell in “The Wire” — it’s the intermediary that manages communications and connections. Just as Stringer organizes operations in a way that the street-level activities can run smoothly without revealing their internals, Kubernetes Services manage how network traffic is directed to the Pods. Services ensure that even if the underlying Pods change or move, the access to these Pods remains consistent and reliable.

Ingress: The Lester Freamon of the Network

Summary: Think of Ingress as Lester Freamon, who’s always a step ahead in setting up the pieces on the board, figuring out how to connect different dots. In Kubernetes, Ingress controls the entry points into the cluster, much like Freamon would manage intelligence to control access to a building or an operation. It manages external access to the services within a cluster, typically via HTTP, routing incoming requests to the right services based on the configured rules.

ConfigMaps: The Bubbles of Configuration

Summary: ConfigMaps in Kubernetes can be likened to Bubbles — always resourceful with information about the streets. ConfigMaps are used to store non-confidential data (like environment configurations and operational parameters) that your applications need to run effectively. Just as Bubbles provides tips that aren’t top-secret but are crucial for everyday operations, ConfigMaps provide essential details that help your Pods operate smoothly in the cluster environment.

Secrets: The Omar Little of Kubernetes

Summary: Secrets are the Omar Little of Kubernetes — they carry valuable, sensitive information that needs to be kept away from prying eyes. Secrets manage sensitive data like passwords, tokens, and keys, ensuring that this data is stored securely and accessed only by those components that need it to perform their operations securely. Just as Omar keeps his moves secret and his plans close to his chest, Kubernetes Secrets keep your sensitive data under wraps, accessible only to those who have the right permissions.

“The Wire” and Kubernetes: Managing the Details

In the world of Kubernetes, managing these components effectively is akin to handling the intricate details of operations in “The Wire.” Each component has a specific role and a specific way of interacting with the system that ensures everything runs as smoothly as possible. From Stringer’s strategic thinking in Services to Lester’s detailed oversight in Ingress setups, and from Bubbles’ indispensable street knowledge in ConfigMaps to Omar’s guarded secrets, understanding these roles helps ensure your Kubernetes cluster runs without a hitch — keeping your applications robust, scalable, and secure.

Remember, just as each character in “The Wire” plays a crucial role in the bigger picture, each of these Kubernetes components plays a vital part in the application’s architecture and operational efficiency.

--

--