Skip to content

Latest commit

 

History

History
216 lines (167 loc) · 6.38 KB

pod.md

File metadata and controls

216 lines (167 loc) · 6.38 KB

Kubernetes Pod object

Kubernetes Documentation/Concepts/Workloads/Pods

A Pod consists of one or more containers (containers are not managed individually). Inside a Pod we have the same IP address, same access to storage and same namespace. It is the smallest unit we can work with.

Command line examples

# create and run a particular image in a pod (since 1.18, kubectl run is only used to create pods)
kubectl run nginx-demo --image=nginx:1.16 --port=80 -l mylabelkey=mylabelvalue

# create pod and associated service (ClusterIP) on port 80
kubectl run nginx-demo --image=nginx --port=80 --expose

# get all pods of the current namespace with the pod labels
kubectl get pods --show-labels

# get additional information on pods with a label selector
kubectl get pods --selector="run=load-balancer-example" -o=wide

# or without the quotes and multiple entries
k get pods --selector bu=finance,env=prod,tier=frontend

# add a label to a specific pod
kubectl label pods xxxxx-yyyy experimental=true

# get all pods of the current namespace with a given label
kubectl get pods -l experimental=true

Manifest examples

  • Nginx container
apiVersion: v1
kind: Pod
metadata:
  name: nginx-demo
  labels:
    tier: frontend
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
        # just for information (and EXPOSE in a Dockerfile)
        - containerPort: 80
  • Busybox container
apiVersion: v1
kind: Pod
metadata:
  name: busybox-demo
spec:
  containers:
    - name: hello
      image: busybox
      command: ['sh', '-c', 'echo "Hello, Kubernetes!" && sleep 3600']
  restartPolicy: OnFailure
  • The following Pod has two Containers. Each Container has a request of 0.25 cpu and 64MiB (226 bytes) of memory. Each Container has a limit of 0.5 cpu and 128MiB of memory. You can say the Pod has a request of 0.5 cpu and 128 MiB of memory, and a limit of 1 cpu and 256MiB of memory.
apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  containers:
    - name: app
      image: images.my-company.example/app:v4
      resources:
        requests:
          memory: "64Mi"
          cpu: "250m"
        limits:
          memory: "128Mi"
          cpu: "500m"
    - name: log-aggregator
      image: images.my-company.example/log-aggregator:v6
      resources:
        requests:
          memory: "64Mi"
          cpu: "250m"
        limits:
          memory: "128Mi"
          cpu: "500m"

Usecases

Multi-Container Pod

While Pods are often deployed with one application container in each, it may be interesting to have an additional container in a Pod.

Secondary container type Usecase
Ambassador Communicate with outside resources, often outside the cluster. Using a proxy, like Envoy or other, you can embed a proxy instead of using one provided by the cluster. It is helpful if you are unsure of the cluster configuration.
Adapter Modify the data generated by the primary container. In some cases, you may need to modify a datastream for proper use.
Sidecar Help or provide a service not found in the primary application. Logging containers are a common sidecar.

Scheduling and Eviction

  • Attract a Pod to a Node (aka Node Affinity): Assigning Pods to Nodes

    • Attach label to a node
    # add labels to a node
    kubectl label nodes kubernetes-foo-node-1.c.a-robinson.internal disktype=ssd
    
    # list nodes with label information
    kubectl get nodes --show-labels
    • Add a nodeSelector field to the pod configuration
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      labels:
        env: test
    spec:
      containers:
        - name: nginx
          image: nginx
          imagePullPolicy: IfNotPresent
      nodeSelector:
        disktype: ssd
    • Or, use affinity field in PodSpec

    • Or, use nodeName (but not recommended)

  • Repel a Pod to a Nice: Taints and Tolerations

    • Taint a node
    # add a taint on a node
    kubectl taint nodes node1 key1=value1:NoSchedule
    
    # list nodes with taint information
    kubectl get nodes -o json | jq '.items[].spec.taints'
    • Specify toleration in the PodSpec (both will work)
    tolerations:
      - key: "key1"
        operator: "Equal"
        value: "value1"
        effect: "NoSchedule"
    ---
    tolerations:
      - key: "key1"
        operator: "Exists"
        effect: "NoSchedule"

Monitoring, Logging, and Debugging

# execute bash commands in the pod
kubectl exec --stdin --tty <pod-name> -- bash

Inject Data Into Applications

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
  labels:
    purpose: demonstrate-command
spec:
  containers:
    - name: command-demo-container
      image: debian
      command: ["printenv"] # The command run by the container (Docker field: Entrypoint)
      args: ["HOSTNAME", "KUBERNETES_PORT"] # The arguments passed to the command (Docker field: Cmd)
  restartPolicy: OnFailure

Static pods

Static pods are created directly by one Node Kubelet, assuming the yaml definition file is present in the static pod folder (Kubelet service argument or configuration file element). It is used by kubeadm to deploy controlplane components. As DaemonSets, static pods are ignored by the kube-scheduler.

# create a static pod named static-busybox that uses the busybox image and the command sleep 1000
kubectl run --restart=Never --image=busybox static-busybox --dry-run=client -o yaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml

Init Containers

Resources