As explained in the last tutorial, We can deploy the application in kubernetes by creating deployment, services and pods using kubectl commands or using YAML configuration files.
Already we have explained how to create kubernetes deployment, service and pods using kubectl command. In this tutorial, we will explain you how to create Kubernetes YAML for Deployment, Pods and Service.
In the previous posts, already we have explained the below topics. Refer those links to understand this topic from basics.
What is Kubernetes - Learn Kubernetes from Basics
How to Install Kubernetes Cluster with Docker on Linux
Create Kubernetes Deployment, Services & Pods Using Kubectl
What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
How to Run Docker Containers - Explained with Examples
In the previous posts, already we have explained the below topics. Refer those links to understand this topic from basics.
What is Kubernetes - Learn Kubernetes from Basics
How to Install Kubernetes Cluster with Docker on Linux
Create Kubernetes Deployment, Services & Pods Using Kubectl
What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
How to Run Docker Containers - Explained with Examples
How to Create Kubernetes YAML for Deployment, Service & Pods
Using YAML helps us to create more complex structures and apply it immediately on the fly. YAML files can be added to version control system to track the file changes. You can create number of yaml files for each application deployments, services, pods and so on.
Let's use the same example what we have used in the earlier tutorial i.e., to create a deployment for httpd web server, It should run a single container within a pod using a docker image "httpd" and it should listen on port 80. Then finally create a service yaml file to expose the port externally so that users will be able to access it.
Also You can Watch this Tutorial video on our YouTube Channel - Create Kubernetes YAML for Deployment, Service & Pods.
Create Kubernetes YAML for Deployment
Create a normal file with yaml extension and add some properties as below. Already, I have created a basic deployment file with below objects to create a pod with single apache webserver container using httpd image.[root@kubernetes-master ~]# cat httpd-basic-deployment.yaml
kind: Deployment
apiVersion: apps/v1beta1
metadata:
name: my-httpd
spec:
replicas: 1
template:
metadata:
labels:
app: webservers
spec:
containers:
- name: my-httpd-container1
image: httpd
ports:
- containerPort: 80
We must start with,
kind - It specfies that, what are you going to achieve with this file (deployment, pod, service, secret, jobs, replication controller, replicaset).
apiVersion - It specifies the api version for the kind. "apps/v1beta1" is the apiVersion for "Deployment" kind. To know more about these apiVersion and kind details, refer this link here.
metadata - Information about the kind specified. Here, we defined only "name" key and its value, so your deployment name will be created as "my-httpd".
spec - Actual specification starts from here with number of replicas, template to be used for entire pods and so on.
replicas - Number of replicas will be created and distributed across the available worker nodes.
template - Template to be used for entire pods. Templates are simply definitions of objects to be replicated
metadata - This metadata only for pods. It can be used to filter or identify by the name given within labels when you have lot of pods.
spec - This specification used for containers including the name of the container, name of the image, ports to be exposed, storage volumes and etc,.
Apply the YAML for kubernetes deployment.
Use "kubectl apply" command to apply the yaml configuration file.[root@kubernetes-master ~]# kubectl apply -f httpd-basic-deployment.yml
deployment.apps/my-httpd created
[root@kubernetes-master ~]#
Check the status of the deployment and pods to ensure the pod is created using yaml file.
[root@kubernetes-master ~]# kubectl get deploy -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
my-httpd 1 1 1 1 2m my-httpd-container1 httpd app=webservers
Above command "kubectl get deploy" output shows some information about our deployment that, it is created with name "my-httpd", name of the containers (my-httpd-container1), name of the images (httpd) and selector which was used with labels (app=webservers).
Let's check the pod status,
[root@kubernetes-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
my-httpd-7bb768779c-m4k56 1/1 Running 0 3m 172.16.1.18 kubernetes-client1.learnitguide.net <none>
[root@kubernetes-master ~]#
Above command "kubectl get pod" output shows that, one pod is running in worker node "kubernetes-client1.learnitguide.net" with IP address "172.16.1.18".
Now Login into worker node "kubernetes-client1.learnitguide.net" and use the command "curl" to check the webserver is working locally.
[root@kubernetes-master ~]# ssh kubernetes-client1.learnitguide.net
root@kubernetes-client1.learnitguide.net's password:
Last login: Wed Aug 15 13:16:16 2018 from 192.168.2.99
[root@kubernetes-client1 ~]# curl 172.16.1.18:80
<html><body><h1>It works!</h1></body></html>
[root@kubernetes-client1 ~]# exit
logout
Connection to kubernetes-client1.learnitguide.net closed.
[root@kubernetes-master ~]#
As expected, we are able to access the webserver locally. Let's create a service yaml file, so that it will expose the port "80" to the outside world and mapped the port to the container port (target port).
Create Kubernetes YAML for Service
Service is an endpoint that exposes the ports to the outside world and mapped the port to the container port (target port). Already, I have created a basic service yaml file with below objects to expose the port.[root@kubernetes-master ~]# cat httpd-basic-service.yaml
kind: Service
apiVersion: v1
metadata:
name: my-httpd-service
spec:
selector:
app: webservers
type: LoadBalancer
ports:
- name: my-apache-port
port: 8080
targetPort: 80
We must start with,
kind - It specfies that, what are you going to achieve with this file (deployment, pod, service, secret, jobs, replication controller, replicaset).
apiVersion - It specifies the api version for the kind. "v1" is the apiVersion for "Service" kind. To know more about these apiVersion and kind details, refer this link here.
metadata - Information about the kind specified. Here, we defined only "name" key and its value, so your service name will be created as "my-httpd-service".
spec - Actual specification starts from here with selector name, type of the service and ports to be exposed with name, port and target port.
selector - We must specify the correct label name here what we have used while creating the deployment. else, it wont work properly.
type - Type of the service (ClusterIP, NodePort, LoadBalancer) - To know more about these type of service, refer this link here.
ports - Specify the name of the service port, port number and target port.
Apply the YAML file for kubernetes service.
Use "kubectl apply" command to apply the yaml configuration file.[root@kubernetes-master ~]# kubectl apply -f httpd-basic-service.yml
service/my-httpd-service created
Check the status of service to ensure the service is created correctly as per the yaml file.
[root@kubernetes-master ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d <none>
my-httpd-service LoadBalancer 10.105.115.116 <pending> 8080:30374/TCP 1m app=webservers
Above command "kubectl get svc" output shows that, service "my-httpd-service" is created with LoadBalancer type, random port number (30374) and assigned to given selector. We know already that, the pod is running on worker node "kubernetes-client1.learnitguide.net". So we will be able to access this using the URL "https://kubernetes-client1.learnitguide.net:30374" from other system if you are in the same network.
[root@kubernetes-master ~]# curl https://kubernetes-client1.learnitguide.net:30374
<html><body><h1>It works!</h1></body></html>
[root@kubernetes-master ~]#
That's it, Hope you have got an idea how to create kubernetes YAML for deployments, services and pods in kubernetes cluster. Going forward we will play more with kubernetes tool.
If you are interested in learning, Request you to go through the below recommended tutorial.
DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online
Keep practicing and have fun. Leave your comments if any.
Stay connected with us on social networking sites, Thank you.
0 تعليقات