Skip to content

Commit

Permalink
Update example about production
Browse files Browse the repository at this point in the history
  • Loading branch information
ledongthuc committed Mar 26, 2020
1 parent 966e8c3 commit bb3fa7f
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 4 deletions.
5 changes: 4 additions & 1 deletion go/hello-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ BINARY_NAME=hello-api
DOCKER_USERNAME=ledongthuc
DOCKER_IMG_NAME=$(BINARY_NAME)
DOCKER_VERSION=local
CUSTOM_ROUTES=/custom_url:this is custom;/random.8b3ec8ff2ac96774ff4e6965e9bba307.html:this is random
CUSTOM_ROUTES=/:custom root;/custom_url:this is custom;/random.8b3ec8ff2ac96774ff4e6965e9bba307.html:this is random

all: test build
test:
Expand All @@ -19,3 +19,6 @@ docker-build:
docker build -t $(DOCKER_USERNAME)/$(DOCKER_IMG_NAME):$(DOCKER_VERSION) .
docker-run:
docker run -p 8080:8080 $(DOCKER_USERNAME)/$(BINARY_NAME):$(DOCKER_VERSION) env CUSTOM_ROUTES="$(CUSTOM_ROUTES)" ./app
docker-push: docker-build
docker image tag $(DOCKER_USERNAME)/$(BINARY_NAME):$(DOCKER_VERSION) $(DOCKER_USERNAME)/$(BINARY_NAME):latest;
docker image push $(DOCKER_USERNAME)/$(BINARY_NAME):latest
12 changes: 9 additions & 3 deletions go/hello-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ func main() {
e := echo.New()
customRoutes := getRoutes()

customRoot := false
for index := range customRoutes {
route := customRoutes[index]
if route.path == "/" || route.path == "" {
customRoot = true
}
e.GET(route.path, func(c echo.Context) error {
return c.String(http.StatusOK, route.message)
})
}
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
if !customRoot {
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
}
e.Logger.Fatal(e.Start(":8080"))
}

Expand Down
74 changes: 74 additions & 0 deletions k8s/jobs/ingress_nginx_relative_path/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Service with ingress nginx

1. Install ingress nginx controller

[Install ingress](/k8s/setup/minikube/install_ingress.md)

2. Verify installation

```
kubectl get pods --all-namespaces -l app.kubernetes.io/name=ingress-nginx
```

3. Apply k8s configuration

note: assume you are at root of repository

```
kubectl apply -f ./
```

4. Check exposed ip of ingress

notes: will take litle time to assign IP address

```
kubectl get ingress http-app-ingress
```

Output example:

```
Output:
NAME HOSTS ADDRESS PORTS AGE
http-app-ingress * 192.168.99.101 80 8m18s
```

```
Internet IP
|
[ Ingress ]
--|-----|--
[ Services ]
```

5. Try try try

```
curl -v 192.168.99.101:80/sub-path/
```

Output:
```
* Trying 192.168.99.101...
* TCP_NODELAY set
* Connected to 192.168.99.101 (192.168.99.101) port 80 (#0)
> GET /sub-path/ HTTP/1.1
> Host: 192.168.99.101
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: openresty/1.15.8.2
< Date: Thu, 23 Jan 2020 20:01:08 GMT
< Content-Type: text/plain; charset=utf-8
< Content-Length: 12
< Connection: keep-alive
< X-App-Name: http-echo
< X-App-Version: 0.2.3
<
Hello world
* Connection #0 to host 192.168.99.101 left intact
* Closing connection 0
```
42 changes: 42 additions & 0 deletions k8s/jobs/ingress_nginx_relative_path/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ingress-nginx-relative-path-app1
spec:
selector:
matchLabels:
app: ingress-nginx-relative-path-app1
template:
metadata:
labels:
app: ingress-nginx-relative-path-app1
spec:
containers:
-
env:
- name: CUSTOM_ROUTES
value: "/css/style.css:this is css;/random.8b3ec8ff2ac96774ff4e6965e9bba307.html:this is random;/:this is index file of app1"
image: "ledongthuc/hello-api:latest"
name: ingress-nginx-relative-path-app1
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ingress-nginx-relative-path-app2
spec:
selector:
matchLabels:
app: ingress-nginx-relative-path-app2
template:
metadata:
labels:
app: ingress-nginx-relative-path-app2
spec:
containers:
-
env:
- name: CUSTOM_ROUTES
value: "/api/xyz:this is api;/this is index file of app2"
image: "ledongthuc/hello-api:latest"
name: ingress-nginx-relative-path-app2
38 changes: 38 additions & 0 deletions k8s/jobs/ingress_nginx_relative_path/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-nginx-relative-path-app1
annotations:
kubernetes.io/ingress.class: "nginx" # ingress will scan it
spec:
rules:
- http:
paths:
- path: /css/
backend:
serviceName: ingress-nginx-relative-path-app1
servicePort: 8080
- path: /random*
backend:
serviceName: ingress-nginx-relative-path-app1
servicePort: 8080
- path: /
backend:
serviceName: ingress-nginx-relative-path-app1
servicePort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-nginx-relative-path-app2
annotations:
kubernetes.io/ingress.class: "nginx" # ingress will scan it
spec:
rules:
- http:
paths:
- path: /api/
backend:
serviceName: ingress-nginx-relative-path-app2
servicePort: 8080
20 changes: 20 additions & 0 deletions k8s/jobs/ingress_nginx_relative_path/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx-relative-path-app1
spec:
ports:
- port: 8080
selector:
app: ingress-nginx-relative-path-app1
---
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx-relative-path-app2
spec:
ports:
- port: 8080
selector:
app: ingress-nginx-relative-path-app2

0 comments on commit bb3fa7f

Please sign in to comment.