Ingress trong Kubernetes
Ingress là thành phần được dùng để điều hướng các yêu cầu traffic giao thức HTTP và HTTPS từ bên ngoài (interneet) vào các dịch vụ bên trong Cluster.
Ingress chỉ để phục vụ các cổng, yêu cầu HTTP, HTTPS còn các loại cổng khác, giao
thức khác để truy cập được từ bên ngoài thì dùng Service với kiểu NodePort và
LoadBalancer
Để
Nếu chọn Ngix Ingress Controller thì cài đặt theo: NGINX Ingress Controller for Kubernetes.
Phần này, chọn loại HAProxy Ingress Controller - HAProxy Ingress Controller
Cài đặt HAProxy Ingress Controller
Ở đậy sử dụng bản custome là HAProxy Ingress
Để triển khai thực hiện các bước sau:
#Tạo namespace có tên ingress-controller kubectl create ns ingress-controller
Triển khai các thành phần
kubectl apply -f https://haproxy-ingress.github.io/resources/haproxy-ingress.yaml
Thực hiện đánh nhãn các Node có thể chạy POD Ingress
# Gán thêm label cho các Node (ví dụ node worker2.xtl, worker1.xtl ...) kubectl label node master.xtl role=ingress-controller kubectl label node worker1.xtl role=ingress-controller kubectl label node worker2.xtl role=ingress-controller
Kiểm tra các thành phần
kubectl get all -n ingress-controller
Giờ các tên miên, trỏ tới các IP của Node trong Cluster đã được điều khiển bởi Haproxy,
ví dụ cấu hình một tên miền ảo (chính file /etc/hosts (Linux, macoS)
hoặc C:\Windows\System32\Drivers\etc\hosts (Windows), thêm vào tên miền ảo, giả sử
xuanthulab.test trỏ tới IP của một NODE nào đó
172.16.10.102 xuanthulab.test
Giờ truy cập địa chỉ http://xuanthulab.test sẽ thấy
Vì tên miền xuanthulab.test chưa được cấu hình đến dịch vụ cụ thể nào, nó đã chuyển traffic
do service/ingress-default-backend phục vụ.
Tạo một Ingress
Triển khai một ứng dụng (http) ví dụ
1.app-test.yaml
apiVersion: v1
kind: Service
metadata:
name: http-test-svc
# dịch vụ này tạo tại namespace có Haproxy Ingress
namespace: ingress-controller
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: http-test-app
sessionAffinity: None
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: http-test-svc
name: http-test-svc
# Deploy này tạo tại namespace có Haproxy Ingress
namespace: ingress-controller
spec:
replicas: 2
selector:
matchLabels:
run: http-test-app
template:
metadata:
labels:
run: http-test-app
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: http
ports:
- containerPort: 80
protocol: TCP
resources: {}
Triển khai:
kubectl apply -f 1.app-test.yaml
File trên triển khai một ứng dụng từ image nginx, trong đó có tạo
một service với tên http-test-svc để tương tác với các POD tạo ra.
Giờ ta sẽ tạo một Ingress để điều hướng traffic (http, https) vào dịch vụ này.
Tạo Ingress
2.app-test-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: app
namespace: ingress-controller
spec:
rules:
# Tên miền truy cập
- host: xuanthulab.test
http:
paths:
- path: /
backend:
# dịch vụ phục vụ tương ứng với tên miền và path
serviceName: http-test-svc
servicePort: 80
Triển khai:
kubectl apply -f 2.app-test-ingress.yaml
Giờ kiểm truy cập lại sẽ thấy
Tạo một Ingress với cấu hình SSL
Để cấu hình truy cập an toàn SSL, cần có các xác thực - các cert bạn mua, hoặc đăng ký miễn phí với https://letsencrypt.org/ cho tên miền cụ thể của bạn. Tuy nhiên để thực hành, sẽ sinh cert với openssl (tự xác thực - bị cảnh báo bởi trình duyệt).
Chạy lệnh sau để sinh xác thực cho tên miền ảo xuanthulab.test
openssl req -x509 -newkey rsa:2048 -nodes -days 365 -keyout privkey.pem -out fullchain.pem -subj '/CN=xuanthulab.test'
Sau đó tạo một Secret (thuộc namespace chạy POD), đặt tên Secret này là xuanthulab-test
kubectl create secret tls xuanthulab-test --cert=fullchain.pem --key=privkey.pem -n ingress-controller
Xóa đi Ingress cũ
kubectl delete -f 2.app-test-ingress.yaml
Tạo lại một Ingress có thiết lập xác thực SSL với cert trên
3.app-test-ingress-ssl.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: app
namespace: ingress-controller
spec:
tls:
- hosts:
- xuanthulab.test
secretName: xuanthulab-test
# - hosts:
# - otherdomain.coms
# secretName: otherssl-ssl
rules:
- host: xuanthulab.test
http:
paths:
- path: /
backend:
serviceName: http-test-svc
servicePort: 80
Triển khai
kubectl apply -f 3.app-test-ingress-ssl.yaml
Giờ bạn đã có thể truy cập tới https://xuanthulab.test
Một số thiết lập thêm
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: app
namespace: ingress-controller
annotations:
ingress.kubernetes.io/maxconn-server: "7"
ingress.kubernetes.io/balance-algorithm: "leastconn" # thuật toán cân bằng tải source, leastconn, roundrobin
ingress.kubernetes.io/timeout-queue: "2m"
ingress.kubernetes.io/ssl-redirect: "false"
spec:
.....
