Core Concepts

Pods

Pod is a group of containers that are deployed together on the same host. For single process deployment artifact, we can generally think “pod” as a “container” to accurately understand the concept. Pods operate at one level higher than individual containers.

How to Create a Pod

apiVersion: v1 #K8s API version (mandatory)
kind: Pod #K8s object type. (mandatory)   Note: Rather use Deployment and StatefulSet for Production deployment
metadata:
  name: hello-world #Name of the Pod (mandatory)  This will be displayed via `kubectl get pods`
  labels: #Custom open ended labels/tagging. Note: While defining Service these will be used as selectors 
    app: hello-world
    owner: digital-marketing
    tire: backend
    key1: value1
spec:
  containers:
  - name: hello-world-cntr #Name of the Container inside the Pod
    image: library/hello-world #Docker Image
    resources: #Resources configuration for Docker Image Container
      limits:
        memory: "200Mi"
        cpu: "200m"
      requests:
        memory: "100Mi"
        cpu: "100m"
   

Application’s resource requirements

Commands

Label , Selectors, and Annotations

apiVersion: ..
kind: ..
metadata:
  name: ..
  labels: 
    app: hello-world
    owner: digital-marketing

equality-based . Available in type Service , Deployment , ReplicaSet , Job and DeamonSet

...
spec:
  selector:
    app: hello-world
... 

set-based . Available in type Deployment , ReplicaSet , Job and DeamonSet

...
spec:
  selector:
  matchLabels:
    app: hello-world #Equality based
  matchExpressions: #Set based
    - {key: tier, operator: In, values: [backend]}
    - {key: env, operator: NotIn, values: [dev]}
...

Services

Traffic Flow

A Service Spec sample

apiVersion: v1
kind: Service
metadata:
  name: hello-world-service #Name of the service.
spec:
  type: NodePort #Will discuss this below.  
  selector:
    app: hello-world  #App names from where the ClusterIPs will be pulled 
  ports:
    - protocol: TCP
      port: 80 #Service Port
      targetPort: 9376 #Target Pod's PORT
      nodePort: 30620 #Will discuss this below. Nodes port from where kube-proxy can forward the traffic

ClusterIP

(Default) Exposes the Service on a cluster-internal IP. This makes the Service only reachable from within the cluster via just {servicename} or {servicename}.{namespace}.svc.cluster.local

NodePort

Exposes the Service on each Node’s IP at a static port (the NodePort). Service can be accessed via <NodeIP>:<NodePort> from Outside.

LoadBalancer

Exposes the Service externally using a cloud provider’s Load Balancer.

Traffic Flow

ExternalName

Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record

Configuration Management (ConfigMaps and Secrets)

Define a container environment variable with data from a single ConfigMap/Secret

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
  - name: hello-world-cntr
    ...
    env:
    - name: USERNAME
      valueFrom:
        configMapKeyRef: # For Secrets use secretKeyRef
          name: hello-world-cm # Can be hello-world-secrets
          key: username
    ...

Mount as a Volume and read from it

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
  - name: hello-world-cntr
    ...
    command: [ "/bin/sh", "-c", "ls /etc/config/" ]
    volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: hello-world-cm

Voluments and PersistentVolumeClaims for storage