Ingress

K8s Load Balancer

NGINX Ingress

Deploy NGINX ingress

Deploy NGINX ingress:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml

Deploy NGINX ingress service (example for bare-metal):

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml

Check if the service was created:

kubectl get services --namespace=ingress-nginx

Output example:

NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.111.35.186   <none>        80:32022/TCP,443:31845/TCP   29m

Note port(s) of the service. It will be used later to access your host.

Test a deployment

Let's create two different deployments to test.

Create the first deployment:

kubectl create -f - <<EOF

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-first-deployment
data:
  index.html: |-
    TEST 001
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: first-deployment
  labels:
    app: first-deployment
spec:
  selector:
    matchLabels:
      app: first-deployment
  template:
    metadata:
      labels:
        app: first-deployment
    spec:
    
      volumes:
      - name: v-nginx-001
        configMap:
          name: cm-first-deployment

      containers:
      - name: first-deployment-api
        image: nginx
        
        ports:
        - containerPort: 80
        
        volumeMounts:
        - name: v-nginx-001
          mountPath: /usr/share/nginx/html/index.html
          subPath: index.html

---
kind: Service
apiVersion: v1
metadata:
  name: first-service
spec:
  selector:
    app: first-deployment
  ports:
  - protocol: TCP
    port: 80
  type: NodePort

EOF

Create the second deployment:

kubectl create -f - <<EOF

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-second-deployment
data:
  index.html: |-
    TEST 002
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: second-deployment
  labels:
    app: second-deployment
spec:
  selector:
    matchLabels:
      app: second-deployment
  template:
    metadata:
      labels:
        app: second-deployment
    spec:
    
      volumes:
      - name: v-nginx-001
        configMap:
          name: cm-second-deployment

      containers:
      - name: second-deployment-api
        image: nginx
        
        ports:
        - containerPort: 80
        
        volumeMounts:
        - name: v-nginx-001
          mountPath: /usr/share/nginx/html/index.html
          subPath: index.html

---
kind: Service
apiVersion: v1
metadata:
  name: second-service
spec:
  selector:
    app: second-deployment
  ports:
  - protocol: TCP
    port: 80
  type: NodePort

EOF

Create ingress

Text:

kubectl create -f - <<EOF

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: virtual-host
  annotations:
    # Target URI where the traffic must be redirected
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: test-001.com
    http:
      paths:
      - backend:
          serviceName: first-service
          servicePort: 80
  - host: test-002.com
    http:
      paths:
      - backend:
          serviceName: second-service
          servicePort: 80

  - host: test-003.com
    http:
      paths: 
        - path: /test-001
          backend:
            serviceName: first-service
            servicePort: 80
        - path: /test-002
          backend: 
            serviceName: second-service
            servicePort: 80 

EOF

Check the ingress:

kubectl get ingress

Test

Get your cluster IP:

kubectl cluster-info

Edit your hosts file accordingly, for example:

192.168.99.101 test-001.com
192.168.99.101 test-002.com
192.168.99.101 test-003.com

http://test-001.com:30808/ http://test-002.com:30808/ http://test-003.com:30808/test-001 http://test-003.com:30808/test-002

Use you ingress service port.

References

http://fabricioveronez.net/2019/04/05/kubernetes-ingress-controller/

https://kubernetes.github.io/ingress-nginx/deploy/

https://itnext.io/kubernetes-ingress-controllers-how-to-choose-the-right-one-part-1-41d3554978d2

https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md

Last updated