Skip to content

Commit

Permalink
add sample nginx deployment file
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Dec 7, 2021
1 parent 212a6bc commit 247c097
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions deployment/varnish/default.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ vcl 4.1;
import directors;
import std;

backend default {
.host = "varnish-cache:80";
backend nginx {
.host = "nginx.default.svc:80";
}

sub vcl_init {
# Called when VCL is loaded, before any requests pass through it.
# Typically used to initialize VMODs.

new vdir = directors.round_robin();
vdir.add_backend(default);
vdir.add_backend(nginx);
}

sub vcl_pipe {
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions deployment/varnish/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions deployment/varnish/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- nginx_deployment.yaml
- nginx_service.yaml

configMapGenerator:
- name: varnish-config
Expand Down
24 changes: 24 additions & 0 deletions deployment/varnish/nginx_deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions deployment/varnish/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
apiVersion: v1
kind: Service
metadata:
name: varnish-cache
name: varnish
namespace: default
spec:
ports:
- name: "http"
port: 80
targetPort: 80
selector:
app: varnish-cache
app: varnish
type: NodePort

0 comments on commit 247c097

Please sign in to comment.