As we used dockerfile for maintaining and automatic lots of manual task , in the same we we can have Kubernetes YAML file .
Kubernetes YAML file format is a human-readable text-based format that is used to define Kubernetes objects. It is a superset of JSON, which means that any valid JSON file is also a valid YAML file.
YAML files are divided into three sections:
ApiVersion: This section specifies the Kubernetes API version that the object is using.
Kind: This section specifies the type of object that is being defined.
Metadata: This section contains the metadata for the object, such as its name, labels, and namespace.
Spec: This section specifies the desired state of the object.
Here is an example of a Kubernetes YAML file that defines a pod:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx:1.14.2
In this example, the apiVersion field specifies that the object is using the Kubernetes API version v1.
The kind field specifies that the object is a pod.
The metadata field specifies the name and labels for the pod.
The spec field specifies the desired state of the pod, which is to have one container named my-container that is running the nginx:1.14.2 image.
Kubernetes Services
A Kubernetes service is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable a loose coupling between dependent pods.
Services are important because they allow you to:
Expose a set of pods to the outside world.
Load balance traffic across a set of pods.
Make it easy to find and connect to pods.
There are different types of services in Kubernetes:
ClusterIP: A clusterIP service is only accessible from within the Kubernetes cluster.
NodePort: A NodePort service exposes a service via a static port on each node's IP.
LoadBalancer: A LoadBalancer service exposes a service via the cloud provider's load balancer.
ExternalName: An ExternalName service maps a service name to a hostname or IP address.
The type of service you choose will depend on your specific needs.
Here is an example of a Kubernetes service that exposes a set of pods to the outside world:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 80
In this example, the selector field specifies that the service will expose the pods that are labeled with app: my-app. The ports field specifies that the service will expose port 80 on the pods.
You can find more information about the Kubernetes YAML file format in the Kubernetes documentation: https://kubernetes.io/docs/concepts/configuration/overview/.
Here are some of the benefits of using YAML files for Kubernetes configuration:
YAML is a human-readable format, so it is easy to understand and debug.
YAML is a flexible format, so it can be used to define a wide variety of Kubernetes objects.
YAML files can be version controlled, so you can track changes to your configuration.
YAML files can be used with the kubectl command-line tool to create, update, and delete Kubernetes objects.
Once YAML file is ready we need to run APPLY command to execute the content of YAML file as below.
kubectl apply -f=file_name.yaml
Let we deploy a PHP Guestbook application with Redis as below using YAML file.
Step 1=> Start up the Redis Database
The manifest file, included below, specifies a Deployment controller that runs a single replica Redis Pod.
check Pod status as below
shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-leader-deployment.yamldeployment.apps/redis-leader createdshreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl get podsNAME READY STATUS RESTARTS AGEredis-leader-58b566dc8b-rtn7b 1/1 Running 0 41s
Step2= > Creating the Redis leader Service
The guestbook application needs to communicate to the Redis to write its data. You need to apply a Service to proxy the traffic to the Redis Pod. A Service defines a policy to access the Pods.
Check services now as below
shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-leader-service.yamlservice/redis-leader createdshreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl get podsNAME READY STATUS RESTARTS AGEredis-leader-58b566dc8b-rtn7b 1/1 Running 0 2m12sshreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 32dredis-leader ClusterIP 10.96.249.70 6379/TCP 8sshreeganesh@aim2022:~/Desktop/Docker$
Step3=> Set up Redis followers
Although the Redis leader is a single Pod, you can make it highly available and meet traffic demands by adding a few Redis followers, or replicas.
Let we apply and check the pods as below
shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-follower-deployment.yamldeployment.apps/redis-follower createdshreeganesh@aim2022:~/Desktop/Docker$ kubectl get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 32dredis-leader ClusterIP 10.96.249.70 6379/TCP 4m3sshreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl get podsNAME READY STATUS RESTARTS AGEredis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 5sredis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 5sredis-leader-58b566dc8b-rtn7b 1/1 Running 0 6m15sshreeganesh@aim2022:~/Desktop/Docker$ kubectl get podsNAME READY STATUS RESTARTS AGEredis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 14sredis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 14sredis-leader-58b566dc8b-rtn7b 1/1 Running 0 6m24sshreeganesh@aim2022:~/Desktop/Docker$
Step4=> Creating the Redis follower service
The guestbook application needs to communicate with the Redis followers to read data. To make the Redis followers discoverable, you must set up another Service.
Let we apply service and check the services as below
shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-follower-service.yamlservice/redis-follower createdshreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl get podsNAME READY STATUS RESTARTS AGEredis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 119sredis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 119sredis-leader-58b566dc8b-rtn7b 1/1 Running 0 8m9sshreeganesh@aim2022:~/Desktop/Docker$ kubectl get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 32dredis-follower ClusterIP 10.104.209.252 6379/TCP 5sredis-leader ClusterIP 10.96.249.70 6379/TCP 6m3sshreeganesh@aim2022:~/Desktop/Docker$
Step5 => Creating the Guestbook Frontend Deployment
The guestbook app uses a PHP frontend. It is configured to communicate with either the Redis follower or leader Services, depending on whether the request is a read or a write. The frontend exposes a JSON interface, and serves a jQuery-Ajax-based UX.
check Pod status as below
shreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f frontend-deployment.yamldeployment.apps/frontend createdshreeganesh@aim2022:~/Desktop/Docker$ kubectl get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 32dredis-follower ClusterIP 10.104.209.252 6379/TCP 3m25sredis-leader ClusterIP 10.96.249.70 6379/TCP 9m23sshreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl get podsNAME READY STATUS RESTARTS AGEfrontend-697bd54cd4-bdx4t 1/1 Running 0 4sfrontend-697bd54cd4-c2685 1/1 Running 0 4sfrontend-697bd54cd4-jtrv4 1/1 Running 0 4sredis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 5m24sredis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 5m24sredis-leader-58b566dc8b-rtn7b 1/1 Running 0 11mshreeganesh@aim2022:~/Desktop/Docker$
Step6 =>Creating the Frontend Service
If we want guests to be able to access your guestbook, we must configure the frontend Service to be externally visible, so a client can request the Service from outside the Kubernetes cluster.
let we check services
shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f frontend-service.yamlservice/frontend createdshreeganesh@aim2022:~/Desktop/Docker$shreeganesh@aim2022:~/Desktop/Docker$ kubectl get podsNAME READY STATUS RESTARTS AGEfrontend-697bd54cd4-bdx4t 1/1 Running 0 2m45sfrontend-697bd54cd4-c2685 1/1 Running 0 2m45sfrontend-697bd54cd4-jtrv4 1/1 Running 0 2m45sredis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 8m5sredis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 8m5sredis-leader-58b566dc8b-rtn7b 1/1 Running 0 14mshreeganesh@aim2022:~/Desktop/Docker$ kubectl get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEfrontend ClusterIP 10.103.4.168 80/TCP 7skubernetes ClusterIP 10.96.0.1 443/TCP 32dredis-follower ClusterIP 10.104.209.252 6379/TCP 6m12sredis-leader ClusterIP 10.96.249.70 6379/TCP 12mshreeganesh@aim2022:~/Desktop/Docker$
Step7 =>Viewing the Frontend Service via kubectl port-forward
Run the following command to forward port 8080 on your local machine to port 80 on the service.
kubectl port-forward svc/frontend 8080:80
The response should be similar to this:
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
load the page http://localhost:8080 in your browser to view your guestbook
Step8 => Open Minikube dashboard and check pods/Containers
we can see below details
Step9 => Test application
Step10=> Once all testing done we can safely delete all services and pods as below.
kubectl delete deployment -l app=redis
kubectl delete service -l app=redis
kubectl delete deployment frontend
kubectl delete service frontend
No comments:
Post a Comment