Let's now add an important element to our cluster, in order to get closer and closer to creating an HA cluster.
In Kubernetes, an Ingress is a object that allows access to your Kubernetes services from outside the cluster. The ingress is made up of 2 parts: the kubernetes component that deals with directing external traffic to internal services is called the ingress controller, which obeys the rules present in the ingress resources. So, you configure access by creating a collection of rules, written in a file, that define which inbound connections reach which services. Without the use of the ingress, all the cluster services, to which one wishes to access from the outside, must be exposed to the internet. The advantage of the ingress consists, in fact, in exposing a single access point externally, which will take care of routing the traffic within the cluster.
First we added a new node to the cluster, which will take care of routing incoming requests to the appropriate services. This node will receive requests from the internet, so it must have a FIP. So, we created a new VM (Launch and manage instances) with a low-medium flavor, as it should only act as an ingress, and joined it to cluster (Building the cluster). We then assigned to the node, through a label, the "role" of ingress with the command
# Enter the node name and label. The optional "--overwrite" flag is used in case the value is already present $ kubectl label node <node_name> kubernetes.io/role=<label_value> [--overwrite] |
The addition of the label will be used later, to indicate on which node to install the input controller Pod. The same operation can also be performed on the other nodes, in order to obtain
# Note the "ROLES" column $ kubectl get node NAME STATUS ROLES AGE VERSION mycentos-0.novalocal Ready master 70d v1.19.1 mycentos-1.novalocal Ready worker 69d v1.19.1 mycentos-2.novalocal Ready worker 69d v1.19.1 mycentos-ing.novalocal Ready ingress 4d19h v1.19.1 |
Ingress Controller
As said previously, you must have an Ingress controller to satisfy an Ingress. Only creating an Ingress resource has no effect. There are several input controllers, here we will use one of the most used. Let's go, therefore, on the installation guide of the Nginx Ingress Controller for Kubernetes (an installation with Heml is also available on the same site).
Before continuing with step 2 of the guide, let's stop for a moment, because we need to make some small changes to the files in the folder we just cloned from the GitHub repo. The files in question are located here:
In the first file you only need to add the following line, which explains the FIP of the new node
kind: ConfigMap apiVersion: v1 metadata: name: nginx-config namespace: nginx-ingress data: . . . external-status-address: "<FIP_Node>" #----- Insert the FIP of the node between the quotes |
As for the other file, let's add these lines in the specification
spec:
.
.
.
spec:
hostNetwork: true #----- Thanks to this instruction the controller Pod will have the same IP as the node
nodeSelector: #----- The Pod will be created inside the node
kubernetes.io/role: ingress #----- with the role "ingress" |
and uncomment the lines, present at the end of the file, indicated below (commented by default)
args: - -nginx-configmaps=$(POD_NAMESPACE)/nginx-config - -default-server-tls-secret=$(POD_NAMESPACE)/default-server-secret # - -v=3 # Enables extensive logging. Useful for troubleshooting. - -report-ingress-status #----- Uncommented # - -external-service=nginx-ingress #- -enable-prometheus-metrics #- -global-configuration=$(POD_NAMESPACE)/nginx-configuration - -enable-leader-election #----- Uncommented #- -enable-custom-resources |
This last change will come in handy when we create the ingress resource component.
With these changes made, we can continue with the steps presented in the official Nginx guide. If all went well, we should get the input controller pod
# Note that the Pod is present on the "ingress" node and has its own IP $ kubectl get pod -n nginx-ingress -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-ingress-68d7b4bfdd-mw479 1/1 Running 0 20h 192.168.100.13 mycentos-ing.novalocal |
We now present the ingress resource through a complete example, present in the directory downloaded from GitHub (/kubernetes-ingress/examples/complete-example). Let's move inside the complete-example directory and run the commands
$ ls cafe-ingress.yaml cafe-secret.yaml cafe.yaml dashboard.png README.md $ kubectl apply -f cafe.yaml $ kubectl apply -f cafe-secret.yaml |
At this point we check that the Pods and the associated services have been created
# Two coffee pods and three tea pods have been created, as required within the "cafe.yaml" file $ kubectl get pod -n nginx-ingress -o wide NAME READY STATUS RESTARTS AGE IP NODE coffee-5f56ff9788-7fv8t 1/1 Running 0 20h 10.10.231.197 mycentos-1.novalocal coffee-5f56ff9788-l5l9h 1/1 Running 0 20h 10.10.94.70 mycentos-2.novalocal tea-69c99ff568-68bnr 1/1 Running 0 20h 10.10.94.71 mycentos-2.novalocal tea-69c99ff568-k457k 1/1 Running 0 20h 10.10.231.198 mycentos-1.novalocal tea-69c99ff568-pb9wc 1/1 Running 0 20h 10.10.231.199 mycentos-1.novalocal $ kubectl get svc -n nginx-ingress NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE coffee-svc ClusterIP 10.107.81.22 <none> 80/TCP 20h tea-svc ClusterIP 10.110.106.241 <none> 80/TCP 20h |
Finally, we create the input resource with the usual command kubectl apply -f <file.yaml>, even if it is better to take a look at the contents of the .yaml file first
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: cafe-ingress
spec:
tls:
- hosts:
- cafe.example.com
secretName: cafe-secret
rules:
- host: cafe.example.com
http:
paths:
- path: /tea # Use cafe.example.com/tea to target "tea" services
backend:
serviceName: tea-svc # Enter the service name
servicePort: 80 # Enter the port number on which the service is listening
- path: /coffee # Use cafe.example.com/coffee to target "coffee" services
backend:
serviceName: coffee-svc # Enter the service name
servicePort: 80 # Enter the port number on which the service is listening |
We verify that the ingress resource has been created correctly
# In the ADDRESS column there is the FIP of the ingress node $ kubectl get ing -n nginx-ingress NAME CLASS HOSTS ADDRESS PORTS AGE cafe-ingress <none> cafe.example.com 131.154.97.164 80, 443 20h |