Core Concepts of Kubernetes
In this blog, we will discuss the core concepts of Kubernetes, including pods, deployments, replica sets, and namespaces, and how to write basic YAML for each of these concepts.
Pods
A pod is the smallest deployable unit in Kubernetes. It is a logical host for containers and can contain one or more containers. Each pod has a unique IP address and shares the same network namespace. This means that all containers within a pod can communicate with each other using localhost.
To create a pod in Kubernetes, you need to define its specifications in a YAML file. Here’s an example:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: nginx
image: nginx:latest
This YAML file specifies the name of the pod, which is “my-pod”, and the image that it should use, which is “nginx:latest”. You can apply this YAML file using the kubectl apply
command.
Deployments
A deployment is a higher-level abstraction of a pod. It provides a declarative way to manage pods, replica sets, and rolling updates. A deployment ensures that the desired number of replicas of a pod is always running.
To create a deployment in Kubernetes, you need to define its specifications in a YAML file. Here’s an example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:latest
This YAML file specifies the name of the deployment, which is “my-deployment”, the number of replicas, which is “3”, and the selector that matches the labels in the pod template. It also defines the pod template, which specifies the container specifications. You can apply this YAML file using the kubectl apply
command.
Replica Sets
A replica set is responsible for maintaining a set of replica pods. It ensures that a specified number of replicas of a pod is always running. If a pod fails or gets deleted, the replica set creates a new pod to maintain the desired number of replicas.
To create a replica set in Kubernetes, you need to define its specifications in a YAML file. Here’s an example:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:latest
This YAML file specifies the name of the replica set, which is “my-replicaset”, the number of replicas, which is “3”, and the selector that matches the labels in the pod template. It also defines the pod template, which specifies the container specifications. You can apply this YAML file using the kubectl apply
command.
Namespaces
A namespace is a way to divide cluster resources into virtual groups. It provides a way to organize and isolate resources in a cluster. Namespaces can be used to separate different environments, such as production and development.
To create a namespace in Kubernetes, you need to define its specifications in a YAML file. Here’s an example:
apiVersion: v1
kind: Namespace
metadata:
name: example-namespace
Schema of YAML file
apiVersion
specifies the Kubernetes API version being used. In this case, it is the core/v1 version.kind
specifies the type of Kubernetes resource being created. In this case, it is a Namespace resource.metadata
contains information about the resource being created, such as its name and labels.name
specifies the name of the Resource. In the above example, it isexample-namespace
.
You can apply any YAML file using the kubectl apply
command
kubectl apply -f example-namespace.yaml
This will create a new namespace in your Kubernetes cluster with the name example-namespace
. You can verify that the namespace was created by running the following command:
kubectl get namespaces
This will list all of the namespaces in your cluster, including the newly created example-namespace
.
In this post, we have covered the core concepts of Kubernetes, including Pods, Deployments, ReplicaSets, and Namespaces. We have also provided basic YAML examples for each of these concepts. Kubernetes can be a complex system to navigate, but with these foundational concepts in place, you should be well on your way to understanding and utilizing this powerful tool.
Stay tuned for our next post, where we will dive deeper into Kubernetes and explore more advanced concepts and use cases. Thank you for reading!