Skip to content

Adding the routing to the ingress controller

Course: Kubernetes Part 2 - chapter 11

Next up, we want to do the following -

  • Get rid of all existing deployments in the default namespace
kubectl get deployments
kubectl delete deployment_name
  • Get rid of all the services in the default namespace (dont delete the default kubernetes service, delete the old nginx and apache loadbalancer services)
kubectl get services
kubect
  • Create a deployment and service definition for the nginx image/app (this is different from the nginx controller)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
  • Create a deployment and service for the apache app
apiVersion: apps/v1
kind: Deployment
metadata:
name: apache-deployment
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
containers:
- name: my-apache-site
image: httpd:2.4
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: apache-service
namespace: default
spec:
selector:
app: apache
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
  • Create the ingress resource
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-apps-ingress
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: your-domain.com
http:
paths:
- path: /nginx
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
- path: /apache
pathType: Prefix
backend:
service:
name: apache-service
port:
number: 80
Create a combined manifest with all the api objects
$1b
  • Apply the manifest
kubectl apply -f complete.yml

  • Update your local hosts entry (/etc/hosts ) such that your-domain.com points to the IP of your load balancer
65.20.84.86 your-domain.com
  • Try going to [your-domain.com/apache](http://your-domain.com/apache) and your-domain.com/nginx