In the world of cloud engineering, managing remote servers and deploying web applications are essential skills. Today, I’m sharing the thrilling final day of the 7-Day Bash Scripting Challenge, where we tackle a real-world scenario: securely managing servers and deploying a web app using Docker and Nginx. Here's a simplified breakdown of the process.
The Story
Imagine you’re part of a tech team at CodeCrafters Inc., managing three remote servers: Alpha, Beta, and Gamma. These servers need to communicate securely, and you must deploy a web app across two of them. The challenge? Automate it all using Bash scripts, Docker, and Nginx.
The Mission
Create a Secure Server Network
First, we generate SSH keys to set up passwordless authentication between the servers. This allows the main server (Alpha) to control Beta and Gamma securely.Remote Management with Bash Scripts
Using Bash scripting, we automate tasks like running commands on remote servers and securely transferring files.Web App Deployment with Docker and Nginx
Finally, we containerize a simple web app using Docker and deploy it on the Beta and Gamma servers, using Nginx to serve the application.
Step-by-Step Guide
Step 1: Setup Secure Server Communication
We start by creating passwordless SSH connections between the servers. This removes the need to repeatedly type passwords when running remote commands or transferring files.
- Generate SSH keys on the main server:
ssh-keygen -t rsa
- Share the key with the other servers:
ssh-copy-id user@192.168.1.101
ssh-copy-id user@192.168.1.102
Step 2: Automate Remote Commands
Next, we write a script to remotely execute commands on Beta and Gamma from the Alpha server:
#!/bin/bash
clients=("192.168.1.101" "192.168.1.102")
for client in "${clients[@]}"; do
ssh user@$client "$@"
done
This lets us automate tasks like checking system uptime or installing software.
Step 3: Secure File Transfers
We also need to share files between servers. Another script makes this seamless:
#!/bin/bash
file_to_transfer=$1
clients=("192.168.1.101" "192.168.1.102")
for client in "${clients[@]}"; do
scp $file_to_transfer user@$client:/home/user/
done
You can now transfer your web app files or Docker images with a single command!
Step 4: Build and Containerize the Web App
We create a simple web app and use Docker to package it:
- Write an HTML file:
<h1>Welcome to Bash Blaze Day 7 Challenge!</h1>
- Create a
Dockerfile
:
FROM nginx:alpine
COPY ./app /usr/share/nginx/html
- Build and save the Docker image:
docker build -t webapp:v1 .
docker save webapp:v1 > webapp.tar
Step 5: Deploy the Web App
Transfer the Docker image to Beta and Gamma and run it using this script:
#!/bin/bash
docker load < /home/user/webapp.tar
docker run -d --name webapp -p 80:80 webapp:v1
Once deployed, the web app is live on both servers!
Step 6: Validate
Visit the servers in your browser:
-
http://192.168.1.101
-
http://192.168.1.102
You should see your web app up and running!
Why This Matters
This challenge demonstrates the power of Bash scripting in automating routine cloud engineering tasks. From managing secure server communication to deploying scalable applications, you’ve seen how simple scripts can drive efficiency and reliability in remote server management.
💡 Stay tuned for more updates!
References
- Day 1: Introduction to Bash Scripting
- Day 2: Automating Backups
- Day 3: User Account Management
- Day 4: Process Monitoring & Restarting
- Day 5: Log Analysis
- Day 6: Complex Data Manipulation
Let’s connect and share ideas!
Top comments (0)