Skip to content

Commit b6ffd5d

Browse files
authored
Merge branch 'main' into ah-day-87
2 parents 79331cb + 694de15 commit b6ffd5d

39 files changed

Lines changed: 1722 additions & 4 deletions

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"githubPullRequests.ignoredPullRequestBranches": [
3+
"main"
4+
]
5+
}

2022/Days/day34.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Following [Module 09a](https://microsoftlearning.github.io/AZ-104-MicrosoftAzure
179179

180180
- Task 6: Configure and test autoscaling of the Azure web app
181181

182-
This script I am using can be found in (Cloud/05Serverless)
182+
This script I am using can be found in (Cloud/04Serverless)
183183

184184
![](Images/Day34_Cloud36.png)
185185

2023.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ Or contact us via Twitter, my handle is [@MichaelCade1](https://twitter.com/Mich
156156

157157
### Engineering for Day 2 Ops
158158

159-
- [] 👷🏻‍♀️ 84 > [](2023/day84.md)
160-
- [] 👷🏻‍♀️ 85 > [](2023/day85.md)
161-
- [] 👷🏻‍♀️ 86 > [](2023/day86.md)
159+
160+
- [] 👷🏻‍♀️ 84 > [Writing an API - What is an API?](2023/day84.md)
161+
- [] 👷🏻‍♀️ 85 > [Queues, Queue workers and Tasks (Asynchronous architecture)](2023/day85.md)
162+
- [] 👷🏻‍♀️ 86 > [Designing for Resilience, Redundancy and Reliability](2023/day86.md)
162163
- [] 👷🏻‍♀️ 87 > [Zero Downtime Deployments](2023/day87.md)
163164
- [] 👷🏻‍♀️ 88 > [](2023/day88.md)
164165
- [] 👷🏻‍♀️ 89 > [](2023/day89.md)

2023/day2-ops-code/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Getting started
2+
3+
This repo expects you to have a working kubernetes cluster already setup and
4+
available with kubectl
5+
6+
We expect you already have a kubernetes cluster setup and available with kubectl and helm.
7+
8+
I like using (Civo)[https://www.civo.com/] for this as it is easy to setup and run clusters
9+
10+
The code is available in this folder to build/push your own images if you wish - there are no instructions for this.
11+
12+
## Start the Database
13+
```shell
14+
kubectl apply -f database/mysql.yaml
15+
```
16+
17+
18+
## deploy the day1 - sync
19+
```shell
20+
kubectl apply -f synchronous/k8s.yaml
21+
```
22+
23+
Check your logs
24+
```shell
25+
kubectl logs deploy/generator
26+
27+
kubectl logs deploy/requestor
28+
```
29+
30+
## deploy nats
31+
helm repo add nats https://nats-io.github.io/k8s/helm/charts/
32+
helm install my-nats nats/nats
33+
34+
## deploy day 2 - async
35+
```shell
36+
kubectl apply -f async/k8s.yaml
37+
```
38+
39+
Check your logs
40+
```shell
41+
kubectl logs deploy/generator
42+
43+
kubectl logs deploy/requestor
44+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Set the base image to use
2+
FROM golang:1.17-alpine
3+
4+
# Set the working directory inside the container
5+
WORKDIR /app
6+
7+
# Copy the source code into the container
8+
COPY . .
9+
10+
# Build the Go application
11+
RUN go build -o main .
12+
13+
# Expose the port that the application will run on
14+
EXPOSE 8080
15+
16+
# Define the command that will run when the container starts
17+
CMD ["/app/main"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module main
2+
3+
go 1.20
4+
5+
require (
6+
github.com/go-sql-driver/mysql v1.7.0
7+
github.com/nats-io/nats.go v1.24.0
8+
)
9+
10+
require (
11+
github.com/golang/protobuf v1.5.3 // indirect
12+
github.com/nats-io/nats-server/v2 v2.9.15 // indirect
13+
github.com/nats-io/nkeys v0.3.0 // indirect
14+
github.com/nats-io/nuid v1.0.1 // indirect
15+
golang.org/x/crypto v0.6.0 // indirect
16+
google.golang.org/protobuf v1.30.0 // indirect
17+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
2+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
3+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
4+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
5+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
6+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7+
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
8+
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
9+
github.com/nats-io/jwt/v2 v2.3.0 h1:z2mA1a7tIf5ShggOFlR1oBPgd6hGqcDYsISxZByUzdI=
10+
github.com/nats-io/nats-server/v2 v2.9.15 h1:MuwEJheIwpvFgqvbs20W8Ish2azcygjf4Z0liVu2I4c=
11+
github.com/nats-io/nats-server/v2 v2.9.15/go.mod h1:QlCTy115fqpx4KSOPFIxSV7DdI6OxtZsGOL1JLdeRlE=
12+
github.com/nats-io/nats.go v1.24.0 h1:CRiD8L5GOQu/DcfkmgBcTTIQORMwizF+rPk6T0RaHVQ=
13+
github.com/nats-io/nats.go v1.24.0/go.mod h1:dVQF+BK3SzUZpwyzHedXsvH3EO38aVKuOPkkHlv5hXA=
14+
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
15+
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
16+
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
17+
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
18+
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
19+
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
20+
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
21+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
22+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
24+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
25+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
26+
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
27+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
28+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
29+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
30+
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
31+
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
32+
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
_ "github.com/go-sql-driver/mysql"
7+
nats "github.com/nats-io/nats.go"
8+
"math/rand"
9+
"time"
10+
)
11+
12+
func generateAndStoreString() (string, error) {
13+
// Connect to the database
14+
db, err := sql.Open("mysql", "root:password@tcp(mysql:3306)/mysql")
15+
if err != nil {
16+
return "", err
17+
}
18+
defer db.Close()
19+
20+
// Generate a random string
21+
// Define a string of characters to use
22+
characters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
23+
24+
// Generate a random string of length 10
25+
randomString := make([]byte, 64)
26+
for i := range randomString {
27+
randomString[i] = characters[rand.Intn(len(characters))]
28+
}
29+
30+
// Insert the random number into the database
31+
_, err = db.Exec("INSERT INTO generator_async(random_string) VALUES(?)", string(randomString))
32+
if err != nil {
33+
return "", err
34+
}
35+
36+
fmt.Printf("Random string %s has been inserted into the database\n", string(randomString))
37+
return string(randomString), nil
38+
}
39+
40+
func main() {
41+
err := createGeneratordb()
42+
if err != nil {
43+
panic(err.Error())
44+
}
45+
46+
nc, _ := nats.Connect("nats://my-nats:4222")
47+
defer nc.Close()
48+
49+
nc.Subscribe("generator", func(msg *nats.Msg) {
50+
s, err := generateAndStoreString()
51+
if err != nil {
52+
print(err)
53+
}
54+
nc.Publish("generator_reply", []byte(s))
55+
nc.Publish("confirmation", []byte(s))
56+
})
57+
58+
nc.Subscribe("confirmation_reply", func(msg *nats.Msg) {
59+
stringReceived(string(msg.Data))
60+
})
61+
// Subscribe to the queue
62+
// when a message comes in call generateAndStoreString() then put the string on the
63+
// reply queue. also add a message onto the confirmation queue
64+
65+
// subscribe to the confirmation reply queue
66+
// when a message comes in call
67+
68+
for {
69+
time.Sleep(1 * time.Second)
70+
}
71+
72+
}
73+
74+
func createGeneratordb() error {
75+
db, err := sql.Open("mysql", "root:password@tcp(mysql:3306)/mysql")
76+
if err != nil {
77+
return err
78+
}
79+
defer db.Close()
80+
81+
// try to create a table for us
82+
_, err = db.Exec("CREATE TABLE IF NOT EXISTS generator_async(random_string VARCHAR(100), seen BOOLEAN, requested BOOLEAN)")
83+
84+
return err
85+
}
86+
87+
func stringReceived(input string) {
88+
89+
// Connect to the database
90+
db, err := sql.Open("mysql", "root:password@tcp(mysql:3306)/mysql")
91+
if err != nil {
92+
print(err)
93+
}
94+
defer db.Close()
95+
96+
_, err = db.Exec("UPDATE generator_async SET requested = true WHERE random_string = ?", input)
97+
if err != nil {
98+
print(err)
99+
}
100+
}

2023/day2-ops-code/async/k8s.yaml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: requestor
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: requestor
10+
template:
11+
metadata:
12+
labels:
13+
app: requestor
14+
spec:
15+
containers:
16+
- name: requestor
17+
image: heyal/requestor:async
18+
imagePullPolicy: Always
19+
ports:
20+
- containerPort: 8080
21+
---
22+
apiVersion: v1
23+
kind: Service
24+
metadata:
25+
name: requestor-service
26+
spec:
27+
selector:
28+
app: requestor
29+
ports:
30+
- name: http
31+
protocol: TCP
32+
port: 8080
33+
targetPort: 8080
34+
type: ClusterIP
35+
---
36+
apiVersion: apps/v1
37+
kind: Deployment
38+
metadata:
39+
name: generator
40+
spec:
41+
replicas: 1
42+
selector:
43+
matchLabels:
44+
app: generator
45+
template:
46+
metadata:
47+
labels:
48+
app: generator
49+
spec:
50+
containers:
51+
- name: generator
52+
image: heyal/generator:async
53+
imagePullPolicy: Always
54+
ports:
55+
- containerPort: 8080
56+
---
57+
apiVersion: v1
58+
kind: Service
59+
metadata:
60+
name: generator-service
61+
spec:
62+
selector:
63+
app: generator
64+
ports:
65+
- name: http
66+
protocol: TCP
67+
port: 8080
68+
targetPort: 8080
69+
type: ClusterIP
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Set the base image to use
2+
FROM golang:1.17-alpine
3+
4+
# Set the working directory inside the container
5+
WORKDIR /app
6+
7+
# Copy the source code into the container
8+
COPY . .
9+
10+
# Build the Go application
11+
RUN go build -o main .
12+
13+
# Expose the port that the application will run on
14+
EXPOSE 8080
15+
16+
# Define the command that will run when the container starts
17+
CMD ["/app/main"]

0 commit comments

Comments
 (0)