Kubernetes is a powerful Platform as a Service that enables you to manage a cluster of nodes that run the Linux operating system. Kubernetes can be used to manage a variety of applications, including web applications, database and more.
Details about the Kubernetes components
- API Server – Intract with kubectl command utility, Primary component, Authorization work.
- Controller Manager – Replica controller, Node Controller maintainer the define pods and nodes.
- Scheduler – Matain proper balacing of hardware utilization and assign new pods to sutable workder node as per ranking.
- ECTD – Database its store the data using key and value form format.
- Kubelet – Pod moniotr, Pod creation and deletation.
- Kube-proxy – Pods communication between 2 nodes, Network related rules manages by kube-proxy.
In this post, We will deploy the Nginx web server on Kubernetes.
Step 1: Write a Deployment YAML
To deploy the nginx docker container in K8s, you can use the provided code by creating a deployment YAML file with the name
and paste the following code.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx labels: app: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80
Take a look at the above YaML configuration:
apiVersion: Define the Kubernetes API in order to create objects.
kind: specify the type of object you intend to create.
metadata: define the object name ( including a name string, uniquely identifying the object, the UID, and an optional namespace ).
spec: Define the state of objects.
selector: specify the mapping between deployment and managed pods.
We need to save and exit from text editor, Our deployment YAML is ready Lets deploy the nginx pod on K8s cluster.
Step 2: Create a Deployment
We need to execute the following command to deploy the nginx pods which is container nginx image.
kubectl create -f nginx-deployment.yaml
You will get success message for the nginx deployment on your screen.
We will get a single pod of the Nginx image and, according to the Dockerfile configuration, Nginx will be up and running on port 80.
Step 3: Let’s Verify the Deployment
We need to execute the given command to see pods and ruuning or not.
To Validate the pod.
kubectl get pods
To check deployment.
kubectl get deployment
Step 4: Create a Service
We need to create a
nginx-service.yaml file in order access nginx web server, Create a
nginx-service.yaml file with follwing code.
To create a service.yaml file
Paste the following code.
apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx type: NodePort ports: - protocol: TCP port: 80 targetPort: 80
The Kubernetes service section: K8s services allow network access to the set of pods.
port: Define the application’s port inside the Docker container.
nodePort: The port number to use to connect to the base machine.
protocol: define a network layer type like TCP/UDP, To communicate with the network.
selector: To know and identify the deployment-created objects.
Save and close the text editor. As per our service configuration, we define port 80 to access the nginx web server, which is mapped with a label like
app: nginx, We are using NodePort to access the nginx web server on each node using port 80.
Step 5: Deploy Service
We need to execute the given command to deploy the nginx-service on K8s cluster.
kubectl create -f nginx-service.yaml
We should get a success message on the screen, To check the service status for Nginx, execute the given command.
To get service status.
kubectl get svc
Here we can see that NodePort assigned the ramdon port to access the Nginx web server on each node
Note: If you are using minikube So you need to use the given command to expose the total url to access your kubernets application.
minikube service nginx-service --url
We should get output like this.
With the browser we can get nginx test page like this.
Step 6: Clean Up
Our nginx web server deployment is complete, and we can now delete all of the resources that we deployed a few minutes ago with the following command.
To get nginx service name.
kubectl get svc | grep nginx
To destory the nginx service.
kubectl delete svc nginx-service
To get nginx deployment.
kubectl get deployment | grep nginx
To destory nginx deployment.
kubectl delete deployment nginx
To validate the previous action.
kubectl get all
You should get an output like this from the previous
kubectl command execution.
Click here to get apache deployment process step by step on kubernetes cluster.
We have successfully deploy nginx web server on kubernetes cluster in a simple way, If you still have questions, please post them in the comments section below.