Skip to content

Commit

Permalink
Add k8s files
Browse files Browse the repository at this point in the history
  • Loading branch information
karan56625 committed Aug 18, 2024
1 parent 1ab9c0a commit bb0d39e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/webcrawler-client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"webCrawler/config"
)

func main() {
// Define command-line flags
urlFlag := flag.String("url", "", "The URL to crawl")
flag.Parse()

if *urlFlag == "" {
log.Fatal("URL flag is required")
}

webcrawlerHost, exists := os.LookupEnv("WEBCRAWLER_HOST")
if !exists {
fmt.Println("WEBCRAWLER_HOST is not set")
fmt.Println("Using local host as webcrawler host")
webcrawlerHost = "localhost"
}
webcrawlerPort, exists := os.LookupEnv("WEBCRAWLER_PORT")
if !exists {
fmt.Println("WEBCRAWLER_PORT is not set")
fmt.Println("Using default webcrawler port ", config.Port)
webcrawlerPort = config.Port
}

// Define the web crawler service endpoint
endpoint := "http://" + webcrawlerHost + ":" + webcrawlerPort + config.WebCrawlerEndpoint

// Create the request to start crawling
resp, err := http.Get(endpoint + "?url=" + *urlFlag)
if err != nil {
log.Fatalf("Failed to send request: %v", err)
}
defer resp.Body.Close()

// Read and display the response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response: %v", err)
}

fmt.Println("Crawl Result:")
fmt.Println(string(body))
}
14 changes: 14 additions & 0 deletions docker/webcrawler-client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.23-alpine AS builder

WORKDIR /app

COPY . .
RUN go mod download && go build -o webcrawler-client ./cmd/webcrawler-client

FROM alpine:latest

WORKDIR /root/

COPY --from=builder /app/webcrawler-client .

CMD ["./webcrawler-client"]
26 changes: 26 additions & 0 deletions docker/webcrawler/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use the official Golang image to build the web crawler
FROM golang:1.23-alpine AS builder

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy the source code into the container
COPY . .

# Download the Go Modules dependencies
RUN go mod download && go build -o webcrawler ./cmd/webcrawler

# Start a new stage from scratch
FROM alpine:latest

# Set the Current Working Directory inside the container
WORKDIR /usr

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/webcrawler .

# Expose port 8080 to the outside world
EXPOSE 8081

# Command to run the executable
CMD ["./webcrawler"]
42 changes: 42 additions & 0 deletions k8s/webcrawler-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: webcrawler
labels:
app: webcrawler
spec:
replicas: 2
selector:
matchLabels:
app: webcrawler
template:
metadata:
labels:
app: webcrawler
spec:
containers:
- name: webcrawler
image: ghcr.io/karan56625/webcrawler:1.0
ports:
- containerPort: 8081
env:
- name: NUMBER_OF_WORKER
value: "10"
- name: WORKER_QUEUE_LENGTH
value: "20"
livenessProbe:
httpGet:
path: /health
port: 8081
initialDelaySeconds: 10
timeoutSeconds: 5
periodSeconds: 10
failureThreshold: 3
readinessProbe:
httpGet:
path: /readiness
port: 8081
initialDelaySeconds: 10
timeoutSeconds: 5
periodSeconds: 10
failureThreshold: 3
12 changes: 12 additions & 0 deletions k8s/webcrawler-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: webcrawler-service
spec:
selector:
app: webcrawler
ports:
- protocol: TCP
port: 80
targetPort: 8081
type: LoadBalancer

0 comments on commit bb0d39e

Please sign in to comment.