目录
一、背景
其实在实际使用k8s ingress时,rewrite重定向和TCP转发是非常常见的,所以有必要了解基本的使用,关于Nginx Ingress更详细的介绍,可以参考我之前的文章:Kubernetes(k8s)Ingress原理
二、K8s Ingress安装
前提也是需要k8s环境,k8s环境部署可以参考我这篇文章:Kubernetes(k8s)最新版最完整版环境部署+master高可用实现(k8sV1.24.1+dashboard+harbor)
- ingress 官方网站:https://kubernetes.github.io/ingress-nginx/
- ingress 仓库地址:https://github.com/kubernetes/ingress-nginx
### 1、下载yaml部署文件
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/cloud/deploy.yaml
### 2、可以先把镜像下载,再安装
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.2.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
### 3、修改镜像地址
sed -i '[email protected]/ingress-nginx/controller:v1.2.0\(.*\)@registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.2.0@' deploy.yaml
sed -i '[email protected]/ingress-nginx/kube-webhook-certgen:v1.1.1\(.*\)[email protected]/google_containers/kube-webhook-certgen:v1.1.1@' deploy.yaml
### 4、还需要修改两地方
#1、kind: 类型修改成DaemonSet
#2、在dnsPolicy: ClusterFirst下面添加,去掉dnsPolicy: ClusterFirst:
#dnsPolicy: ClusterFirstWithHostNet
#hostNetwork: true
#3、把LoadBalancer修改成NodePort
#4、在--validating-webhook-key下面添加- --watch-ingress-without-class=true
kubectl apply -f deploy.yaml
三、K8s Ingress rewrite 讲解与使用
1)配置说明
Ingress rewrite实现是通过在annotations
下添加对应的字段,annotations 参数说明:
字段 | 字段类型 | 字段说明 |
---|---|---|
nginx.ingress.kubernetes.io/rewrite-target | 字符串 | 必须重定向流量的目标URI |
nginx.ingress.kubernetes.io/ssl-redirect | 布尔值 | 指示位置部分是否仅可访问sSL(当Ingress包含证书时,默认为true ) |
nginx.ingress.kubernetes.io/force-ssl-redirect | 布尔值 | 即使Ingress未启用rLS,也强制重定向到HTTPS |
nginx.ingress.kubernetes.io/app-root | 字符串 | 定义controller必须重定向的应用程序根,如果它在’/'上下文中 |
nginx.ingress, kubernetes.io/use-regex | 布尔值 | 指示Ingress.上定义的路径是否使用正则表达式 |
想了解更多annotations参数,可以参考官方文档:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/
2)示例演示
1、部署应用
# 添加chart源
helm repo add bitnami https://charts.bitnami.com/bitnami
# 安装Nginx
helm pull bitnami/nginx --version 13.2.1
helm install my-nginx-1 ./nginx-13.2.1.tgz
# 安装Apache
helm pull bitnami/apache --version 9.2.7
helm install my-apache-1 ./apache-9.2.7.tgz
2、配置ingress rewrite转发(http)
【示例1】
cat >ingress-http-test1.yaml<<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /test
pathType: Prefix
backend:
service:
name: my-nginx-1
port:
number: 80
EOF
【示例2】域名转发
cat >ingress-http-test2.yaml<<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: http-ingress-test
annotations: # 注解
nginx.ingress.kubernetes.io/rewrite-target: "/$1" # 重写
spec:
ingressClassName: "nginx" # 控制器选择为Nginx
rules:
- host: ingress-http-test2.com
http:
paths:
- path: /nginx-1/(.*)$
pathType: Prefix
backend:
service:
name: my-nginx-1
port:
number: 80
- path: /apache-1/(.*)$
pathType: Prefix
backend:
service:
name: my-apache-1
port:
number: 80
EOF
配置hosts
192.168.182.110 ingress-http-test2.com
http://ingress-http-test2.com/nginx-1/
http://ingress-http-test2.com/apache-1/
3、配置ingress rewrite转发(https)
【1】创建证书(有证书可忽略)
首先需要证书,如果没有证书,可以按照下面命令创建证书
openssl genrsa -out ingress-http-test2.key 2048
openssl req -new -x509 -key ingress-http-test2.key -out ingress-http-test2.cert -days 360 -subj /CN=ingress-http-test2.com
【2】创建 Secret
kubectl create secret tls https-tls-test --cert=ingress-http-test2.cert --key=ingress-http-test2.key
【3】Ingress引用Secret资源实现HTTPS
cat >ingress-https-test.yaml<<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: https-ingress-test
annotations: # 注解
nginx.ingress.kubernetes.io/rewrite-target: "/$1" # 重写
spec:
ingressClassName: "nginx" # 控制器选择为Nginx
rules:
- host: ingress-http-test2.com
http:
paths:
- path: /nginx-1/(.*)$
pathType: Prefix
backend:
service:
name: my-nginx-1
port:
number: 80
- path: /apache-1/(.*)$
pathType: Prefix
backend:
service:
name: my-apache-1
port:
number: 80
tls: # tls资源
- hosts:
- ingress-http-test2.com
secretName: https-tls-test
EOF
https://ingress-http-test2.com/nginx-1/
https://ingress-http-test2.com/apache-1/
四、K8s Ingress TCP四层转发
Ingress配置转发端口本质,还是利用service nodePort能力,通过暴露ingress的本地端口来转发。Ingress 默认不支持TCP or UDP services。因此Ingress controller使用
--tcp-services-configmap
和--udp-services-configmap
这两个配置达到转发端口的目的。
1)开启tcp/udp转发
检查nginx-ingress是否开启tcp/udp转发,如果没开启,需要修改deployment ingress-nginx的参数配置,添加–tcp-service-configmap,如下:
执行更新
kubectl apply -f deploy.yaml
2)部署应用(mysql)
helm repo add bitnami https://charts.bitnami.com/bitnami
helm pull bitnami/mysql --version 9.4.4
tar -xf mysql-9.4.4.tgz
helm install my-mysql ./mysql --version 9.4.4 --set global.storageClass='nfs-client' --set architecture='replication'
3)更改ingress-nginx的service,声明tcp和udp用的端口号
### 先查看
kubectl get svc -n ingress-nginx
### 添加对应配置
kubectl edit svc ingress-nginx-controller -n ingress-nginx
### 再查看
kubectl get svc -n ingress-nginx
3)定义configmap
格式为
<ingress-controller-svc-port>:"<namespace>/<service-name>:<port>
"。
例如下面配置的data第一行表示将default
命名空间下的kuard
服务的9527
端口映射到ingress-controller service的30880
端口,即可通过ingress-controller的service ip加30880
端口访问到kuard
服务。
cat >tcp-services.yaml<<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: tcp-services
namespace: ingress-nginx
data:
30336: "default/my-mysql-primary:3306"
EOF
4)检查
进入nginx-ingress容器查看TCP services处会出现对应的负载配置
kubectl exec -it -n ingress-nginx `kubectl get pods -n ingress-nginx |grep ingress-nginx-controller|head -1|awk '{print $1}'` -- cat nginx.conf
从上面看用nginx ingress做tcp转发还是比较麻烦的,所以这里推荐使用istio,有不清楚的istio的,可以参考我这篇文章:【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作
K8s Ingress rewrite与TCP四层转发讲解与实战操作就先到这里了,有疑问的小伙伴欢迎留言,后续会持续更新【大数据+云原生】相关的文章~
标签:Ingress,http,rewrite,ingress,TCP,nginx,转发,com From: https://www.cnblogs.com/liugp/p/16972366.html