Skip to content

Latest commit

 

History

History
464 lines (316 loc) · 9.14 KB

pods.md

File metadata and controls

464 lines (316 loc) · 9.14 KB
  • 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


Check number of pods in the default namespace


show

kubectl get pods
# OR
kubectl get po


Create a new pod with name nginx and using the nginx image

show

kubectl run nginx --image=nginx


Create a pod named mongo using image mongo in a new Kubernetes namespace my-website

show

kubectl create namespace my-website
kubectl run mongo --image=mongo --namespace=my-website


Create a new pod with name nginx and using the nginx image in the alpha namespace

show

kubectl create namespace alpha
kubectl run nginx --image=nginx --namespace=alpha


Create a new pod custom-nginx using the nginx image and expose it on container port 8080.

show

kubectl run custom-nginx --image=nginx --port=8080


Check which node the pod is hosted on

show

kubectl get pods -o wide


Get only the pods name

show

kubectl get pods -o name


Delete the pod with the name nginx

show

kubectl delete pod nginx


Delete the pod with the name nginx in the alpha namespace

show

kubectl delete pod nginx --namespace=alpha


Create pod nginx-labels with nginx image and label name=nginx, tier=frontend, env=dev


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


Delete the pod with name nginx-labels with force and no grace period

show

kubectl delete pod nginx-labels --force --grace-period=0


Create a pod with name nginx-file and image nginx using pod defination file

show

kubectl run nginx-file --image=nginx --dry-run=client -o yaml > nginx-file.yaml
kubectl apply -f nginx-file.yaml


Create a nginx pod with name nginx and copy the pod definition file to a nginx_definition.yaml file

show

kubectl run nginx --image=nginx
kubectl get nginx -o yaml > nginx_definition.yaml


Create a ubuntu-1 pod with image ubuntu with command sleep 4800

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


Multi-container Pods


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


Remove the description annotations for pod nginx-annotations

show

kubectl annotate pod nginx-annotations description-


Remove these nginx-annotations pod to have a clean state in your cluster

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

Check the static pod path in the kubelet config file

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

Execute the below on node01

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

Check on controlpanel node

kubectl get pods
# NAME             READY   STATUS    RESTARTS   AGE
# webtool-node01   1/1     Running   0          11s


Init Containers

Refer Init Containers

Clean up


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