In this post, we will see what Kubectl is and some of the most commonly used Kubectl commands.
After an overview of some commands, we will see how to use it for deploying MongoDB and a Mongo-Express on Kubernetes.
But first of all: what Kubeclt is?
“Kubectl is a command-line interface for running commands against Kubernetes clusters.
Whether we are deploying applications, inspecting and managing cluster resources, or troubleshooting issues, Kubectl is an indispensable tool“.
VIEW CLUSTER INFORMATION:
kubectl cluster-info
VIEW NODES:
kubectl get nodes
VIEW PODS:
kubectl get pods
VIEW NAMESPACES:
kubectl get namespaces
VIEW SERVICES:
kubectl get services
DESCRIBE A SERVICE:
kubectl describe service <service name>
VIEW CONFIGMAPS:
kubectl get configmaps
DESCRIBE A CONFIGMAP:
kubectl describe configmap <configmap name>
Now, in order to know other commands that we could use in our Kubernetes projects, we will see how to deploy a MongoDB and a Mongo-Express on Kubernetes, following these steps:
- Create a Namespace
- Create a Secret for MongoDB
- Create a Persistent Volume
- Create a Statefulset for MongoDB
- Create a Service for MongoDB
- Create a Deployment for Mongo-Express
- Create a Service for Mongo-Express
NAMESPACE
It is used to isolate resources.
[namespace.yml]
apiVersion: v1
kind: Namespace
metadata:
name: mongodb-namespace
create Namespace:
kubectl apply -f <yaml file>
delete Namespace:
kubectl delete namespace <namespace>
delete Namespace using YAML file:
kubectl delete -f <yaml file>
For all following commands, if we insert the Namespace in the yaml files, we don’t need to specify every time the Namespace but, in order to avoid problem, I prefer specify it.
Moreover, we could setting a Namespace Context using the command:
kubectl config set-context --current --namespace=<namespace-name>
SECRET
It is used to store sensitive data, such as passwords, OAuth tokens, and SSH keys securely
[mongodb-secret.yml]
apiVersion: v1
kind: Secret
metadata:
name: mongodb-secret # Name of the Secret
namespace: mongodb-namespace # Namespace where the Secret is created
type: Opaque # Type of the Secret, Opaque is a default type
data:
mongodb-username: YWRtaW4= # Base64 encoded value of "admin"
mongodb-password: UGFzczEyMw== # Base64 encoded value of "Pass123"
create and show Secrets:
kubectl apply -f <yaml file> -n <namespace-name>
kubect get secrets -n <namespace>
kubectl describe secret mongodb-secret -n mongodb-namespace
delete Secret:
kubectl delete secret <secret name> -n<namespace-name>
delete Secret using YAML file:
kubectl delete -f <yaml file>
PERSISTENT VOLUME
Persistent Volume (PV) in Kubernetes is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes.
In reality we don’t use directly the PV but we use the Persistent Volume Claim (PVC) because, it abstracts the details of the storage and provides a way for the StatefulSet to request storage without worrying about the specifics of the underlying PV.
Kubernetes handles the binding of the PVC to the appropriate PV.
[persistent-volume.yml]
apiVersion: v1
kind: PersistentVolume
metadata:
name: testk8s-pv # Name of the PersistentVolume
spec:
capacity:
storage: 1Gi # Storage capacity of the PersistentVolume
accessModes:
- ReadWriteOnce # Access mode for the PersistentVolume, allowing read-write by a single node
hostPath:
path: /Users/commander/K8s # Path on the host node's filesystem to store the data
create and show PV:
kubectl apply -f <yaml file> -n <namespace-name>
kubectl get pv -n <namespace>
get details about PV:
kubectl describe pv <PV name>
delete PV:
kubectl delete pv <PV name>
delete PV using YAML file:
kubectl delete -f <yaml file>
[persistent-volume-claim.yml]
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: testk8s-pvc # Name of the PersistentVolumeClaim
namespace: mongodb-namespace # Namespace where the PersistentVolumeClaim is created
spec:
accessModes:
- ReadWriteOnce # Access mode for the PersistentVolumeClaim, allowing read-write by a single node
resources:
requests:
storage: 1Gi # Requested storage size for the PersistentVolumeClaim
create and show PVC:
kubectl apply -f <yaml file> -n <namespace-name>
kubectl get pvc -n <namespace-name>
delete PVC:
kubectl delete pvc <PVC name> -n <namespace-name>
delete PVC using YAML file:
kubectl delete -f <yaml file>
STATEFULSET
A statefulset in Kubernetes is a workload API object used to manage stateful applications. Unlike a Deployment, a StatefulSet maintains a unique identity for each pod. These pods are created from the same spec but are not interchangeable; each has a persistent identifier that it maintains across any rescheduling.
[mongodb.yml]
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongodb-statefulset # Name of the StatefulSet
namespace: mongodb-namespace # Namespace where the StatefulSet is deployed
spec:
serviceName: "mongodb" # Service name to associate with the StatefulSet
replicas: 1 # Number of replicas (MongoDB instances)
selector:
matchLabels:
app: mongodb # Selector to match the MongoDB app
template:
metadata:
labels:
app: mongodb # Labels to identify the pods
spec:
containers:
- name: mongodb # Name of the container
image: mongo # Docker image to use for the MongoDB container
env: # Environment variables for MongoDB initialization
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret # Secret reference for the MongoDB username
key: mongodb-username # Key within the secret for the username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret # Secret reference for the MongoDB password
key: mongodb-password # Key within the secret for the password
ports:
- containerPort: 27017 # Port on which MongoDB listens
volumeMounts:
- name: mongodb-storage # Volume mount for MongoDB data storage
mountPath: /data/db # Path to mount the storage inside the container
volumes:
- name: mongodb-storage # Define a volume for the StatefulSet
persistentVolumeClaim:
claimName: testk8s-pvc # Reference to the PersistentVolumeClaim
create and show a Statefulset:
kubectl apply -f <yaml file> -n <namespace-name>
kubectl get statefulsets -n <namespace-name>
describe Statefulset:
kubectl describe statefulsets <statefulset-name> -n <namespace-name>
delete Statefulset:
kubectl delete statefulset <statefulset-name> -n <namespace-name>
delete Statefulset using YAML file:
kubectl delete -f <yaml file> -n <namespace-name>
SERVICE FOR MONGODB
A service in Kubernetes is an abstraction which defines a logical set of pods and a policy by which to access them. Services enable the external access to the pods by providing load balancing. There are different types of services such as ClusterIP, NodePort, and LoadBalancer.
[mongodb-service.yml]
apiVersion: v1
kind: Service
metadata:
name: mongodb-service # Name of the Service
namespace: mongodb-namespace # Namespace where the Service is deployed
spec:
type: NodePort # Expose the service using NodePort
ports:
- port: 27017 # Port exposed by the Service
targetPort: 27017 # Port on the Pod to which traffic will be forwarded
nodePort: 32000 # External port to expose (choose a port in the range 30000-32767)
selector:
app: mongodb # Selector to match the MongoDB Pods
create and show a Service:
kubectl apply -f <yaml file> -n <namespace-name>
kubectl get services -n <namespace-name>
describe a Service:
kubectl describe services <service name> -n <namespace-name>
delete Service:
kubectl delete services <service name> -n <namespace-name>
delete Service using YAML file:
kubectl delete -f <yaml file> -n <namespace-name>
DEPLOYMENT
A deployment in Kubernetes provides declarative updates to applications. We describe a desired state in a deployment object, and the Deployment Controller changes the actual state to the desired state at a controlled rate. Deployments are used to create and update instances of our application.
[mongodb-ui.yml]
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-ui-deployment # Name of the Deployment
namespace: mongodb-namespace # Namespace where the Deployment is deployed
spec:
replicas: 2 # Number of replicas (mongo-express instances)
selector:
matchLabels:
app: mongodb-ui # Selector to match the mongo-express app
template:
metadata:
labels:
app: mongodb-ui # Labels to identify the pods
spec:
containers:
- name: mongo-express # Name of the container
image: mongo-express # Docker image to use for the mongo-express container
env: # Environment variables for mongo-express configuration
- name: ME_CONFIG_MONGODB_ADMINUSERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret # Secret reference for the MongoDB admin username
key: mongodb-username # Key within the secret for the admin username
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret # Secret reference for the MongoDB admin password
key: mongodb-password # Key within the secret for the admin password
- name: ME_CONFIG_MONGODB_SERVER
value: mongodb-service # Address of the MongoDB server
ports:
- containerPort: 8081 # Port on which mongo-express listens
create and show a Deployment:
kubectl apply -f <yaml file> -n <namespace-name>
kubectl get deployments -n <namespace-name>
describe a Deployment:
kubectl describe deployments <deployment-name> -n <namespace-name>
delete a Deployment:
kubectl delete deployments <deployment-name> -n <namespace-name>
delete a Deployment using YAML file:
kubectl delete -f <yaml file>
SERVICE FOR MONGO-EXPRESS
As for MongoDB, we have to define the Service for Mongo-Express.
[mongodb-ui-service.yml]
apiVersion: v1
kind: Service
metadata:
name: mongodb-ui-service # Name of the Service
namespace: mongodb-namespace # Namespace where the Service is deployed
spec:
type: NodePort # Expose the service using NodePort
ports:
- port: 8081 # Port exposed by the Service
targetPort: 8081 # Port on the Pod to which traffic will be forwarded
nodePort: 32001 # External port to expose (choose a port in the range 30000-32767)
selector:
app: mongodb-ui # Selector to match the MongoDB Express Pods
create Service for Mongo-Express:
kubectl apply -f <yaml file> -n <namespace-name>
We have done and now, before running Mongo-Express, we will check that everything is working fine:
check Pods:
kubectl get pods -n <namespace-name>
read Pod’s log:
kubectl logs <pod-name> -n <namespace-name>
The last command to run, in order to use Mongo-Express is:
minikube service <service-name> -n <namespace-name> --url
that it is used to retrieve the URL for a specific service running in a Minikube cluster.
This command finds the service we need within a specific namespace and constructs a URL that we can use to access the service from our local machine.
Now, if we open a browser and go to 127.0.0.1:51625, we will enter in Mongo-Express: