Kubernetes Explained: A Deep Dive into Pods, Services, Replicasets, Controllers, Namespaces, Config, and Secrets

Utsav Desai
19 min readApr 7, 2023

--

Some type of Kubernetes environment setup

Kubernetes is a popular container orchestration platform that is used to manage, scale, and deploy containerized applications. To setup a Kubernetes cluster, there are various tools available, including Minikube, Kubeadm, and Kops.

→ Minikube

Minikube is a tool that allows you to run a single-node Kubernetes cluster locally on your computer. It is commonly used for development and testing purposes. Minikube creates a VM on your local machine and installs Kubernetes components in it. You can then use the Kubernetes command-line tool (kubectl) to interact with the cluster.

To set up Minikube, follow these steps:

  1. Install a hypervisor such as VirtualBox or HyperKit.
  2. Install the Minikube binary from the official website.
  3. Start Minikube by running the command minikube start.

Once Minikube is up and running, you can use kubectl to interact with the cluster. For example, you can deploy an application by running the command kubectl create deployment my-app --image=my-image.

→ Kubeadm

Kubeadm is a tool that allows you to set up a Kubernetes cluster on multiple nodes. It automates the process of bootstrapping a cluster by installing Kubernetes components and configuring them for you.

To set up a Kubernetes cluster using Kubeadm, follow these steps:

  1. Install Docker and kubeadm on all the nodes in your cluster.
  2. Initialize the master node by running the command kubeadm init.
  3. Join the worker nodes to the cluster by running the command kubeadm join.

Once the cluster is up and running, you can use kubectl to interact with it. For example, you can deploy an application by running the command kubectl create deployment my-app --image=my-image.

→ Kops

Kops is a tool that allows you to create and manage Kubernetes clusters on cloud platforms such as AWS, Google Cloud, and Azure. It automates the process of setting up a cluster by creating the necessary resources and configuring Kubernetes components.

To set up a Kubernetes cluster using Kops, follow these steps:

  1. Install the Kops binary on your local machine.
  2. Create an S3 bucket to store the cluster configuration by running the command aws s3api create-bucket --bucket=my-bucket --region=my-region.
  3. Create a Kubernetes cluster by running the command kops create cluster --name=my-cluster --state=s3://my-bucket --zones=my-zones.
  4. Update the cluster configuration by running the command kops update cluster --name=my-cluster --state=s3://my-bucket.
  5. Provision the cluster resources by running the command kops update cluster --name=my-cluster --state=s3://my-bucket --yes.
  6. Validate that the cluster is up and running by running the command kops validate cluster --name=my-cluster --state=s3://my-bucket.

Once the cluster is up and running, you can use kubectl to interact with it. For example, you can deploy an application by running the command kubectl create deployment my-app --image=my-image.

Some Of The Key Features in K8s

  1. Pods
  2. Services
  3. Replication Controller
  4. ReplicaSet
  5. Deployment
  6. Namespaces
  7. Config Maps
  8. Secrets

Pods

In Kubernetes, a Pod is the smallest deployable unit that can be scheduled and managed. A Pod is a logical host for one or more containers, which share the same network namespace and can access the same volumes.

Creating a Pod To create a Pod in Kubernetes, you need to define a Pod specification in a YAML file and use kubectl to create the Pod. Here is an example Pod specification:

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx

This specification creates a Pod named my-pod with one container named my-container, which uses the nginx image.

You can create the Pod by running the following command:

kubectl apply -f pod.yaml

Getting a Pod To get a list of all Pods in the current namespace, run the following command:

kubectl get pods

To Get Detailed information about a specific Pod, run the following command:

kubectl describe pod my-pod

This command will show you detailed information about the my-pod Pod, including its status, IP address, and container logs.

Editing a Pod To edit the specification of an existing Pod, you can use the following command:

kubectl edit pod my-pod

This command will open the Pod specification in your default text editor, where you can make changes to the specification. Once you save the changes and close the editor, Kubernetes will update the Pod with the new specification.

Deleting a Pod To delete a Pod, run the following command:

kubectl delete pod my-pod

This command will delete the my-pod Pod and all its associated resources, including its containers and volumes.

These are some of the basic operations you can perform on a Pod in Kubernetes. Pods are a fundamental building block of Kubernetes applications, and mastering their use is key to understanding how to deploy and manage containerized applications in Kubernetes.

Service

In Kubernetes, a Service is an abstraction layer that enables the communication between a set of Pods and other services or end-users. Services provide a stable IP address and DNS name for a set of Pods, allowing clients to access the Pods without having to know their individual IP addresses.

There are several types of Services that can be used to expose and manage access to a set of Pods. These different types of Services provide different levels of functionality and flexibility, depending on the use case.

Here is a brief overview of the different types of Services in Kubernetes:

1. ClusterIP

In Kubernetes, a ClusterIP Service provides a stable IP address and DNS name for a set of Pods within a cluster. The Service is only accessible from within the cluster and is not exposed to the outside world.

Creating a ClusterIP Service To create a ClusterIP Service in Kubernetes, you need to define a Service specification in a YAML file and use kubectl to create the Service. Here is an example Service specification:

apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
type: ClusterIP
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080

This specification creates a ClusterIP Service named my-service that selects Pods with the label app=my-app. The Service exposes port 80 internally within the cluster, and maps it to port 8080 on the Pods.

You can create the Service by running the following command:

kubectl apply -f service.yaml

Getting a ClusterIP Service To get a list of all Services in the current namespace, including ClusterIP Services, run the following command:

kubectl get services

This command will show you a list of all Services in the current namespace, including their names, IP addresses, and port mappings.

To get detailed information about a specific ClusterIP Service, run the following command:

kubectl describe service my-service

This command will show you detailed information about the my-service Service, including its IP address, port mappings, and endpoint information.

Editing a ClusterIP Service To edit the specification of an existing ClusterIP Service, you can use the following command:

kubectl edit service my-service

This command will open the Service specification in your default text editor, where you can make changes to the specification. Once you save the changes and close the editor, Kubernetes will update the Service with the new specification.

Deleting a ClusterIP Service To delete a ClusterIP Service, run the following command:

kubectl delete service my-service

This command will delete the my-service Service and all its associated resources.

These are some of the basic operations you can perform on a ClusterIP Service in Kubernetes. ClusterIP Services are an essential building block for building microservices architectures that require stable internal communication between services.

2. NodePort

In Kubernetes, a NodePort Service exposes a specific port on all Nodes in the cluster, which can be used to access the Service from outside the cluster. The Service is also accessible from within the cluster using the ClusterIP.

Creating a NodePort Service To create a NodePort Service in Kubernetes, you need to define a Service specification in a YAML file and use kubectl to create the Service. Here is an example Service specification:

apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
type: NodePort
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080

This specification creates a NodePort Service named my-service that selects Pods with the label app=my-app. The Service exposes port 80 on all Nodes in the cluster, and maps it to port 8080 on the Pods.

You can create the Service by running the following command:

kubectl apply -f service.yaml

Getting a NodePort Service To get a list of all Services in the current namespace, including NodePort Services, run the following command:

kubectl get services

This command will show you a list of all Services in the current namespace, including their names, IP addresses, and port mappings.

To get detailed information about a specific NodePort Service, run the following command:

kubectl describe service my-service

This command will show you detailed information about the my-service Service, including its IP address, port mappings, and endpoint information.

Editing a NodePort Service To edit the specification of an existing NodePort Service, you can use the following command:

kubectl edit service my-service

This command will open the Service specification in your default text editor, where you can make changes to the specification. Once you save the changes and close the editor, Kubernetes will update the Service with the new specification.

Deleting a NodePort Service To delete a NodePort Service, run the following command:

kubectl delete service my-service

This command will delete the my-service Service and all its associated resources.

These are some of the basic operations you can perform on a NodePort Service in Kubernetes. NodePort Services are a critical building block for building applications that need to be accessible from outside the cluster and expose a specific port on all Nodes in the cluster for this purpose.

3. LoadBalancer

In Kubernetes, a LoadBalancer Service provides a stable IP address and DNS name for a set of Pods in the cluster and also exposes the Service to the external network.

Creating a LoadBalancer Service To create a LoadBalancer Service in Kubernetes, you need to define a Service specification in a YAML file and use kubectl to create the Service. Here is an example Service specification:

apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
type: LoadBalancer
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080

This specification creates a LoadBalancer Service named my-service that selects Pods with the label app=my-app. The Service exposes port 80 externally and maps it to port 8080 on the Pods.

You can create the Service by running the following command:

kubectl apply -f service.yaml

Getting a LoadBalancer Service To get a list of all Services in the current namespace, including LoadBalancer Services, run the following command:

kubectl get services

This command will show you a list of all Services in the current namespace, including their names, IP addresses, and port mappings.

To get detailed information about a specific LoadBalancer Service, run the following command:

kubectl describe service my-service

This command will show you detailed information about the my-service Service, including its IP address, port mappings, and endpoint information.

Editing a LoadBalancer Service To edit the specification of an existing LoadBalancer Service, you can use the following command:

kubectl edit service my-service

This command will open the Service specification in your default text editor, where you can make changes to the specification. Once you save the changes and close the editor, Kubernetes will update the Service with the new specification.

Deleting a LoadBalancer Service To delete a LoadBalancer Service, run the following command:

kubectl delete service my-service

This command will delete the my-service Service and all its associated resources.

These are some of the basic operations you can perform on a LoadBalancer Service in Kubernetes. LoadBalancer Services are useful when you need to expose a Service to an external network, for example, to serve web traffic to clients outside the cluster. However, note that not all Kubernetes clusters support LoadBalancer Services, as they require integration with a cloud provider’s load balancer service.

Replication Controller

In Kubernetes, a ReplicationController (RC) is a Kubernetes resource that ensures that a specified number of replicas of a Pod are running at any given time.

Creating a ReplicationController To create a ReplicationController in Kubernetes, you need to define a ReplicationController specification in a YAML file and use kubectl to create the ReplicationController. Here is an example ReplicationController specification:

apiVersion: v1
kind: ReplicationController
metadata:
name: my-rc
spec:
replicas: 3
selector:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80

This specification creates a ReplicationController named my-rc that ensures that 3 replicas of a Pod are running at any given time. The ReplicationController selects Pods with the label app=my-app, and creates Pods using a Pod specification that defines a single container running the nginx image on port 80.

You can create the ReplicationController by running the following command:

kubectl apply -f rc.yaml

Getting a ReplicationController To get a list of all ReplicationControllers in the current namespace, run the following command:

kubectl get rc

This command will show you a list of all ReplicationControllers in the current namespace, including their names, desired replicas, and current replicas.

To get detailed information about a specific ReplicationController, run the following command:

kubectl describe rc my-rc

This command will show you detailed information about the my-rc ReplicationController, including its Pod template, selector, and current replicas.

Editing a ReplicationController To edit the specification of an existing ReplicationController, you can use the following command:

kubectl edit rc my-rc

This command will open the ReplicationController specification in your default text editor, where you can make changes to the specification. Once you save the changes and close the editor, Kubernetes will update the ReplicationController with the new specification.

Scaling a ReplicationController To scale the number of replicas in a ReplicationController, you can use the following command:

kubectl scale rc my-rc --replicas=5

This command will scale the my-rc ReplicationController to 5 replicas. Kubernetes will create or delete Pods as necessary to reach the desired replica count.

Deleting a ReplicationController To delete a ReplicationController, run the following command:

kubectl delete rc my-rc

This command will delete the my-rc ReplicationController and all its associated resources, including the Pods it managed.

These are some of the basic operations you can perform on a ReplicationController in Kubernetes. ReplicationControllers are useful for ensuring that a specified number of replicas of a Pod are running at any given time, and for enabling easy scaling and management of Pods. However, note that ReplicationControllers is now deprecated in favor of Deployments, which offer more advanced features and functionality.

ReplicaSet

ReplicaSet in Kubernetes is responsible for maintaining a desired number of replicas of a Pod. It is a higher-level abstraction than a Replication Controller, which allows more advanced selection rules and is more scalable.

Creating a ReplicaSet To create a ReplicaSet in Kubernetes, you need to define a ReplicaSet specification in a YAML file and use kubectl to create the ReplicaSet. Here is an example ReplicaSet specification:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-rs
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80

This specification creates a ReplicaSet named my-rs that ensures that 3 replicas of a Pod are running at any given time. The ReplicaSet selects Pods with the label app=my-app, and creates Pods using a Pod specification that defines a single container running the nginx image on port 80.

You can create the ReplicaSet by running the following command:

kubectl apply -f rs.yaml

Getting a ReplicaSet To get a list of ReplicaSets in the current namespace, you can use the kubectl get rs command. To get more detailed information about a specific ReplicaSet, you can use the kubectl describe rs command followed by the name of the ReplicaSet. For example:

kubectl describe rs my-rs

This command will output detailed information about the my-rs ReplicaSet, including its current status and the Pods that it has created.

Editing a ReplicaSet To edit a ReplicaSet, you can use the kubectl edit rs command followed by the name of the ReplicaSet. This will open the ReplicaSet specification in your default text editor, allowing you to make changes to the specification. For example:

kubectl edit rs my-rs

Scaling a ReplicaSet To scale a ReplicaSet, you can use the kubectl scale command followed by the name of the ReplicaSet and the desired number of replicas. For example, to scale the my-rs ReplicaSet to 5 replicas, you can run the following command:

kubectl scale rs my-rs --replicas=5

This will update the ReplicaSet specification to ensure that 5 replicas of the Pod are running at any given time.

Deleting a ReplicaSet To delete a ReplicaSet, you can use the kubectl delete command followed by the name of the ReplicaSet. For example:

kubectl delete rs my-rs

This will delete the my-rs ReplicaSet and all of the Pods that it has created.

These are some of the basic operations you can perform on a ReplicaSet in Kubernetes.

The major difference between a replication controller and replica set is that the rolling-update command works with Replication Controllers, but won’t work with a Replica Set. This is because Replica Sets are meant to be used as the backend for Deployments.

Deployment

In Kubernetes, a Deployment is an object that manages a set of replica Pods. It provides a declarative way to define and manage the lifecycle of Pods and their associated containerized applications.

Creating a Deployment To create a Deployment in Kubernetes, you need to define a Deployment specification in a YAML file and use kubectl to create the Deployment. Here is an example Deployment specification:

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: my-container
image: nginx:1.19
ports:
- containerPort: 80

This specification creates a Deployment named my-deployment that ensures that 3 replicas of a Pod are running at any given time. The Deployment selects Pods with the label app=my-app, and creates Pods using a Pod specification that defines a single container running the nginx image on port 80.

You can create the Deployment by running the following command:

kubectl apply -f deployment.yaml

Getting a Deployment To get a list of Deployments in the current namespace, you can use the kubectl get deployments command. To get more detailed information about a specific Deployment, you can use the kubectl describe deployment command followed by the name of the Deployment. For example:

kubectl describe deployment my-deployment

This command will output detailed information about the my-deployment Deployment, including its current status and the Pods that it has created.

Editing a Deployment To edit a Deployment, you can use the kubectl edit deployment command followed by the name of the Deployment. This will open the Deployment specification in your default text editor, allowing you to make changes to the specification. For example:

kubectl edit deployment my-deployment

Scaling a Deployment To scale a Deployment, you can use the kubectl scale command followed by the name of the Deployment and the desired number of replicas. For example, to scale the my-deployment Deployment to 5 replicas, you can run the following command:

kubectl scale deployment my-deployment --replicas=5

This will update the Deployment specification to ensure that 5 replicas of the Pod are running at any given time.

Updating a Deployment To update a Deployment, you can use the kubectl set image command followed by the name of the Deployment, the name of the container, and the new image tag. For example, to update the my-deployment Deployment to use the nginx image version 1.20, you can run the following command:

kubectl set image deployment/my-deployment my-container=nginx:1.20

This will update the Deployment specification to use the new image tag.

Rolling back a Deployment If a Deployment update causes problems, you can roll back to the previous version using the kubectl rollout undo command. For example, to roll back the my-deployment Deployment to the previous version, you can run the following command:

kubectl rollout undo deployment/my-deployment

This will revert the Deployment to the previous version and ensure that the previous version is running in all replica Pods.

These are some of the basic operations you can perform on a Deployment in Kubernetes.

Namespaces

n Kubernetes, Namespaces are a way to organize and isolate resources within a cluster. A Namespace is a virtual cluster that provides a way to separate different parts of the system into their own groups. By default, Kubernetes uses the “default” namespace, but users can create additional namespaces as needed to organize their resources.

Namespaces can be used to:

  • Provide isolation between different parts of the system
  • Limit resource usage by assigning resource quotas to a specific namespace
  • Provide logical grouping of resources
  • Simplify resource management by reducing clutter

Here are some examples of how to use Namespaces in Kubernetes:

  1. Creating a Namespace: To create a Namespace, you can use the kubectl create namespace command:
kubectl create namespace my-namespace

This will create a new namespace called “my-namespace”. You can verify that the Namespace has been created by running:

kubectl get namespaces

2. Assigning resources to a Namespace: To assign a resource to a specific Namespace, you can use the “namespace” field in the YAML manifest file for that resource. For example, to create a Deployment in the “my-namespace” namespace, you can use the following YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
namespace: my-namespace
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image
ports:
- containerPort: 8080

This will create a Deployment called “my-deployment” in the “my-namespace” namespace.

3. Viewing resources in a Namespace: To view resources in a specific Namespace, you can use the “-n” or “ — namespace” flag with the kubectl command. For example, to view all the Pods in the “my-namespace” namespace, you can run:

kubectl get pods -n my-namespace

4. Editing a Namespace: To edit a Namespace, you can use the kubectl edit namespace command. For example, to add a label to the “my-namespace” namespace, you can run:

kubectl edit namespace my-namespace

This will open the Namespace YAML file in your default editor. You can then add a label to the metadata section of the file.

5. Deleting a Namespace: To delete a Namespace and all the resources in it, you can use the kubectl delete namespace command. For example, to delete the “my-namespace” namespace, you can run:

kubectl delete namespace my-namespace

This will delete the Namespace and all the resources in it, including Deployments, Pods, Services, and ConfigMaps.

6. Scaling resources in a Namespace: To scale resources in a specific Namespace, you can use the “-n” or “ — namespace” flag with the kubectl scale command. For example, to scale a Deployment called “my-deployment” in the “my-namespace” namespace to 5 replicas, you can run:

kubectl scale deployment my-deployment --replicas=5 -n my-namespace

This will scale the Deployment to 5 replicas in the “my-namespace” namespace.

7. Set the Default Namespace: To set the “test” Namespace as the default Namespace, you can run:

kubectl config set-context --current --namespace=test

In summary, Namespaces are a powerful tool for organizing and managing resources in Kubernetes. They allow you to isolate and group resources, limit resource usage, and simplify resource management.

ConfigMaps

In Kubernetes, ConfigMaps are used to store and manage non-confidential configuration data for an application. This includes things like environment variables, configuration files, and command-line arguments.

ConfigMaps can be created and managed through Kubernetes YAML files or through the Kubernetes API using kubectl commands.

Here’s an example of a ConfigMap YAML file:

apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
ENVIRONMENT: production
DATABASE_URL: postgres://user:password@localhost:5432/db

This ConfigMap is named “example-configmap” and contains two key-value pairs of configuration data, one for the environment and one for the database URL.

To create this ConfigMap using kubectl, you would run the following command:

kubectl create configmap example-configmap --from-literal=ENVIRONMENT=production --from-literal=DATABASE_URL=postgres://user:password@localhost:5432/db

This command creates a ConfigMap named “example-configmap” with the same key-value pairs as the YAML file.

To view the ConfigMap, you can use the kubectl get configmaps command:

kubectl get configmaps

This will show you a list of all ConfigMaps in the current namespace, including the “example-configmap” we just created.

You can also describe the ConfigMap to see more information about its contents:

kubectl describe configmap example-configmap

This will show you the key-value pairs contained in the ConfigMap, as well as any other metadata associated with it.

To edit a ConfigMap, you can use the kubectl edit configmap command:

kubectl edit configmap example-configmap

This will open the ConfigMap YAML in your default text editor, allowing you to make changes.

You can also use ConfigMaps to inject configuration data into your application pods. This is typically done using environment variables or configuration files. Here’s an example of a pod YAML file that uses a ConfigMap to set environment variables:

apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx
env:
- name: ENVIRONMENT
valueFrom:
configMapKeyRef:
name: example-configmap
key: ENVIRONMENT
- name: DATABASE_URL
valueFrom:
configMapKeyRef:
name: example-configmap
key: DATABASE_URL

This pod is named “example-pod” and contains one container that runs the nginx image. The env section of the pod YAML sets two environment variables, one for the environment and one for the database URL. These values are retrieved from the "example-configmap" ConfigMap using the configMapKeyRef field.

To apply this pod YAML, you would run the following command:

kubectl apply -f example-pod.yaml

This will create the pod and inject the environment variables from the ConfigMap.

Overall, ConfigMaps are a powerful tool for managing configuration data in Kubernetes applications, allowing you to easily store, manage, and inject configuration data into your application pods.

Secrets

In Kubernetes, Secrets are a way to securely store and manage sensitive information such as passwords, API keys, and other confidential data. They are used to store data that should not be visible to or accessible by users or processes that do not have appropriate permissions. Secrets are managed as Kubernetes objects, which means they can be created, updated, and deleted using kubectl or other Kubernetes API clients.

Secrets can be used in many different ways in Kubernetes, such as:

  • Providing access credentials for external systems or services.
  • Storing SSL/TLS certificates for secure communication.
  • Providing tokens or other sensitive information to running pods.

To create a Secret in Kubernetes, you need to define the sensitive data in a YAML or JSON file, encode it using base64, and create the Secret object using kubectl. Here is an example of a Secret object:

apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
username: dXNlcm5hbWU=
password: cGFzc3dvcmQ=

In this example, we have created a Secret named “my-secret” that contains two key-value pairs: “username” and “password”. The values are base64-encoded versions of the actual sensitive data.

Create a Secret from a YAML file:

kubectl create -f secret.yaml

Get a list of Secrets:

kubectl get secrets

Describe a specific Secret:

kubectl describe secret my-secret

Edit a Secret:

kubectl edit secret my-secret

Delete a Secret:

kubectl delete secret my-secret

In addition to creating Secrets manually, you can also use them in Kubernetes Pods and other resources by referencing them in the YAML or JSON file. For example, you can mount a Secret as a volume in a Pod or reference specific keys in the Secret as environment variables. This allows you to securely manage sensitive data in your Kubernetes applications.

--

--

Utsav Desai
Utsav Desai

Written by Utsav Desai

Utsav Desai is a technology enthusiast with an interest in DevOps, App Development, and Web Development.

Responses (1)