Pods
Pods are the smallest/basic deployable units of computing that you can create and manage in Kubernetes.
What is a Pod?
While Kubernetes supports more container runtimes other than just Docker, Docker is the most commonly known runtime, and it helps to describe Pods using some terminology from Docker.
The shared context of a Pod is a set of Linux namespaces, cgroups, and potentially other facets of isolation - the same things that isolate a Docker container. Within a Pod's context, the individual applications may have further sub-isolations applied.
In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes
While Kubernetes supports more container runtimes other than just Docker, Docker is the most commonly known runtime, and it helps to describe Pods using some terminology from Docker.
The shared context of a Pod is a set of Linux namespaces, cgroups, and potentially other facets of isolation - the same things that isolate a Docker container. Within a Pod's context, the individual applications may have further sub-isolations applied.
In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes
Using Pods
Pods that run a single container. The "one-container-per-Pod" model is the most common Kubernetes use case; in this case, you can think of a Pod as a wrapper around a single container; Kubernetes manages Pods rather than managing the containers directly.
A Pod can run multiple containers that need to work together. A Pod can encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. These co-located containers form a single cohesive unit of service—for example, one container serving data stored in a shared volume to the public, while a separate sidecar container refreshes or updates those files.
Pod networking
Each Pod is assigned a unique IP address for each address family. Every container in a Pod shares the network namespace, including the IP address and network ports. Inside a Pod (and only then), the containers that belong to the Pod can communicate with one another using localhost. When containers in a Pod communicate with entities outside the Pod, they must coordinate how they use the shared network resources (such as ports). Within a Pod, containers share an IP address and port space, and can find each other via localhost.
Pod Lifecycle
Pods follow a defined lifecycle:
- starting in the Pending Phase
- moving through Running Phase if at least one of its primary containers starts OK,
- and then through either the Succeeded or Failed Phases depending on whether any container in the Pod terminated in failure.
Pod properties
To switch to the Pod
kubectl exec -it pod1 -- bash
To access the application running in the pod ( consider 192.168.135.2 is IP of the pod)
curl <Pod's IP address>
To find all the properties of the pod in yaml format (even if we define the properties, kubernetes, internally will auto create a looot of properties for the pod, check it out by typing the below command)
kubectl get pods pod1 -o yaml
To find all the properties of the pod in json format
kubectl get pods pod1 -o json
EXAMPLE:
To create a pod with docker image nginx
To create a pod with docker image nginx
Type in this command:
kubectl run nginx-pod1 --image=nginx
OR
follow the first step:
1.# vi nginx-pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod1
labels:
app: nginx
tier: dev
spec:
containers:
- name: nginx-container
image: nginx
nodeName: kworker1
kubectl create -f nginx-pod1.yaml (Create new without update)
kubectl apply -f nginx-pod1.yaml (update / create new)
2.# Display Pods and it's properties
kubectl get pod
kubectl get pod -o wide
kubectl get pod nginx-pod1 -o yaml
kubectl get pod nginx-pod1 -o json
kubectl describe pod nginx-pod1
kubectl describe pod nginx-pod1
3.# To get inside the pod
kubectl exec -it nginx-pod1 -- bash
kubectl exec -it nginx-pod1 -- bash
4.# Create test HTML page
cat <<EOF > /usr/share/nginx/html/test.html
<!DOCTYPE html>
<html>
<head>
<title>Testing..</title>
</head>
<body>
<h1 style="color:rgb(90,70,250);">Hello, DevopsWorld...!</h1>
<h2>Congratulations, you passed :-) </h2>
</body>
</html>
EOF
cat <<EOF > /usr/share/nginx/html/test.html
<!DOCTYPE html>
<html>
<head>
<title>Testing..</title>
</head>
<body>
<h1 style="color:rgb(90,70,250);">Hello, DevopsWorld...!</h1>
<h2>Congratulations, you passed :-) </h2>
</body>
</html>
EOF
exit
5.# Expose PODS using NodePort service
kubectl expose pod nginx-pod1 --type=NodePort --port=80
kubectl expose pod nginx-pod1 --type=NodePort --port=80
80 is when you use the cluster-IP to access the webpage, example (10.105.4.192:80)
30437 is when you use the localhost to access the webpage, example (localhost:30437)
6.# Display Service and find NodePort
kubectl describe svc nginx-pod1
kubectl get svc
kubectl describe svc nginx-pod1
kubectl get svc
7.# Open Web-browser and access webapge using
http://nodeip:nodeport/test.html
http://nodeip:nodeport/test.html
8.# Delete pod & svc
kubectl delete svc nginx-pod
kubectl delete pod nginx-pod
kubectl delete svc nginx-pod
kubectl delete pod nginx-pod
No comments:
Post a Comment