From bb3fa7f6534face3712c082769c232cae1a373b7 Mon Sep 17 00:00:00 2001 From: Thuc Le Date: Thu, 26 Mar 2020 08:17:57 +0100 Subject: [PATCH] Update example about production --- go/hello-api/Makefile | 5 +- go/hello-api/main.go | 12 ++- .../ingress_nginx_relative_path/README.md | 74 +++++++++++++++++++ .../ingress_nginx_relative_path/default.yaml | 42 +++++++++++ .../ingress_nginx_relative_path/ingress.yaml | 38 ++++++++++ .../ingress_nginx_relative_path/service.yaml | 20 +++++ 6 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 k8s/jobs/ingress_nginx_relative_path/README.md create mode 100644 k8s/jobs/ingress_nginx_relative_path/default.yaml create mode 100644 k8s/jobs/ingress_nginx_relative_path/ingress.yaml create mode 100644 k8s/jobs/ingress_nginx_relative_path/service.yaml diff --git a/go/hello-api/Makefile b/go/hello-api/Makefile index d8591c8..ce5729a 100644 --- a/go/hello-api/Makefile +++ b/go/hello-api/Makefile @@ -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: @@ -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 diff --git a/go/hello-api/main.go b/go/hello-api/main.go index d9f500c..4150457 100644 --- a/go/hello-api/main.go +++ b/go/hello-api/main.go @@ -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")) } diff --git a/k8s/jobs/ingress_nginx_relative_path/README.md b/k8s/jobs/ingress_nginx_relative_path/README.md new file mode 100644 index 0000000..1c6f33d --- /dev/null +++ b/k8s/jobs/ingress_nginx_relative_path/README.md @@ -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 +``` diff --git a/k8s/jobs/ingress_nginx_relative_path/default.yaml b/k8s/jobs/ingress_nginx_relative_path/default.yaml new file mode 100644 index 0000000..ad82198 --- /dev/null +++ b/k8s/jobs/ingress_nginx_relative_path/default.yaml @@ -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 diff --git a/k8s/jobs/ingress_nginx_relative_path/ingress.yaml b/k8s/jobs/ingress_nginx_relative_path/ingress.yaml new file mode 100644 index 0000000..868b789 --- /dev/null +++ b/k8s/jobs/ingress_nginx_relative_path/ingress.yaml @@ -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 diff --git a/k8s/jobs/ingress_nginx_relative_path/service.yaml b/k8s/jobs/ingress_nginx_relative_path/service.yaml new file mode 100644 index 0000000..303e572 --- /dev/null +++ b/k8s/jobs/ingress_nginx_relative_path/service.yaml @@ -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