# Create Kubernetes Nginx Ingress Controller for External API Traffic

## Walkthrough

### Connecting to the CloudAcademy Web based K8s IDE <a href="#lab-page-title" id="lab-page-title"></a>

In the AWS Management Console search bar, enter *EC2*, and click the **EC2** result under **Services.**

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2Frb7zIGTuiKzxOZohsiQR%2Fimage.png?alt=media&#x26;token=eabf86f9-bfbc-4297-a925-877ffa9e30a1" alt=""><figcaption></figcaption></figure>

In the left-hand menu, click on **Instances**, select the *k8s.cluster.cloudacademy.platform.instance* EC2 instance, and locate and copy the *IPv4 Public IP* address.&#x20;

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2Fe6Ad1fu6ovvupcFQkm18%2Fimage.png?alt=media&#x26;token=5a4950be-89dc-4568-99f4-0deef4e4ae45" alt=""><figcaption></figcaption></figure>

The web-based CloudAcademy IDE has been configured to listen for inbound connections on port **3000** using HTTP. Using your browser, navigate to the IDE hosted on the *k8s.cluster.cloudacademy.platform.instance* EC2 instance using the public IP address you just copied.

```
http://PUBLIC_IP_IDE_CLOUDACADEMY_PLATFORM_INSTANCE:3000
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FY6MnBSa0vvtaTaoJEzla%2Fimage.png?alt=media&#x26;token=4f2a3f17-776b-41ae-95d5-1108b50538ea" alt=""><figcaption></figcaption></figure>

### Install Cilium CNI <a href="#lab-page-title" id="lab-page-title"></a>

Within the Files Explorer pane right-click on the **project/code/cillium** folder and then select the **Open in Terminal** option to launch the integrated terminal

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FSQuHmGRZYqLqTIbHwx1b%2Fimage.png?alt=media&#x26;token=7cd6bdfd-dbae-41fe-b983-eff4b46c8426" alt=""><figcaption></figcaption></figure>

Use the **kubectl** command to install the Cilium Kubernetes CNI plugin into the local Kubernetes cluster. Within the terminal enter the following command.

```
kubectl apply -f install.1.6.1.yaml
```

Use both **watch** and **kubectl** to view the Cilium pods starting up. Within the terminal enter the following commands

```
watch -n2 kubectl -n kube-system get pods
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FNoZ9LMw3H149ylRtJLO9%2Fimage.png?alt=media&#x26;token=ac4e53eb-b418-4af0-bbcf-6d99c111273a" alt=""><figcaption></figcaption></figure>

## Install Nginx Ingress Controller <a href="#lab-page-title" id="lab-page-title"></a>

Change directories. Within the terminal enter the following command

```
cd /home/project/code/ingress-nginx 
```

Install the Nginx Ingress Controller resources. Within the terminal enter the following

```
kubectl apply -f install.0.32.0.yaml
```

Examine the new Nginx Ingress Controller resources created by the previous command. Within the terminal run the following commands

```
kubectl get all -n ingress-nginx
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2Fdyo44V3Mfh3wnWxDDbq8%2Fimage.png?alt=media&#x26;token=b48f24ad-3c82-4f23-ac2f-72082c1ec0f9" alt=""><figcaption></figcaption></figure>

Behind the scenes minikube is being used to provide the Kubernetes cluster within this lab environment. To facilitate internet inbound traffic to the Nginx ingress controller hosted when using minikube for the cluster - you'll need to make a networking update (`hostNetwork: true`) to the **ingress-nginx-controller** deployment resource. To accomplish this, first generate a local copy of the YAML configuration for the **ingress-nginx-controller** deployment resource. Within the terminal run the following commands.

```
kubectl get deploy ingress-nginx-controller -n ingress-nginx -o yaml >> nginx-ingress-controller.yaml
```

The **nginx-ingress-controller.yaml** manifest file is very large. To make it easier to inject the new **hostNetwork** setting into the exact required location, use the provided **yq** utility. The **yq** utility will first need to be made executable. Within the terminal run the following commands.

```
chmod +x ../utils/yq
```

Confirm that the **yq** utility is now executable. Within the terminal run the following commands

```
ls -la ../utils/yq
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FuTTFOilHYh6q0NbHk8Ji%2Fimage.png?alt=media&#x26;token=2b2d137f-6f46-477a-9aec-2f3d50e69fd8" alt=""><figcaption></figcaption></figure>

Inject the **hostNetwork: true** setting into the **nginx-ingress-controller.yaml** file. Within the terminal run the following commands.

```
../utils/yq w nginx-ingress-controller.yaml spec.template.spec.hostNetwork true > nginx-ingress-controller-v2.yaml
```

Delete and recreate the updated **ingress-nginx-controller** deployment. Within the terminal run the following commands

```
kubectl delete deploy ingress-nginx-controller -n ingress-nginx
kubectl apply -f nginx-ingress-controller-v2.yaml
```

### Deploy the API Deployment and Service Resources <a href="#lab-page-title" id="lab-page-title"></a>

Within the **Files** pane, open the **project/code/API/lab-code/deploy-api.yaml** file within the editor. Take some time to review the Kubernetes resources that are going to be provisioned within the cluster:

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2F5sMPLEe7haIaN07zFU9e%2Fimage.png?alt=media&#x26;token=f92e0eec-3347-49bb-8e82-2e59f8fe4989" alt=""><figcaption></figcaption></figure>

Ensure that you are in the **lab-code** directory. Within the terminal run the following commands

```
cd /home/project/code/API/lab-code
```

You will now create all of the API resources declared within the **deploy-api.yaml** file. Within the terminal run the following commands

```
kubectl apply -f deploy-api.yaml
```

Confirm that all of the pods have launched successfully. Within the terminal run the following commands.

```
kubectl get pods
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FDPy9JXOcRn95z31Er4P5%2Fimage.png?alt=media&#x26;token=5ab9786b-a3b7-4bc4-8d92-e3fadb396b47" alt=""><figcaption></figcaption></figure>

Confirm that the service has launched successfully. Within the terminal run the following commands

```
kubectl get services
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2Fbf7jXjOGrK7qR5t80J7Q%2Fimage.png?alt=media&#x26;token=b6c82e14-bb10-4c42-aa49-23e79c60c456" alt=""><figcaption></figcaption></figure>

### Deploy the API Ingress Resource <a href="#lab-page-title" id="lab-page-title"></a>

Within the **Files** pane, open the **lab-code/deploy-api-ingress.yaml** file within the editor. Take some time to review the Kubernetes Ingress resource that is going to be provisioned within the cluster.

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2F20oZJPHPas58Szubex72%2Fimage.png?alt=media&#x26;token=0eb822fb-5cb9-45f2-a0f3-065c71589534" alt=""><figcaption></figcaption></figure>

Ensure that you are in the **lab-code** directory. Within the terminal run the following commands

```
cd /home/project/code/API/lab-code
```

Before you create the Kubernetes Ingress resource, you need to update the **deploy-api-ingress.yaml** file by replacing the X.X.X.X placeholder with the public IP address assigned to the EC2 instance that the Kubernetes cluster is running on. Within the terminal run the following commands

```
EXTIP=`curl ipinfo.io/ip`
sed -i -e "s/X.X.X.X.nip.io/$EXTIP.nip.io/g" deploy-api-ingress.yaml
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2F0Jo9cLLxKr1MI6Q982Hf%2Fimage.png?alt=media&#x26;token=921178e7-e30d-4ab6-ab50-f125efb34cff" alt=""><figcaption></figcaption></figure>

Create the new Kubernetes Ingress resource for the API service. Within the terminal run the following commands.

```
kubectl apply -f deploy-api-ingress.yaml
```

Confirm that all of the ingress resources has been created successfully. Within the terminal run the following commands.

```
kubectl get ingress
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FCBB2uRWkYACendTHcWk8%2Fimage.png?alt=media&#x26;token=ac7fe9bd-7c61-4a72-9e53-1ab3b86ff120" alt=""><figcaption></figcaption></figure>

## Perform External API Test <a href="#lab-page-title" id="lab-page-title"></a>

Within the terminal execute the following **curl** command to exercise the API

```
EXTIP=`curl ipinfo.io/ip`
curl -s api.$EXTIP.nip.io/languages  
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FqJmSuQMcYYWWhYvNC7nr%2Fimage.png?alt=media&#x26;token=c0f11ebd-62bf-4238-b0a2-e50b7264a876" alt=""><figcaption></figcaption></figure>

Install the jq utility to provide formatting capabilities. Within the terminal run the following commands

```
sudo apt-get install -y jq
```

Retest the API again, but this time format the response. Within the terminal run the following commands

```
curl -s api.$EXTIP.nip.io/languages | jq .
```

<figure><img src="https://2666479439-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLYaaERPa1yy1Qa8E1NQ%2Fuploads%2FCJLX1YDA8poO5OltidP6%2Fimage.png?alt=media&#x26;token=045e68cd-e8dd-4506-a02a-0efb23e5e27e" alt=""><figcaption></figcaption></figure>
