Skip to content

Commit 3ca02de

Browse files
kate-osbornKate Osborn
authored and
Kate Osborn
committed
Add Dockerfile for agent (#399)
1 parent e37cb0e commit 3ca02de

File tree

6 files changed

+207
-0
lines changed

6 files changed

+207
-0
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ OUT_DIR=$(shell pwd)/build/.out
1212

1313
.DEFAULT_GOAL := help
1414

15+
AGENT_VERSION ?= 2.22.1
16+
ALPINE_VERSION ?= 3.16
17+
NGINX_WITH_AGENT_PREFIX ?= nginx-with-agent
18+
1519
.PHONY: help
1620
help: Makefile ## Display this help
1721
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "; printf "Usage:\n\n make \033[36m<target>\033[0m\n\nTargets:\n\n"}; {printf " \033[36m%-30s\033[0m %s\n", $$1, $$2}'
@@ -21,6 +25,11 @@ container: build ## Build the container
2125
@docker -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with Docker\n"; exit $$code)
2226
docker build --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE) --target $(TARGET) -f build/Dockerfile -t $(PREFIX):$(TAG) .
2327

28+
.PHONY: nginx-with-agent-container
29+
nginx-with-agent-container: ## Build the nginx-with-agent container
30+
@docker -v || (code=$$?; printf "\033[0;31mError\033[0m: there was a problem with Docker\n"; exit $$code)
31+
docker build --build-arg AGENT_VERSION=$(AGENT_VERSION) --build-arg ALPINE_VERSION=$(ALPINE_VERSION) -f build/nginx-with-agent/Dockerfile -t $(PREFIX)/$(NGINX_WITH_AGENT_PREFIX):$(TAG) .
32+
2433
.PHONY: build
2534
build: ## Build the binary
2635
ifeq (${TARGET},local)

build/nginx-with-agent/Dockerfile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
FROM nginx:1.22.1-alpine
2+
ARG AGENT_VERSION
3+
ARG ALPINE_VERSION
4+
5+
WORKDIR /nginx-with-agent
6+
7+
RUN apk add --no-cache libcap
8+
9+
# For now, get the agent apk package from github release. Eventually, we will pull the pre-build package from nginx.org.
10+
RUN wget -nv -O agent.apk https://github.com/nginx/agent/releases/download/v$AGENT_VERSION/nginx-agent-$AGENT_VERSION-v$ALPINE_VERSION-x86_64.apk \
11+
&& apk add --allow-untrusted agent.apk
12+
13+
# Copy nginx-agent config file and entrypont script.
14+
# We could also mount this to the Pod.
15+
COPY ./build/nginx-with-agent/nginx-agent.conf /etc/nginx-agent/nginx-agent.conf
16+
COPY ./build/nginx-with-agent/entrypoint.sh /nginx-with-agent/entrypoint.sh
17+
18+
# Copy nginx config file and httpmatches njs module.
19+
# We could also mount this to the Pod.
20+
COPY ./internal/nginx/modules/src/httpmatches.js /usr/lib/nginx/modules/njs/httpmatches.js
21+
COPY ./build/nginx-with-agent/nginx.conf /etc/nginx/nginx.conf
22+
23+
# Create nginx directories, clear /conf.d directory, change owner of nginx and agent directories to nginx user 101,
24+
# and make the entrypoint script executable.
25+
RUN mkdir -p /etc/nginx/secrets /var/lib/nginx /var/log/nginx \
26+
&& rm -f /etc/nginx/conf.d/* \
27+
&& chown -R 101:101 /etc/nginx /var/lib/nginx /var/log/nginx /var/cache/nginx \
28+
&& chown -R 101:101 /var/log/nginx-agent /etc/nginx-agent /var/log/nginx-agent /etc/nginx-agent\
29+
&& chmod +x /nginx-with-agent/entrypoint.sh
30+
31+
32+
# The following instructions allow nginx and nginx-debug binaries to bind to privileged ports.
33+
# However, adding this capability prevents the agent from reading nginx's /proc/<pid>/exe symlink which is required by
34+
# the agent to determine the path to the nginx binary. While we wait for a more permanent fix, we will work around this
35+
# by having nginx bind to non-privileged ports. See this write-up for more details: https://dxuuu.xyz/filecaps.html
36+
37+
#RUN setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx 'cap_net_bind_service=+ep' /usr/sbin/nginx-debug
38+
#RUN setcap -v 'cap_net_bind_service=+ep' /usr/sbin/nginx 'cap_net_bind_service=+ep' /usr/sbin/nginx-debug
39+
40+
# Set user to 101 (nginx)
41+
USER 101:101
42+
43+
STOPSIGNAL SIGTERM
44+
45+
EXPOSE 8080 8443
46+
47+
ENTRYPOINT ["/nginx-with-agent/entrypoint.sh"]

build/nginx-with-agent/entrypoint.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -x
5+
set -euxo pipefail
6+
7+
handle_term()
8+
{
9+
echo "received TERM signal"
10+
echo "stopping nginx-agent ..."
11+
kill -TERM "${agent_pid}" 2>/dev/null
12+
echo "stopping nginx ..."
13+
kill -TERM "${nginx_pid}" 2>/dev/null
14+
}
15+
16+
trap 'handle_term' TERM
17+
18+
# Launch nginx
19+
echo "starting nginx ..."
20+
nginx -g "daemon off;" &
21+
22+
nginx_pid=$!
23+
24+
cat /etc/nginx-agent/nginx-agent.conf
25+
# start nginx-agent, pass args
26+
echo "starting nginx-agent ..."
27+
nginx-agent "$@" &
28+
29+
agent_pid=$!
30+
31+
if [ $? != 0 ]; then
32+
echo "couldn't start the agent, please check the log file"
33+
exit 1
34+
fi
35+
36+
wait_term()
37+
{
38+
wait ${agent_pid}
39+
trap - TERM
40+
kill -QUIT "${nginx_pid}" 2>/dev/null
41+
echo "waiting for nginx to stop..."
42+
wait ${nginx_pid}
43+
}
44+
45+
wait_term
46+
47+
echo "nginx-agent process has stopped, exiting."
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# /etc/nginx-agent/nginx-agent.conf
3+
#
4+
# Configuration file for NGINX Agent.
5+
#
6+
# This file is to track agent configuration values that are meant to be statically set. There
7+
# are additional agent configuration values that are set via the API and agent install script
8+
# which can be found in /etc/nginx-agent/agent-dynamic.conf.
9+
10+
log:
11+
# set log level (panic, fatal, error, info, debug, trace; default "info")
12+
level: info
13+
# set log path. if empty, don't log to file.
14+
path: /var/log/nginx-agent/
15+
16+
nginx:
17+
# path of NGINX logs to exclude
18+
exclude_logs: ""
19+
socket: ""
20+
21+
dataplane:
22+
status:
23+
# poll interval for data plane status - the frequency the agent will query the dataplane for changes
24+
poll_interval: 30s
25+
# report interval for data plane status - the maximum duration to wait before syncing dataplane information if no updates have being observed
26+
report_interval: 24h
27+
28+
metrics:
29+
# specify the size of a buffer to build before sending metrics
30+
bulk_size: 20
31+
# specify metrics poll interval
32+
report_interval: 1m
33+
collection_interval: 15s
34+
mode: aggregated
35+
36+
# OSS NGINX default config path
37+
# path to aux file dirs can also be added
38+
config_dirs: "/etc/nginx"
39+
40+
api:
41+
# default port for Agent API, this is for the server configuration of the REST API
42+
port: 8081
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This manifest is for testing purposes and is not the final manifest for the nginx-with-agent.
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: nginx-with-agent
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: nginx-with-agent
11+
template:
12+
metadata:
13+
labels:
14+
app: nginx-with-agent
15+
spec:
16+
serviceAccountName: default
17+
automountServiceAccountToken: false
18+
containers:
19+
- image: docker.io/nginx-kubernetes-gateway/nginx-with-agent:edge
20+
imagePullPolicy: IfNotPresent
21+
name: nginx-with-agent
22+
securityContext:
23+
allowPrivilegeEscalation: true
24+
runAsNonRoot: true
25+
runAsUser: 101 #nginx
26+
capabilities:
27+
drop:
28+
- ALL
29+
ports:
30+
- name: http
31+
containerPort: 8080
32+
- name: https
33+
containerPort: 8443

build/nginx-with-agent/nginx.conf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
load_module /usr/lib/nginx/modules/ngx_http_js_module.so;
2+
3+
events {}
4+
5+
pid /etc/nginx/nginx.pid;
6+
7+
error_log /var/log/nginx/error.log debug;
8+
9+
http {
10+
include /etc/nginx/conf.d/*.conf;
11+
js_import /usr/lib/nginx/modules/njs/httpmatches.js;
12+
13+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
14+
'$status $body_bytes_sent "$http_referer" '
15+
'"$http_user_agent" "$http_x_forwarded_for" ';
16+
17+
access_log /var/log/nginx/access.log main;
18+
19+
# stub status API
20+
# needed by the agent in order to collect metrics
21+
server {
22+
listen 127.0.0.1:8082;
23+
location /api {
24+
stub_status;
25+
allow 127.0.0.1;
26+
deny all;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)