一、布景

其实在实际运用k8s ingress时,rewrite重定向和TCP转发对错常常见的,所以有必要了解根本的运用,关于Nginx Ingress更具体的介绍,能够参阅我之前的文章:Kubernetes(k8s)Ingress原理

二、K8s Ingress装置

条件也是需求k8s环境,k8s环境布置能够参阅我这篇文章:Kubernetes(k8s)最新版最完整版环境布置+master高可用完成(k8sV1.24.1+dashboard+harbor)

  • ingress 官方网站:kubernetes.github.io/ingress-ngi…
  • ingress 仓库地址:github.com/kubernetes/…

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作

### 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 's@k8s.gcr.io/ingress-nginx/controller:v1.2.0\(.*\)@registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.2.0@' deploy.yaml
sed -i 's@k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1\(.*\)$@registry.cn-hangzhou.aliyuncs.com/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参数,能够参阅官方文档:kubernetes.github.io/ingress-ngi…

2)示例演示

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作

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

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作

【示例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

ingress-http-test2.com/nginx-1/

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作
ingress-http-test2.com/apache-1/
【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作

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

ingress-http-test2.com/nginx-1/

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作
ingress-http-test2.com/apache-1/
【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作

四、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,如下:

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作
履行更新

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

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作

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

【云原生】K8s Ingress rewrite与TCP四层转发讲解与实战操作
从上面看用nginx ingress做tcp转发还是比较费事的,所以这里引荐运用istio,有不清楚的istio的,能够参阅我这篇文章:【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

K8s Ingress rewrite与TCP四层转发讲解与实战操作就先到这里了,有疑问的小伙伴欢迎留言,后续会持续更新【大数据+云原生】相关的文章~