Skip to content

Core Kubernetes Objects: Deployment, Service, Ingress, ConfigMap, Storage

Interactive Diagram

새 탭 ↗

A Kubernetes cluster splits into the control plane and worker nodes.

  • Control plane: kube-apiserver is the entry point for every request, and declared state is stored in etcd. kube-scheduler decides which node a new Pod goes to, and kube-controller-manager runs controllers such as Deployment and ReplicaSet to bring the actual state in line with the declaration.
  • Worker node: kubelet runs containers through the container runtime (containerd, etc.) according to the Pod spec from the apiserver and reports their status. kube-proxy (or an equivalent provided by the CNI) forwards traffic arriving at a Service to Pods.

In managed Kubernetes (AKS, EKS, GKE), the cloud provider runs the control plane, and users mainly manage worker nodes and the objects on them.

image

From an application’s point of view, this flow is enough to read most deployments. External requests pass through an Ingress to a Service, and the Service spreads traffic across the Pods it selects by label. A Deployment keeps the desired number of Pods, configuration comes from ConfigMaps, and persistent data connects to a PV through a PVC.

PlantUML Diagram

Each object can be created/modified by writing it in YAML format.

apiVersion: apps/v1
kind: [Deployment, StatefulSet, Service, Ingress, etc]
metadata:
name: [name]
labels:
[key]: [value]
...
[key]: [value]
spec:
...

Defines the life cycle of a pod and the pod’s instances.

apiVersion: apps/v1
kind: Deployment
metadata:
name: employees
labels:
app: employees
spec:
replicas: 3 # define the initial number of pod instances
selector:
matchLabels: # specify target pods to manage by key:value
app: employees
strategy: # specify the update method when the container changes: rollingUpdate/Recreate
rollingUpdate:
maxSurge: 1 # number of pods rolled at a time during a rolling update
maxUnavailable: 1 # number of pods killed during a rolling update
type: RollingUpdate # replace multiple pods sequentially
template: # pod definition
metadata:
labels:
app: employees
spec:
containers: # container array info; defines container details
- name: employees # container name
image: nogada/employees # registry uri
imagePullPolicy: IfNotPresent # specify the container image update method
ports:
- containerPort: 8080 # port used by the container
name: tomcat # declaration to reference the port by name from outside
resources: # container resource allocation; HPA computes CPU utilization against requests, so autoscaling needs requests
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 300m
memory: 256Mi
livenessProbe: # defines how to check whether the pod is alive
httpGet:
path: /
port: 8080
initialDelaySeconds: 5
periodSeconds: 60
readinessProbe: # checks whether the pod is ready for traffic; on failure it is removed from Service endpoints (slow starts belong to startupProbe)
httpGet:
path: /
port: 8080
initialDelaySeconds: 30
periodSeconds: 5
volumeMounts: # mount info for using storage
- name: spring-properties
mountPath: /app/config
volumes: # specify the type of mounted storage: PVC(PV), Configmap, Secret
- name: spring-properties
configMap:
name: config

Declaration that exposes a container so that it can communicate with the pod. Pod IPs change dynamically, so they must be connected through a service. Connections take the form client -> service -> pod, and pod -> service -> pod

apiVersion: v1
kind: Service
metadata:
name: employees
spec:
type: ClusterIP # NodePort, ClusterIP(default), LoadBalancer
selector:
app: employees # select target pods via the key value of the pod's metadata.labels.
ports:
- protocol: TCP # protocol TCP/UDP
port: 80 # service port
name: http # name of the port to reference; used by name when referencing the port from ingress, etc.
targetPort: tomcat # specify the pod's port; designate by pod container port or port name

An Ingress is an object that declares HTTP(S) routing rules. What actually receives and handles requests is an Ingress controller (NGINX, Traefik, etc.) installed separately in the cluster; without a controller, creating an Ingress does nothing. It connects backend Services by host (domain) and sub-URL pattern, and can handle TLS termination (offload) with a certificate registered as a Secret.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-wildcard-host
spec:
rules:
- host: "foo.bar.com" # target domain name for the service
http:
paths:
- pathType: Prefix
path: "/bar" # sub-URL for routing distinction
backend:
service:
name: service1
port:
number: 80
- host: "*.foo.com" # target domain name for the service
http:
paths:
- pathType: Prefix
path: "/foo" # sub-URL for routing distinction
backend:
service:
name: service2
port:
number: 80

A Configmap is an Object used to store non-confidential data in key-value pairs. A Pod can consume a Configmap as environment variables, command-line arguments, or configuration files in a volume. Using a Configmap, you can separate environment-specific configuration from the container image, making your application easily portable.

apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
# property-like keys; each key maps to a simple value
player_initial_lives: "3"
ui_properties_file_name: "user-interface.properties"
# file-like keys
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true

Configmap usage example : as environment variables, or mounted as files in a directory (existing paths are replaced) To mount only a specific file into an existing path, specify the file name with subPath. The application-property ConfigMap in the example must contain an application.properties key.

apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
command: ["sleep", "3600"]
env:
# define environment variables
- name: PLAYER_INITIAL_LIVES # note that the ConfigMap key name here
# differs in case.
valueFrom:
configMapKeyRef:
name: game-demo # the ConfigMap for this value.
key: player_initial_lives # the key to fetch.
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
- name: property
mountPath: "/app/config/application.properties"
subPath: application.properties
volumes:
# set the volume at the pod level, then mount it into the container within that pod.
- name: property
configMap:
# provide the name of the ConfigMap to mount.
name: application-property
- name: config
configMap:
# provide the name of the ConfigMap to mount.
name: game-demo
# array of keys from the ConfigMap to create as files
items:
- key: "game.properties"
path: "game.properties"
- key: "user-interface.properties"
path: "user-interface.properties"

ref) kubernetes

  • storage class : provides functionality to connect Block/File systems. Supports the storage Life cycle through CSI (Container Storage Interface)
  • PV(Persistent Volume): a provided storage Instance that manages information such as accessmode (R/W/Once/Many), capacity, and reuse policy
    apiVersion: v1
    kind: PersistentVolume
    metadata:
    name: pv0003
    spec:
    capacity:
    storage: 5Gi
    volumeMode: Filesystem
    accessModes:
    - ReadWriteOnce
    persistentVolumeReclaimPolicy: Retain # Recycle is deprecated; dynamic provisioning usually uses Delete
    storageClassName: slow
    mountOptions:
    - hard
    - nfsvers=4.1
    nfs:
    path: /tmp
    server: 172.17.0.2
  • PVC(Persistent Volume Claim) : maps the pod that uses the storage to the actual PV
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: myclaim
    spec:
    accessModes:
    - ReadWriteOnce
    volumeMode: Filesystem
    resources:
    requests:
    storage: 8Gi
    storageClassName: slow
    selector:
    matchLabels:
    release: "stable"
    matchExpressions:
    - {key: environment, operator: In, values: [dev]}
    This PVC looks for a PV of at least 8Gi labeled release: stable. The example pv0003 has no labels and only 5Gi, so it won’t bind to this PVC. In practice, most clusters use dynamic provisioning by setting a storageClass, and the selector is left empty.
  • Usage example
    apiVersion: v1
    kind: Pod
    metadata:
    name: mypod
    spec:
    containers:
    - name: myfrontend
    image: nginx
    volumeMounts:
    - mountPath: "/var/www/html"
    name: mypd
    volumes:
    - name: mypd
    persistentVolumeClaim:
    claimName: myclaim