Create a Custom Annotation for the Kubernetes ingress-nginx Controller
By: Fernando Diaz
Tags: Nginx, Programming, DevOps, Cloud Computing, Kubernetes

Testing the Annotation

In order to test the annotation, we can perform the following:

1. First, let’s add memory to Docker Desktop. Although you can run with less memory, 8GB is recommended.

2. Now we can install all the required applications to create the development environment.

I am using OSX, so I will install using Homebrew, but these tools can be installed on other operating systems by other means. Click on each link for an install guide.

3. Create a development cluster running your version of the ingress-nginx controller. This is done with a few simple commands.

$ cd ingress-nginx $ make clean $ make build

go: downloading k8s.io/klog v1.0.0

go: downloading k8s.io/apimachinery v0.18.5

... $ make dev-env

[dev-env] building image

...

Kubernetes cluster ready and ingress-nginx listening in localhost using ports 80 and 443

... $ kubectl get pods -n ingress-nginx

NAME READY STATUS

ingress-nginx-admission-create-mjqhb 0/1 Completed

ingress-nginx-admission-patch-krjg8 0/1 Completed

ingress-nginx-controller-6fcd5657-vql5g 1/1 Running

4. Create a Deployment, Service, and Ingress. This step is needed so that we can test our new ingress rule.

Deployment

Here we are deploying gcr.io/kubernetes-e2e-test-images/echoserver:2.1 just responds with information about the request. The application runs on port 8080.

$ echo "

apiVersion: apps/v1

kind: Deployment

metadata:

name: meow

spec:

replicas: 2

selector:

matchLabels:

app: meow

template:

metadata:

labels:

app: meow

spec:

containers:

- name: meow

image: gcr.io/kubernetes-e2e-test-images/echoserver:2.1

ports:

- containerPort: 8080

" | kubectl apply -f - # wait a min for the deployment to be created

$ kubectl get deploy

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE

meow 2 2 2 2 # you should have 2 pods running

$ kubectl get pods

NAME READY STATUS

meow-5557bc7c54-cw2ck 1/1 Running

meow-5557bc7c54-kfzm5 1/1 Running

Service

This service allows other pods to access the application above via the Cluster-IP assigned.

$ echo "

apiVersion: v1

kind: Service

metadata:

name: meow-svc

spec:

ports:

- port: 80

targetPort: 8080

protocol: TCP

name: http

selector:

app: meow

" | kubectl apply -f - # wait a min for the service to be created

$ kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)

meow-svc ClusterIP 10.107.78.24 <none> 80/TCP

Ingress

This ingress allows us to access the service meow-svc via the /meow path. Since we didn’t specify a host, then we can access it from the cluster. We will send a request, just to make sure it responds appropriately.

$ echo "

apiVersion: extensions/v1beta1

kind: Ingress

metadata:

name: meow-ingress

annotations:

nginx.ingress.kubernetes.io/nickname: \"crazzzzy-server\"

spec:

rules:

- http:

paths:

- path: /meow

backend:

serviceName: meow-svc

servicePort: 80

" | kubectl apply -f - $ kubectl get ing

NAME CLASS HOSTS ADDRESS PORTS AGE

meow-ingress <none> * 80 5s $ kubectl get pod -n ingress-nginx

NAME READY STATUS

ingress-nginx-admission-create-mjqhb 0/1 Completed

ingress-nginx-admission-patch-krjg8 0/1 Completed

ingress-nginx-controller-6fcd5657-vql5g 1/1 Running # Since we haven't exposed the ingress-controller, let's see if the routing is working from the pod

$ kubectl exec -it ingress-nginx-controller-6fcd5657-vql5g -n ingress-nginx -- curl 127.0.0.1/meow Hostname: meow-85d75dbf8d-lczd2

Pod Information:

-no pod information available-

Server values:

server_version=nginx: 1.12.2 - lua: 10010

Request Information:

client_address=10.244.0.6

method=GET

real path=/meow

query=

request_version=1.1

request_scheme=http

request_uri=http://127.0.0.1:8080/meow Request Headers:

accept=*/*

host=127.0.0.1

user-agent=curl/7.67.0

x-forwarded-for=127.0.0.1

x-forwarded-host=127.0.0.1

x-forwarded-port=80

x-forwarded-proto=http

x-real-ip=127.0.0.1

x-request-id=c183a12ae4aec14ea023b7e23d1e5246

x-scheme=http Request Body:

-no body in request-

5. Now let’s see if our changes propagated. We can do this by checking the ingress-nginx config file for changes.

$ kubectl exec -it ingress-nginx-controller-6fcd5657-vql5g -n ingress-nginx -- cat /etc/nginx/nginx.conf | grep crazzzzy-server -a5 -b5

7411- proxy_cache_path /tmp/nginx-cache-auth levels=1:2 keys_zone=auth_cache:10m max_size=128m inactive=30m use_temp_path=off;

7534-

7537- # Global filters

7556-

7559- ## start server _

7579: # crazzzzy-server

7599- server {

7610- server_name _ ;

7629-

7633- listen 80 default_server reuseport backlog=511 ;

7685- listen [::]:80 default_server reuseport backlog=511 ;

And there you have it, a working annotation! Now you can get started with creating annotations for all of the features available to nginx.

You can start by checking out which issues you would like to work on or by seeing what nginx directives have yet to be implemented.