Day 5 - Kubernetes YAML file

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.

# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: leader
tier: backend
spec:
containers:
- name: leader
image: "docker.io/redis:6.0.5"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379

check Pod status as below

shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-leader-deployment.yaml
deployment.apps/redis-leader created
shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-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.

# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: redis-leader
labels:
app: redis
role: leader
tier: backend
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: leader
tier: backend

Check services now as below

shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-leader-service.yaml
service/redis-leader created
shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-leader-58b566dc8b-rtn7b 1/1 Running 0 2m12s
shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 32d
redis-leader ClusterIP 10.96.249.70 6379/TCP 8s
shreeganesh@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.

# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
replicas: 2
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
role: follower
tier: backend
spec:
containers:
- name: follower
image: gcr.io/google_samples/gb-redis-follower:v2
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 6379

Let we apply and check the pods as below

shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-follower-deployment.yaml
deployment.apps/redis-follower created
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 32d
redis-leader ClusterIP 10.96.249.70 6379/TCP 4m3s
shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 5s
redis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 5s
redis-leader-58b566dc8b-rtn7b 1/1 Running 0 6m15s
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 14s
redis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 14s
redis-leader-58b566dc8b-rtn7b 1/1 Running 0 6m24s
shreeganesh@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.

# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: redis-follower
labels:
app: redis
role: follower
tier: backend
spec:
ports:
# the port that this service should serve on
- port: 6379
selector:
app: redis
role: follower
tier: backend

Let we apply service and check the services as below

shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f redis-follower-service.yaml
service/redis-follower created
shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 119s
redis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 119s
redis-leader-58b566dc8b-rtn7b 1/1 Running 0 8m9s
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 32d
redis-follower ClusterIP 10.104.209.252 6379/TCP 5s
redis-leader ClusterIP 10.96.249.70 6379/TCP 6m3s
shreeganesh@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.

# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
app: guestbook
tier: frontend
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v5
env:
- name: GET_HOSTS_FROM
value: "dns"
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 80

check Pod status as below

shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f frontend-deployment.yaml
deployment.apps/frontend created
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 32d
redis-follower ClusterIP 10.104.209.252 6379/TCP 3m25s
redis-leader ClusterIP 10.96.249.70 6379/TCP 9m23s
shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-697bd54cd4-bdx4t 1/1 Running 0 4s
frontend-697bd54cd4-c2685 1/1 Running 0 4s
frontend-697bd54cd4-jtrv4 1/1 Running 0 4s
redis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 5m24s
redis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 5m24s
redis-leader-58b566dc8b-rtn7b 1/1 Running 0 11m
shreeganesh@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.

# SOURCE: https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
# type: LoadBalancer
#type: LoadBalancer
ports:
# the port that this service should serve on
- port: 80
selector:
app: guestbook
tier: frontend

 let we check services

shreeganesh@aim2022:~/Desktop/Docker$ kubectl apply -f frontend-service.yaml
service/frontend created
shreeganesh@aim2022:~/Desktop/Docker$
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-697bd54cd4-bdx4t 1/1 Running 0 2m45s
frontend-697bd54cd4-c2685 1/1 Running 0 2m45s
frontend-697bd54cd4-jtrv4 1/1 Running 0 2m45s
redis-follower-6f6cd6cbdb-bl7sg 1/1 Running 0 8m5s
redis-follower-6f6cd6cbdb-vnswk 1/1 Running 0 8m5s
redis-leader-58b566dc8b-rtn7b 1/1 Running 0 14m
shreeganesh@aim2022:~/Desktop/Docker$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend ClusterIP 10.103.4.168 80/TCP 7s
kubernetes ClusterIP 10.96.0.1 443/TCP 32d
redis-follower ClusterIP 10.104.209.252 6379/TCP 6m12s
redis-leader ClusterIP 10.96.249.70 6379/TCP 12m
shreeganesh@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

Total Pageviews