Kubernetes——Ingress
来源
本文章是复刻《云原生Kubernetes全栈架构师实战》
机器详情
版本:v1.27.6
主机名 | IP地址 | 内存 | CPU |
---|---|---|---|
k8s-master01 | 192.168.46.3/24 | 4G | 2C |
k8s-master02 | 192.168.46.4/24 | 4G | 2C |
k8s-master03 | 192.168.46.5/24 | 4G | 2C |
k8s-node01 | 192.168.46.6/24 | 4G | 2C |
k8s-node02 | 192.168.46.7/24 | 4G | 2C |
vip地址 | 192.168.46.10/24 |
---|---|
pod网段 | 172.16.0.0/12 |
service网段 | 10.0.0.0/16 |
安装Ingress Nginx Controller
安装Helm
# 下载需要的版本(这里选择v3.12.2)
https://github.com/helm/helm/releases
# 解压
tar -zxvf helm-v3.13.2-linux-amd64.tar.gz
# 移动helm程序
cp linux-amd64/helm /usr/local/bin/helm
# 测试是否安装成功
helm help
安装Ingress Nginx Controller
如下链接有许多Kubernetes packages
https://artifacthub.io/
# helm添加Ingress Nginx Controller仓库
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
# 查看最新稳定版本(我的版本为4.8.3)
helm search repo ingress-nginx
# 从仓库拉取
helm pull ingress-nginx/ingress-nginx
# 安装包解压
tar -xvf ingress-nginx-4.8.3.tgz
cd ingress-nginx/
# 修改values.yaml
vim values.yaml
# 修改ingress-nginx/controller的镜像地址并注释digest和digestChroot
registry: swr.cn-north-4.myhuaweicloud.com
image: ctl456/ingress-nginx-controller
## digest: sha256:5b161f051d017e55d358435f295f5e9a297e66158f136321d9b04520ec6c48a3
## digestChroot: sha256:5976b1067cfbca8a21d0ba53d71f83543a73316a61ea7f7e436d6cf84ddf9b26
# 修改ingress-nginx/opentelemetry镜像地址
image: swr.cn-north-4.myhuaweicloud.com/ctl456/ingress-nginx-opentelemetry:latest
# 修改ingress-nginx/kube-webhook-certgen镜像地址并注释digest
registry: swr.cn-north-4.myhuaweicloud.com
image: ctl456/ingress-nginx-kube-webhook-certgen
## digest: sha256:a7943503b45d552785aa3b5e457f169a5661fb94d82b8a3373bcd9ebaf9aac80
# 修改defaultbackend-amd64镜像地址
registry: swr.cn-north-4.myhuaweicloud.com
image: ctl456/defaultbackend-amd64
# 修改dnsPolicy
dnsPolicy: ClusterFirstWithHostNet
# 修改hostNetwork
hostNetwork: true
# 添加nodeSelector条件
ingress: "true"
# 修改kind
kind: DaemonSet
# 修改ingressClassResource
default: true
# 给k8s-node02打标签
kubectl label node k8s-node02 ingress=true
# 创建ingress-nginx命名空间
kubectl create ns ingress-nginx
# 安装
helm install ingress-nginx -n ingress-nginx .
# 查看部署情况
kubectl get pod -n ingress-nginx
Ingress Nginx入门
# 创建study-ingress命名空间用于测试
kubectl create ns study-ingress
# 创建一个简单的Nginx模拟Web服务
kubectl create deploy nginx --image=swr.cn-north-4.myhuaweicloud.com/ctl456/nginx:latest -n study-ingress
# 创建该容器的Service
kubectl expose deploy nginx --port 80 -n study-ingress
# 创建ingress指向Service
vim web-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: study-ingress
spec:
rules:
- host: nginx.test.com
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path: /
pathType: ImplementationSpecific
# 创建ingress
kubectl create -f web-ingress.yaml
# 由于Ingress Controller是以hostNetwork模式部署的
# 所以只需要将域名解析至Ingress Controller所在的节点就可以
# 查看Ingress Controller所在的节点
kubectl get pod -n ingress-nginx -o wide
# 解析域名(C:\Windows\System32\drivers\etc\hosts)
192.168.46.7 nginx.test.com
Ingress Nginx域名重定向Redirect
vim redirect.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/permanent-redirect: https://www.baidu.com
name: nginx-redirect
namespace: study-ingress
spec:
rules:
- host: nginx.redirect.com
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path: /
pathType: ImplementationSpecific
kubectl create -f redirect.yaml
windows设置好域名解析,浏览器访问http://nginx.redirect.com会自动跳转到百度
Ingress Nginx前后端分离Rewrite
# 创建一个应用模拟后端
kubectl create deploy backend-api --image=registry.cn-beijing.aliyuncs.com/dotbalo/nginx:backend-api -n study-ingress
# 创建service暴露该应用
kubectl expose deploy backend-api --port 80 -n study-ingress
vim rewrite.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: backend-api
namespace: study-ingress
spec:
ingressClassName: nginx
rules:
- host: nginx.test.com
http:
paths:
- backend:
service:
name: backend-api
port:
number: 80
path: /api-a(/|$)(.*)
pathType: ImplementationSpecific
kubectl create -f rewrite.yaml
Ingress Nginx错误代码重定向
vim values.yaml
# 修改defaultBackend
enabled: true
# 修改config
config:
apiVersion: v1
client_max_body_size: 20m
custom-http-errors: "404,415,503"
# 更新
helm upgrade ingress-nginx -n ingress-nginx .
# 查看是否runing
kubectl get pod -n ingress-nginx
Ingress Nginx SSL
# OpenSSL生成一个测试证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginx.test.com"
# 创建secret
kubectl create secret tls ca-secret --cert=tls.crt --key=tls.key -n study-ingress
vim ingress-ssl.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: study-ingress
spec:
ingressClassName: nginx
rules:
- host: nginx.test.com
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- nginx.test.com
secretName: ca-secret
kubectl create -f ingress-ssl.yaml
Ingress Nginx匹配请求
# 部署移动端应用
kubectl create deploy phone --image=registry.cn-beijing.aliyuncs.com/dotbalo/nginx:phone -n study-ingress
# 为该应用创建service
kubectl expose deploy phone --port 80 -n study-ingress
# 创建ingress
kubectl create ingress phone --rule=m.test.com/*=phone:80 -n study-ingress
# 部署计算机端应用
kubectl create deploy laptop --image=registry.cn-beijing.aliyuncs.com/dotbalo/nginx:laptop -n study-ingress
# 为该应用创建service
kubectl expose deploy laptop --port 80 -n study-ingress
vim values.yaml
# 修改allowSnippetAnnotations
allowSnippetAnnotations: true
helm upgrade ingress-nginx -n ingress-nginx .
vim laptop-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
set $agentflag 0;
if ($http_user_agent ~* "(Android|iPhone|Windows Phone|UC|Kindle)" ){
set $agentflag 1;
}
if ( $agentflag = 1 ) {
return 301 http://m.test.com;
}
name: laptop
namespace: study-ingress
spec:
ingressClassName: nginx
rules:
- host: test.com
http:
paths:
- backend:
service:
name: laptop
port:
number: 80
path: /
pathType: ImplementationSpecific
kubectl create -f laptop-ingress.yaml
浏览器访问
Ingress Nginx基本认证
# 安装所需工具
## ubuntu
apt install apache2-utils -y
## centos
yum install httpd -y
# 创建foo用户
htpasswd -c auth foo
# 创建secret
kubectl create secret generic basic-auth --from-file=auth -n study-ingress
vim ingress-with-auth.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/auth-realm: Please Input Your Username and Password
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-type: basic
name: ingress-with-auth
namespace: study-ingress
spec:
ingressClassName: nginx
rules:
- host: auth.test.com
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path:
pathType: ImplementationSpecific
kubectl create -f ingress-with-auth.yaml
Ingress Nginx黑/白名单
配置黑名单
vim values.yaml
# 修改config (多个IP可用逗号分隔)
config:
block-cidrs: 192.168.46.5
helm upgrade ingress-nginx -n ingress-nginx .
配置白名单
vim auth-whitelist.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/auth-realm: Please Input Your Username and Password
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/whitelist-source-range: 192.168.46.4
name: ingress-with-auth
namespace: study-ingress
spec:
ingressClassName: nginx
rules:
- host: auth.test.com
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path:
pathType: ImplementationSpecific
kubectl create -f auth-whitelist.yaml
Ingress Nginx速率限制
# 限制每秒的连接,单个IP
nginx.ingress.kubernetes.io/limit-rps
# 限制每分钟的连接,单个IP
nginx.ingress.kubernetes.io/limit-rpm
# 限制客户端每秒的传输的字节数,单位为KB,需要开启proxy-buffering
nginx.ingress.kubernetes.io/limit-rate
# 速率限制白名单
nginx.ingress.kubernetes.io/limit-whitelist
vim auth-rate-limit.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/auth-realm: Please Input Your Username and Password
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/limit-connections: "1"
name: ingress-with-auth
namespace: study-ingress
spec:
ingressClassName: nginx
rules:
- host: auth.test.com
http:
paths:
- backend:
service:
name: nginx
port:
number: 80
path:
pathType: ImplementationSpecific
kubectl create -f auth-rate-limit.yaml
使用Nginx实现灰度/金丝雀发布
创建v1版本
# 创建模拟生产环境的命名空间和服务
kubectl create ns production
kubectl create deploy canary-v1 --image=registry.cn-beijing.aliyuncs.com/dotbalo/canary:v1 -n production
kubectl expose deploy canary-v1 --port 8080 -n production
kubectl create ingress canary-v1 --rule=canary.com/*=canary-v1:8080 -n production
创建v2版本
# 创建命名空间模拟灰度环境
kubectl create ns canary
kubectl create deploy canary-v2 --image=registry.cn-beijing.aliyuncs.com/dotbalo/canary:v2 -n canary
kubectl expose deploy canary-v2 --port 8080 -n canary
Canary版本切入部分流量
vim canary-v2.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
name: canary-v2
namespace: canary
spec:
ingressClassName: nginx
rules:
- host: canary.com
http:
paths:
- backend:
service:
name: canary-v2
port:
number: 8080
path:
pathType: ImplementationSpecific
kubectl create -f canary-v2.yaml