# Ingress

## NGINX Ingress

### Deploy NGINX ingress

Deploy NGINX ingress:

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

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

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

{% hint style="info" %}
Check your provider: <https://kubernetes.github.io/ingress-nginx/deploy/#provider-specific-steps>
{% endhint %}

Check if the service was created:

```bash
kubectl get services --namespace=ingress-nginx
```

Output example:

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

{% hint style="info" %}
Note port(s) of the service. It will be used later to access your host.
{% endhint %}

### Test a deployment

Let's create two different deployments to test.

Create the **first** deployment:

```bash
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:

```bash
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:

```bash
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:

```bash
kubectl get ingress
```

### Test

Get your cluster IP:

```bash
kubectl cluster-info
```

Edit your hosts file accordingly, for example:

```bash
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>

{% hint style="info" %}
Use you ingress service port.
{% endhint %}

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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.devops.buzz/public/kubernetes/ingress.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
