This scenario shows how K8s ingress works on minikube. When browsing urls, ingress controller (nginx) directs traffic to the related services.
- Run minikube on Windows Hyperv or Virtualbox. In this scenario:
minikube start --driver=hyperv
or
minikube start --driver=hyperv --force-systemd
-
To install ingress controller on K8s cluster, please visit to learn: https://kubernetes.github.io/ingress-nginx/deploy/
-
On Minikube, it is only needed to enable ingress controller.
minikube addons enable ingress
minikube addons list
- Copy and save (below) as file on your PC (appingress.yaml).
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: appingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: webapp.com
http:
paths:
- path: /blue
pathType: Prefix
backend:
service:
name: bluesvc
port:
number: 80
- path: /green
pathType: Prefix
backend:
service:
name: greensvc
port:
number: 80
- Copy and save (below) as file on your PC (todoingress.yaml).
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: todoingress
spec:
rules:
- host: todoapp.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: todosvc
port:
number: 80
- Copy and save (below) as file on your PC (deploy.yaml).
apiVersion: apps/v1
kind: Deployment
metadata:
name: blueapp
labels:
app: blue
spec:
replicas: 2
selector:
matchLabels:
app: blue
template:
metadata:
labels:
app: blue
spec:
containers:
- name: blueapp
image: ozgurozturknet/k8s:blue
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /healthcheck
port: 80
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 3
---
apiVersion: v1
kind: Service
metadata:
name: bluesvc
spec:
selector:
app: blue
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: greenapp
labels:
app: green
spec:
replicas: 2
selector:
matchLabels:
app: green
template:
metadata:
labels:
app: green
spec:
containers:
- name: greenapp
image: ozgurozturknet/k8s:green
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /healthcheck
port: 80
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 3
---
apiVersion: v1
kind: Service
metadata:
name: greensvc
spec:
selector:
app: green
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: todoapp
labels:
app: todo
spec:
replicas: 1
selector:
matchLabels:
app: todo
template:
metadata:
labels:
app: todo
spec:
containers:
- name: todoapp
image: ozgurozturknet/samplewebapp:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: todosvc
spec:
selector:
app: todo
ports:
- protocol: TCP
port: 80
targetPort: 80
- Run "deploy.yaml" and "appingress.yaml" to create deployments and services
- Add url-ip on Windows/System32/Drivers/etc/hosts file:
- When running on browser the url "webapp.com/blue", one of the blue app containers return response.
- When running on browser the url "webapp.com/green", one of the green app containers return response.
- When running on browser, "todoapp.com":
-
Hence, we can open services running on the cluster with one IP to the out of the cluster.
-
Delete all yaml file and minikube.
https://github.com/aytitech/k8sfundamentals/tree/main/ingress