- A Kubernetes pod is a group of containers, and is the smallest unit that Kubernetes administers.
- Pods have a single IP address that is applied to every container within the pod.
- Pods can have single or multiple containers.
- Containers in a pod share the same resources such as memory and storage.
- Remember, you CANNOT edit specifications of an existing POD other than the below.
- spec.containers[*].image
- spec.initContainers[*].image
- spec.activeDeadlineSeconds
- spec.tolerations
- Edit the pod for changes and a tmp file is created. Delete and Recreate the pod using the tmp file.
- Basics
- Multi-container Pods
- Node Selector
- Resources - Requests and limits
- Static Pods
- Init Containers
show
kubectl get pods
# OR
kubectl get po
show
kubectl run nginx --image=nginx
show
kubectl create namespace my-website
kubectl run mongo --image=mongo --namespace=my-website
show
kubectl create namespace alpha
kubectl run nginx --image=nginx --namespace=alpha
show
kubectl run custom-nginx --image=nginx --port=8080
show
kubectl get pods -o wide
show
kubectl get pods -o name
show
kubectl delete pod nginx
show
kubectl delete pod nginx --namespace=alpha
show
kubectl run nginx-labels --image=nginx --labels=name=nginx,tier=frontend,env=dev,version=v1
OR
cat << EOF > nginx-labels.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
env: dev
name: nginx
tier: frontend
version: v1
name: nginx-labels
spec:
containers:
- image: nginx
name: nginx
EOF
kubectl apply -f nginx-labels.yaml
show
kubectl delete pod nginx-labels --force --grace-period=0
show
kubectl run nginx-file --image=nginx --dry-run=client -o yaml > nginx-file.yaml
kubectl apply -f nginx-file.yaml
show
kubectl run nginx --image=nginx
kubectl get nginx -o yaml > nginx_definition.yaml
show
kubectl run ubuntu-1 --image=ubuntu --command sleep 4800
A web application requires a specific version of redis to be used as a cache. Create a pod with the following characteristics, and leave it running when complete:
- The pod must run in the web namespace.
- The name of the pod should be cache
- Use the redis image with the 3.2 tag
- Expose port 6379
show
kubectl create namespace web
kubectl run cache --image redis:3.2 --port 6379 --namespace web
Refer Multi-container Pods
Create a pod nginx-node-selector
that will be deployed to a Node that has the label accelerator=nvidia-tesla-p100
show
Add the label to a node:
kubectl label nodes node01 accelerator=nvidia-tesla-p100
We can use the 'nodeSelector' property on the Pod YAML:
cat << EOF > nginx-node-selector.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-node-selector
spec:
containers:
- name: nginx-node-selector
image: nginx
nodeSelector: # add this
accelerator: nvidia-tesla-p100 # the selection label
EOF
kubectl apply -f nginx-node-selector.yaml
OR
Use node affinity (https://kubernetes.io/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/#schedule-a-pod-using-required-node-affinity)
cat << EOF > nginx-node-selector.yaml
apiVersion: v1
kind: Pod
metadata:
name: affinity-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: accelerator
operator: In
values:
- nvidia-tesla-p100
containers:
- name: nginx-node-selector
image: nginx
EOF
kubectl apply -f nginx-node-selector.yaml
show
kubectl annotate pod nginx-annotations description-
show
kubectl delete pod nginx-annotations --force
Create an nginx pod name nginx-resources
with requests
cpu=100m,memory=256Mi
and limits
cpu=200m,memory=512Mi
show
kubectl run nginx-resources --image=nginx --restart=Never --requests='cpu=100m,memory=256Mi' --limits='cpu=200m,memory=512Mi'
OR
cat << EOF > nginx-resources.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx-resources
name: nginx-resources
spec:
containers:
- image: nginx
name: nginx-resources
resources:
limits:
cpu: 200m
memory: 512Mi
requests:
cpu: 100m
memory: 256Mi
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
EOF
kubectl apply -f nginx-resources.yaml
Configure the kubelet systemd-managed service, on the node labelled with name=node01, to launch a pod containing a single container of Image httpd named webtool automatically. Any spec files required should be placed in the /etc/kubernetes/manifests directory on the node.
show
ps -ef | grep kubelet
# root 2794 1 3 07:43 ? 00:01:05 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --cgroup-driver=systemd --network-plugin=cni --pod-infra-container-image=k8s.gcr.io/pause:3.2 --resolv-conf=/run/systemd/resolve/resolv.conf
# Check the config file @ /var/lib/kubelet/config.yaml for the staticPodPath property
staticPodPath: /etc/kubernetes/manifests
mkdir /etc/kubernetes/manifests # create the static pod path, if it does not exist.
cat << EOF > webtool.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: webtool
name: webtool
spec:
containers:
- image: httpd
name: webtool
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
EOF
systemctl restart kubelet # if required
kubectl get pods
# NAME READY STATUS RESTARTS AGE
# webtool-node01 1/1 Running 0 11s
Refer Init Containers
rm nginx-labels.yaml nginx-file.yaml nginx_definition.yaml nginx-resources.yaml
kubectl delete pod mongo -n my-website --force --grace-period=0
kubectl delete pod cache -n web --force --grace-period=0
kubectl delete pod nginx -n alpha --force --grace-period=0
kubectl delete namespace alpha web my-website