From 247c097fe02827ceaf96031ecf661230581fcc56 Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Tue, 7 Dec 2021 22:15:03 +0300 Subject: [PATCH] add sample nginx deployment file --- README.md | 12 ++++++++++++ deployment/varnish/default.vcl | 10 +++++----- deployment/varnish/deployment.yaml | 10 +++++----- deployment/varnish/kustomization.yaml | 2 ++ deployment/varnish/nginx_deployment.yaml | 24 ++++++++++++++++++++++++ deployment/varnish/service.yaml | 5 +++-- 6 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 deployment/varnish/nginx_deployment.yaml diff --git a/README.md b/README.md index c858664..b120b98 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,18 @@ directory: $ kustomize build . | kubectl apply -f - ``` +[deployment/varnish](deployment/varnish) directory will help you to spin up an in-cluster cache solution quickly. It will also deploy a sample +Nginx with 2 replicas. When you make a GET request to the varnish instance with proper header, you will get following response. Notice **X-Cache** +and **X-Cache-Hits** response headers. This indicates that our requests are successfully hitting the varnish: +```shell +$ curl ${WORKER_NODE_IP}:${VARNISH_NODE_PORT} -H "Host: nginx.default.svc" -v +... omitted +Age: 22 +X-Cache: HIT +X-Cache-Hits: 12 +... omitted +``` + ### Standalone You can use binary method to manage standalone Varnish instances, not in Kubernetes. Binary can be downloaded from [Releases](https://github.com/bilalcaliskan/nginx-conf-generator/releases) page. diff --git a/deployment/varnish/default.vcl b/deployment/varnish/default.vcl index f7258a8..999f795 100644 --- a/deployment/varnish/default.vcl +++ b/deployment/varnish/default.vcl @@ -3,8 +3,8 @@ vcl 4.1; import directors; import std; -backend default { - .host = "varnish-cache:80"; +backend nginx { + .host = "nginx.default.svc:80"; } sub vcl_init { @@ -12,7 +12,7 @@ sub vcl_init { # Typically used to initialize VMODs. new vdir = directors.round_robin(); - vdir.add_backend(default); + vdir.add_backend(nginx); } sub vcl_pipe { @@ -224,8 +224,8 @@ sub vcl_recv { # Backend declaration - if (req.http.host == "varnish-cache") { - set req.backend_hint = default; + if (req.http.host == "nginx.default.svc") { + set req.backend_hint = nginx; if (! req.url ~ "/") { return(pass); } diff --git a/deployment/varnish/deployment.yaml b/deployment/varnish/deployment.yaml index a5fae65..04acaa7 100644 --- a/deployment/varnish/deployment.yaml +++ b/deployment/varnish/deployment.yaml @@ -3,21 +3,21 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: varnish-cache + name: varnish namespace: default spec: replicas: 2 selector: matchLabels: - app: varnish-cache + app: varnish template: metadata: - name: varnish-cache + name: varnish labels: - app: varnish-cache + app: varnish spec: containers: - - name: varnish-cache + - name: varnish image: varnish:7.0.1 imagePullPolicy: IfNotPresent env: diff --git a/deployment/varnish/kustomization.yaml b/deployment/varnish/kustomization.yaml index c06bf91..6b1ba06 100644 --- a/deployment/varnish/kustomization.yaml +++ b/deployment/varnish/kustomization.yaml @@ -6,6 +6,8 @@ kind: Kustomization resources: - deployment.yaml - service.yaml + - nginx_deployment.yaml + - nginx_service.yaml configMapGenerator: - name: varnish-config diff --git a/deployment/varnish/nginx_deployment.yaml b/deployment/varnish/nginx_deployment.yaml new file mode 100644 index 0000000..c4e97a3 --- /dev/null +++ b/deployment/varnish/nginx_deployment.yaml @@ -0,0 +1,24 @@ +--- + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx + namespace: default + labels: + app: nginx +spec: + replicas: 2 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/deployment/varnish/service.yaml b/deployment/varnish/service.yaml index b2d8c52..199178f 100644 --- a/deployment/varnish/service.yaml +++ b/deployment/varnish/service.yaml @@ -3,7 +3,7 @@ apiVersion: v1 kind: Service metadata: - name: varnish-cache + name: varnish namespace: default spec: ports: @@ -11,4 +11,5 @@ spec: port: 80 targetPort: 80 selector: - app: varnish-cache + app: varnish + type: NodePort