Showing posts with label loadbalancer. Show all posts
Showing posts with label loadbalancer. Show all posts

Sunday, March 20, 2022

AWS Compute - ALB (Application Load Balancing)




Application Load Balancer

A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.

Step 1: Create 2 EC2 instances with User Data to install httpd.
User Data script:

#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd

Step 2: Connect to first EC2 instance and run below commands
cd /var/www/html
mkdir orders            (this is case-sensitive! will reflect in other steps)
cd orders
echo "<h1> Orders App </h1> >> index.html

Step 3: Connect to second EC2 instance and run below commands
cd /var/www/html
mkdir payments        (this is case-sensitive! will reflect in other steps)
cd payments
echo "<h1> Payments App </h1> >> index.html

Step 4: Define the target groups which helps to route request to appropriate application.
Select Target Group from Load Balancer Menu and Define below properties

Target Type : Instances
Target Group Name: OrderTG
Protocol HTTP: 80
VPC: default VPC
Protocol Version: HTTP1
Health check Path: orders/index.html

Click on Next button
Register Targets
Select first EC2 instance
Click on Include Pending Below
and Click on Create Target Group

Select Second Target Group from Load Balancer Menu and Define below properties

Target Type : Instances
Target Group Name: PaymentsTG
Protocol HTTP: 80
VPC: default VPC
Protocol Version: HTTP1
Health check Path: payments/index.html

Click on Next button
Register Targets
Select second EC2 instance
Click on Include Pending Below
and Click on Create Target Group

Step 5: Create Application Load Balancer

Click on Create Load balancer Button
Select Application Load Balancer and Click on Create
Load Balancer Name: ALB
Mappings: Select all the AZs
VPC: default VPC
Security Group: WebserverSG
Default Listener : Port: 80 
Default action: OrdersTG

Click on Create Load Balancer Button

Step 6: Load Balancer should be created and it should have a DNS Name by which you are connecting to the applications. Copy the DNS name and put it in the browser, you should be able to see the default apache web page which is implemented in the First EC2 instance (cause default action is OrdersTG).

Step 7: Select Listener tab for ALB and click on view/edit rules link

Click on + symbol on this page and then Insert Rule link
Set below properties for the rule
Path : /orders*
Forward to : OrdersTG
and Click on Save

Click on + symbol on this page and then Insert Rule link
Set below properties for the rule
Path : /payments*
Forward to : PaymentsTG
and Click on Save

Step 8: Go to browser and put <DNS_name_of_ALB>/orders and 
<DNS_name_of_ALB>/payments it should redirect to respective application.


AWS Compute - ELB (Elastic Load Balancing)


Elastic Load Balancing

Elastic Load Balancing automatically distributes your incoming traffic across multiple targets, such as EC2 instances, containers, and IP addresses, in one or more Availability Zones.

It monitors the health of its registered targets and routes traffic only to the healthy targets.


Classic Load Balancer

1. Create 2 EC2 instances with User Data to install httpd.

    In the User Data, put in this script:

#!/bin/bash

sudo yum update -y

sudo yum install -y httpd

sudo systemctl start httpd

sudo systemctl enable httpd

2. Connect to first EC2 instance and run below commands

     echo "<h1>Server 1 </h1>" >> /var/www/html/index.html

3. Connect to second Ec2 instance and run below command

   echo "<h1>Server 2 </h1>" >> /var/www/html/index.html

4. Go to Load Balancing and Select Load Balancers

5. Click on Create Load Balancer and Select Classic Load Balancer and Click on Create button.

6. Define LoadBalancer Properties.

  • Load Balancer Name:  CLB
  • The rest of the properties leave as it is, I am creating in the default VPC and Load Balancer Protocol is HTTP because in my EC2 instances the service is running on Port 80 with HTTP protocol.
  • Click on Next button

7. Select the security Group :- Webserver (Any SG which has port 80 opened) and click on Next

8. No Change on this page, you may see a warning that the application is using insecure protocol HTTP click Next button.

9. Configure Health Check for EC2 instances and Click on Next button

10. Add EC2 instances and Add both EC2 instances and click on Next Button

11. Tags No Change and Review and Create .

12. Once CLB is created then it will have it defined properties, You can access the CLB using its DNS.

13 Use DNS Name on browser and check it will be connected to server1 and server2 frequently.


Sunday, March 13, 2022

Kubernetes Services & Nginx Ingress




SUMMARY:
  1. CLUSTER IP
  2. NODEPORT
  3. LOADBALANCER
  4. INGRESS - NGINX INGRESS CONTROLLER


Kubernetes Services 

An abstract way to expose an application running on a set of Pods as a network service.
With Kubernetes you don't need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them

ClusterIP (internal inbuilt service, basically created when deployment of pods is initiated)
ClusterIP is the default kubernetes service. This service is created inside a cluster and can only be accessed by other pods in that cluster. So basically we use this type of service when we want to expose a service to other pods within the same defined cluster (using ClusterIP service).


Nodeport (external service)
NodePort opens a specific port on your node/VM and when that port gets traffic, that traffic is forwarded directly to the service.

There are a few limitations (so it is not advised to use NodePort):
- only one service per port
- You can only use ports 30000-32767

LoadBalancer (internal service)
This is the standard way to expose service to the internet. All the traffic on the port is forwarded to the service. It's designed to assign an external IP to act as a load balancer for the service. There's no filtering, no routing. LoadBalancer uses cloud service.

Few limitations with LoadBalancer:
- every service exposed will acquire it's own IP address
- It gets very expensive



1. Deployment & NodePort service manifest file

Deployment YAML file:

# Deployment
# nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.7.9
        ports:
        - containerPort: 80


--------------------------------------

NodePort Service YAML file:

# Service
# nginx-svc-np.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: NodePort
  ports:
  - nodePort: 31000         
    #clusterIP (THIS ONE NEED TO CHECK ON YOUR VAGRANTFILE CONFIG WHAT GUEST PORT)
    port: 80                    
    #service port
    targetPort: 80        
    #container port



*******************************************************************

2. Create and Display Deployment and NodePort
kubectl create –f nginx-deploy.yaml
kubectl create -f nginx-svc.yaml

kubectl get service -l app=nginx-app
kubectl get po -o wide
kubectl describe svc my-service


*******************************************************************

3. Testing

# To get inside the pod
kubectl exec [Poc-IP] -it /bin/sh

# Create test HTML page
cat <<EOF > /usr/share/nginx/html/test.html
<!DOCTYPE html>
<html>
<head>
<title>Testing..</title>
</head>
<body>
<h1 style="color:rgb(90,70,250);">Hello, NodePort Service...!</h1>
<h2>Congratulations, you passed :-) </h2>
</body>
</html>
EOF

exit

NodePort - Test using Pod IP:
kubectl get po -o wide
curl http://[POD-IP]/test.html

NodePort – Test using Service IP:
kubectl get svc -l app=nginx-app
curl http://[cluster-ip]/test.html

NodePort- Test using Node IP (external IP)
http://nodep-ip:nodePort/test.html

note: node-ip is the external IP address of a node.


*******************************************************************

4. Cleanup
kubectl delete -f nginx-deploy.yaml
kubectl delete -f nginx-svc.yaml
kubectl get deploy
kubectl get svc
kubectl get pods


*******************************************************************

LoadBalancer
1. YAML: Deployment & Load Balancer Service

# Deployment
# nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.7.9
        ports:
        - containerPort: 80


------------------------------------

# Service - LoadBalancer
#lb.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: LoadBalancer
  ports:
  - nodePort: 31000
    port: 80
    targetPort: 80




*******************************************************************

2. Create & Display: Deployment & Load Balancer Service
kubectl create –f nginx-deploy.yaml
kubectl create -f lb.yaml
kubectl get pod -l app=nginx-app
kubectl get deploy -l app=nginx-app
kubectl get service -l app=nginx-app
kubectl describe service my-service


*******************************************************************

3. Testing Load Balancer Service
# To get inside the pod
kubectl exec -it [pod-name] -- /bin/sh
# Create test HTML page
cat <<EOF > /usr/share/nginx/html/test.html
<!DOCTYPE html>
<html>
<head>
<title>Testing..</title>
</head>
<body>
<h1 style="color:rgb(90,70,250);">Hello, Kubernetes...!</h1>
<h2>Load Balancer is working successfully. Congratulations, you passed :-) </h2>
</body>
</html>
EOF

exit


# Test using load-balancer-ip
http://load-balancer-ip
http://load-balancer-ip/test.html

# Testing using nodePort
http://nodeip:nodeport
http://nodeip:nodeport/test.html

*******************************************************************

4. Cleanup
kubectl delete –f nginx-deploy.yaml
kubectl delete -f lb.yaml
kubectl get pod
kubectl get deploy
kubectl get service



Nginx Ingress Controller

Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource. 

      internet 
            | 
     [ Ingress ] 
     --|-----|-- 
     [ Services ]

An Ingress may be configured to give Services externally-reachable URLs, load balance traffic, terminate SSL / TLS, and offer name-based virtual hosting. An Ingress controller is responsible for fulfilling the Ingress, usually with a load balancer, though it may also configure your edge router or additional frontends to help handle the traffic.

download and provision your master with ingress:
  • kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.35.0/deploy/static/provider/baremetal/deploy.yaml
  • kubectl get svc --all-namespaces
Create a nginx container in a pod:
  • kubectl run nginx --image=nginx
  • kubectl expose pod nginx --type=ClusterIP --port=80

create ingress controller
#ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
   nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
     paths:
     - path: /nginx
       backend:
        serviceName: nginx
        servicePort: 80


kubectl create -f ingress.yaml
kubectl get ingress

**********************************************************************************

now create a tomcat container in the pod:
  • kubectl run tomcat --image=tomcat
  • kubectl expose pod nginx --type=ClusterIP --port=8080
update and apply changes to the ingress controller
#ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
   nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
     paths:
     - path: /nginx
       backend:
        serviceName: nginx
        servicePort: 80
     - path: /abc
        backend:
         serviceName: tomcat
         servicePort: 8080

kubectl apply -f ingress.yaml

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...