Kubernetes Ingress-Nginx on Minikube

By in , , ,
No comments
  1. Deploy our pods(containers), using deployments
$ echo "
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: echo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: echo
  template:
    metadata:
      labels:
        app: echo
    spec:
      containers:
      - name: echo
        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
echo       2         2         2            2
# you should have 2 pods running
$ kubectl get pods
NAME                       READY     STATUS
echo-6cd655b94f-cv8xt     1/1       Running
echo-6cd655b94f-kfzm6     1/1       Running

2. Expose pod using service

$ echo "
apiVersion: v1
kind: Service
metadata:
  name: echo-svc
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
    name: http
  selector:
    app: echo
" | kubectl apply -f -
# wait a min for the service to be created
$ kubectl get svc
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)
echo-svc       ClusterIP   10.107.244.162    <none>        80/TCP

3. Setup ingress

$ echo "
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: echo-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /echo
        backend:
          serviceName: echo-svc
          servicePort: 80
" | kubectl apply -f -
# wait a min for the ingress resource to be created
$ kubectl get ing
NAME           HOSTS     ADDRESS     PORTS
echo-ingress   *         10.0.2.15   80

4. Access Application

$ minikube ip
192.168.99.100
$ curl 192.168.99.100/echo
Hostname: meow-799b895f78-v4l4f
Pod Information:
-no pod information available-
Server values:
server_version=nginx: 1.12.2 - lua: 10010
Request Information:
client_address=172.17.0.5
method=GET
real path=/meow
query=
request_version=1.1
request_scheme=http
request_uri=http://192.168.99.100:8080/meow
Request Headers:
accept=*/*
connection=close
host=192.168.99.100
user-agent=curl/7.54.0
x-forwarded-for=192.168.99.1
x-forwarded-host=192.168.99.100
x-forwarded-port=443
x-forwarded-proto=https
x-original-uri=/meow
x-real-ip=192.168.99.1
x-request-id=a1c65aa5ae7a574e47aaff49548160c5
x-scheme=https
Request Body:
-no body in request-

5. Run nginx with the config below: Point everything to 192.168.99.100

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    gzip on;
    gzip_vary on;
    gzip_min_length 10240;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
    gzip_disable "MSIE [1-6]\.";


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    server {
        listen 8888;
        server_name localhost;
	    location / {
            proxy_set_header  Host $host;
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header  X-Forwarded-For $remote_addr;
            proxy_set_header  X-Forwarded-Host $remote_addr;
            proxy_pass        http://192.168.99.100/;
        }
    }

}

Now you can access the service from inside the network at in http://{machine-ip}:8888

Leave a Reply

Your email address will not be published. Required fields are marked *