|
| 1 | + |
| 2 | +# Scaling, Networking, and Automation Notes |
| 3 | + |
| 4 | +## **1. Horizontal vs Vertical Scaling** |
| 5 | + |
| 6 | +### **Understanding Scaling Through a Real-World Analogy** |
| 7 | +- **Scenario**: Imagine a restaurant during a festive season. |
| 8 | + - The restaurant adds tables (e.g., increasing from 4 to 6) to accommodate more guests. |
| 9 | + - **Drawback**: Despite additional seating, delays in service occur due to overloading of the kitchen and staff. |
| 10 | + - **Solution**: |
| 11 | + - Expand physical space by adding new floors or outlets. |
| 12 | + - Hire more staff and upgrade the kitchen. |
| 13 | +- **Relation to DevOps**: |
| 14 | + - This reflects the two primary scaling strategies: |
| 15 | + 1. Adding capacity by expanding resources (Vertical Scaling). |
| 16 | + 2. Adding more units to share the load (Horizontal Scaling). |
| 17 | + |
| 18 | + |
| 19 | +### **Concept of Scaling in DevOps** |
| 20 | +Scaling ensures that applications can handle increased traffic and workload efficiently. |
| 21 | + |
| 22 | +1. **Horizontal Scaling** (Scaling Out): |
| 23 | + - Adds multiple servers to distribute traffic. |
| 24 | + - A load balancer is used to direct incoming requests to these servers. |
| 25 | + - Example: A web application serving thousands of users by distributing requests across multiple instances. |
| 26 | + |
| 27 | +2. **Vertical Scaling** (Scaling Up): |
| 28 | + - Upgrades the existing server’s capacity (e.g., more RAM, CPU). |
| 29 | + - Ideal for monolithic applications that are not designed for distributed environments. |
| 30 | + |
| 31 | + |
| 32 | +### **Comparison Between Horizontal and Vertical Scaling** |
| 33 | + |
| 34 | +| **Aspect** | **Horizontal Scaling** | **Vertical Scaling** | |
| 35 | +|-------------------------|--------------------------------------------------|--------------------------------------------------| |
| 36 | +| **Scaling Method** | Adds multiple servers to share the load | Increases resources (CPU, RAM) on one server | |
| 37 | +| **Limitation** | Complexity in management and synchronization | Physical hardware limits | |
| 38 | +| **Cost** | Higher initial setup cost, scales well | Expensive with diminishing returns | |
| 39 | +| **Use Case** | Distributed apps, high-read volume databases | Smaller apps with balanced read/write needs | |
| 40 | + |
| 41 | + |
| 42 | +### **Traditional vs. Modern Scaling** |
| 43 | +- **Traditional Approach**: |
| 44 | + - Companies managed physical data centers with limited scalability options. |
| 45 | + - Scaling required manual intervention, including migrations to larger hardware. |
| 46 | +- **Modern Approach**: |
| 47 | + - Cloud providers (AWS, GCP) simplify scaling with: |
| 48 | + - Auto-scaling groups for Horizontal Scaling. |
| 49 | + - On-demand instance upgrades for Vertical Scaling. |
| 50 | + - Tools like Docker and Kubernetes enable easy Horizontal Scaling by running applications in containers. |
| 51 | + |
| 52 | + |
| 53 | +- Why are modern databases more suited to Horizontal Scaling? |
| 54 | + - **Answer**: Containers (via Docker and Kubernetes) allow applications to be easily replicated and deployed in distributed environments, ensuring fault tolerance and scalability. |
| 55 | + |
| 56 | +## **2. Load Balancing: Nginx vs. HAProxy** |
| 57 | + |
| 58 | +### **Load Balancer Functionality Overview** |
| 59 | +Load balancers distribute incoming traffic across servers to ensure: |
| 60 | +- High availability. |
| 61 | +- Fault tolerance. |
| 62 | +- Optimized performance. |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +### **Nginx** |
| 67 | +- **Analogy**: Think of Nginx as a multitasking station manager who directs trains to platforms while handling various administrative duties. |
| 68 | +- **Features**: |
| 69 | + - Optimized for HTTP/HTTPS (Layer 7) traffic. |
| 70 | + - Performs basic TCP load balancing but lacks advanced routing capabilities. |
| 71 | + - Efficient for handling low-to-moderate traffic. |
| 72 | +- **Limitations**: |
| 73 | + - Less robust health checks compared to specialized tools. |
| 74 | + - Struggles with large-scale traffic management. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +### **HAProxy** |
| 79 | +- **Analogy**: HAProxy is like a dedicated railway traffic controller, specializing in efficiently routing trains (traffic) during peak times. |
| 80 | +- **Features**: |
| 81 | + - Optimized for TCP (Layer 4) load balancing with advanced algorithms. |
| 82 | + - Supports robust health checks for backend servers. |
| 83 | + - Handles high-traffic scenarios efficiently. |
| 84 | +- **Advantages Over Nginx**: |
| 85 | + - Prioritizes reliability in large-scale setups. |
| 86 | + - Better suited for applications requiring fine-grained traffic control. |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +### **Key Insight** |
| 91 | +Both tools have unique strengths: |
| 92 | +- **Nginx** excels at Layer 7 traffic and web server integration. |
| 93 | +- **HAProxy** is better for Layer 4 traffic and high-demand environments. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## **3. Practical Demonstration: Setting Up HAProxy for MySQL Load Balancing** |
| 98 | + |
| 99 | +### **Overview** |
| 100 | +- **Objective**: Set up HAProxy to distribute MySQL traffic across backend servers. |
| 101 | +- **Setup**: |
| 102 | + - Frontend Server: Runs HAProxy (no MySQL installed). |
| 103 | + - Backend Server: Hosts MySQL database(s). |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +### **Step-by-Step Guide** |
| 108 | + |
| 109 | +#### 1. **Server Setup** |
| 110 | +- Log into two servers: |
| 111 | + - **Frontend Server**: Dedicated for HAProxy installation. |
| 112 | + - **Backend Server**: Running MySQL for database operations. |
| 113 | +- Verify MySQL installation on the backend: |
| 114 | + ```bash |
| 115 | + sudo mysql |
| 116 | + ``` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +#### 2. **Installing HAProxy** |
| 121 | +- Update the frontend server: |
| 122 | + ```bash |
| 123 | + sudo apt update |
| 124 | + sudo apt install haproxy -y |
| 125 | + ``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +#### 3. **Configuring HAProxy** |
| 130 | +- Open HAProxy configuration file: |
| 131 | + ```bash |
| 132 | + sudo nano /etc/haproxy/haproxy.cfg |
| 133 | + ``` |
| 134 | +- Add the following configuration: |
| 135 | + ```haproxy |
| 136 | + frontend mysql_front |
| 137 | + bind *:3306 |
| 138 | + default_backend mysql_servers |
| 139 | +
|
| 140 | + backend mysql_servers |
| 141 | + mode tcp |
| 142 | + option tcplog |
| 143 | + balance roundrobin |
| 144 | + server mysql_backend <backend-server-ip>:3306 check |
| 145 | + ``` |
| 146 | +- **Explanation**: |
| 147 | + - `frontend`: Defines the entry point for MySQL traffic (port 3306). |
| 148 | + - `backend`: Lists MySQL servers with IP addresses for traffic distribution. |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +#### 4. **Creating a MySQL User for HAProxy** |
| 153 | +- Create a user for HAProxy to perform health checks: |
| 154 | + ```sql |
| 155 | + CREATE USER 'haproxy_check'@'%' IDENTIFIED BY 'password'; |
| 156 | + GRANT REPLICATION CLIENT ON *.* TO 'haproxy_check'@'%'; |
| 157 | + FLUSH PRIVILEGES; |
| 158 | + ``` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +#### 5. **Testing Connectivity** |
| 163 | +- Test connectivity between HAProxy and the backend server: |
| 164 | + ```bash |
| 165 | + ping <backend-server-ip> |
| 166 | + telnet <backend-server-ip> 3306 |
| 167 | + ``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +#### 6. **Installing MySQL Client on HAProxy** |
| 172 | +- Install the MySQL client utility: |
| 173 | + ```bash |
| 174 | + sudo apt install mysql-server |
| 175 | + sudo systemctl status mysql |
| 176 | + ``` |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +#### 7. **Testing the Setup** |
| 181 | +- Connect to the MySQL backend through HAProxy: |
| 182 | + ```bash |
| 183 | + mysql -h localhost -u haproxy_check -p |
| 184 | + ``` |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +#### 8. **Adding More Backend Servers** |
| 189 | +- Update the HAProxy configuration to include additional backend servers: |
| 190 | + ```haproxy |
| 191 | + server mysql_backend_2 <new-server-ip>:3306 check |
| 192 | + ``` |
| 193 | +- Create the `haproxy_check` user on each new server. |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +### **Advantages of HAProxy Load Balancing** |
| 198 | +- Distributes load effectively, reducing the risk of server overloading. |
| 199 | +- Simplifies scaling by allowing new servers to be added with minimal changes. |
| 200 | +- Provides robust health checks to monitor backend server availability. |
0 commit comments