k8s部署ingress-nginx的方法步骤
我的k8s版本是v1.33.6,系统架构arm,ingress-nginx是v1.14.1

1. ingress概述
我们知道service的表现形式为IP:PORT,即工作在第四层传输层(TCP/IP层),那么对于不同的URL地址经常对应用不同的后端服务或者虚拟服务器,这些应用层的转发机制仅通过kubernetes的service机制是无法实现的,这种情况我们可以使用ingress策略定义和一个具体的ingress Controller,两者结合实现一个完整的Ingress 负载均衡,这个负载均衡是基于nginx七层反向代理来实现。
k8s 对外暴露服务(service)主要有两种方式:NotePort, LoadBalance, 此外externalIPs也可以使各类service对外提供服务,但是当集群服务很多的时候,NodePort方式最大的缺点是会占用很多集群机器的端口;LB方式最大的缺点则是每个service一个LB又有点浪费和麻烦,并且需要k8s之外的支持; 而ingress则只需要一个NodePort或者一个LB就可以满足所有service对外服务的需求。工作机制大致可以用下图表示:

一种全局的、为了代理不同后端 Service 而设置的负载均衡服务,就是 Kubernetes 里的Ingress 服务。
Ingress由两部分组成:Ingress controller和Ingress服务。
Ingress Controller 会根据你定义的 Ingress 对象,提供对应的代理能力。业界常用的各种反向代理项目,比如 Nginx、HAProxy、Envoy、Traefik 等,都已经为Kubernetes 专门维护了对应的 Ingress Controller。

实际上,ingress相当于一个7层的负载均衡器,是k8s对反向代理的一个抽象。大概的工作原理也确实类似于Nginx,可以理解成在 Ingress 里建立一个个映射规则 , ingress Controller 通过监听 Ingress里的配置规则并转化成 Nginx 的配置 , 然后对外部提供服务。ingress包括:在这里有两个核心概念:
Ingress:kubernetes中的一个对象,作用是定义请求如何转发到service的规则
ingress controller: 核心是一个deployment,实现方式有很多,比如nginx, Contour, Haproxy,需要编写的yaml有:Deployment, Service, ConfigMap, ServiceAccount(Auth),其中service的类型可以是NodePort或者LoadBalancer。
Ingress(以nginx为例)的工作原理如下:
1、用户编写Ingress规则,说明那个域名对应kubernetes集群中的那个Service
2、Ingress Controller动态感知Ingress服务规则的变化,然后生成一段对应的Nginx反向代理配置
3、Ingress Controller会将生成的nginx配置写入到一个运行着的Nginx服务中,并动态更新
4、到此为止,其实真正在工作的就是一个Nginx了,内部配置了用户定义的请求转发规则

2、 ingress-nginx 部署(Ingress Controller+nginx)
简介: 首先,需要匹配Ingress-nginx版本和kubernetes版本。在https://github.com/kubernetes/ingress-nginx可以找到

网络不好可下载:百度网盘
【超级会员V3】通过百度网盘分享的文件:ingress-nginx.tar.gz
链接:https://pan.baidu.com/s/1WPsIj58GknSLzP9ON3TQHw?pwd=6vm4
tar xf ingress-nginx.tar.gz #里面有两个镜像和应用ingress 定义文件

应用ingress 定义文件
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/deploy.yaml
[root@k8s1 nginx-ingress]# kubectl -n ingress-nginx get pod
NAME READY STATUS RESTARTS AGE
ingress-nginx-controller-5d8448555-4f9m5 1/1 Running 0 19s
[root@k8s1 nginx-ingress]# kubectl -n ingress-nginx get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.1.102 <none> 80:31905/TCP,443:30929/TCP 56s
ingress-nginx-controller-admission ClusterIP 10.96.0.118 <none> 443/TCP 56s
3、准备service和pod
为了后面的实验比较方便,创建如下图所示的模型

4、http代理
1、创建ingress-http.yaml
apiVersion: v1
kind: Service
metadata:
name: owncloud
namespace: owncloud
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
nodePort: 31093
selector:
app: owncloud
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: owncloud
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: 2000M #添加覆盖默认 1MB 限制,支持上传 2GB 文件。
spec:
ingressClassName: nginx
rules:
- host: www.hjj.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: owncloud
port:
number: 80
#创建
[root@k8s1 nginx-ingress]# kubectl apply -f ingress-http.yaml
service/owncloud created
ingress.networking.k8s.io/example-ingress created
#查看
[root@k8s1 nginx-ingress]# kubectl get Ingress -n owncloud
NAME CLASS HOSTS ADDRESS PORTS AGE
example-ingress nginx www.hjj.com 10.90.11.63 80 99s
#查看详细
[root@k8s1 nginx-ingress]# kubectl describe Ingress -n owncloud
Name: example-ingress
Labels: <none>
Namespace: owncloud
Address: 10.90.11.63
Ingress Class: nginx
Default backend: <default>
Rules:
Host Path Backends
---- ---- --------
www.hjj.com
/ owncloud:80 (100.99.6.39:80)
Annotations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 105s (x2 over 2m12s) nginx-ingress-controller Scheduled for sync
[root@k8s1 nginx-ingress]# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-controller-5d8448555-4f9m5 1/1 Running 0 14m
[root@k8s1 nginx-ingress]# kubectl exec -it ingress-nginx-controller-5d8448555-4f9m5 -n ingress-nginx -- sh
/etc/nginx $ cat nginx.conf|grep www.hjj.com
## start server www.hjj.com
server_name www.hjj.com ;
## end server www.hjj.com
2、测试http是否成功
#查看svc端口
[root@k8s-master1 http]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx NodePort 10.0.0.126 <none> 80:30726/TCP,443:30852/TCP 25h
进入windows编辑C:\Windows\System32\drivers\etc\hosts

进入谷歌访问

4、https代理
1、创建证书
#生成证书
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/C=CN/ST=BJ/L=BJ/O=nginx/CN=hjj.com"
#创建密钥
kubectl create secret tls tls-secret --key tls.key --cert tls.crt -n iotdb-web #记得添加命名空间,如果使用免费ssl证书不加会不安全
kubectl create secret tls tls-secret --key tls.key --cert tls.crt -n owncloud #记得添加命名空间,如果使用免费ssl证书不加会不安全
创建ingress-https.yaml
apiVersion: v1
kind: Service
metadata:
name: owncloud
namespace: owncloud
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
nodePort: 31093
selector:
app: owncloud
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: owncloud
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: 2000M #添加覆盖默认 1MB 限制,支持上传 2GB 文件。
spec:
ingressClassName: nginx
tls:
- hosts:
- www.hjj.com
secretName: tls-secret
rules:
- host: www.hjj.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: owncloud
port:
number: 80
---
apiVersion: v1
kind: Service
metadata:
name: iotdb-frontend-service
namespace: iotdb-web
spec:
ports:
- port: 9090
targetPort: 9090
protocol: TCP
name: http
nodePort: 31095
selector:
app: iotdb-frontend
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: iotdb-web
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: 2000M #添加覆盖默认 1MB 限制,支持上传 2GB 文件。
spec:
ingressClassName: nginx
tls:
- hosts:
- mail.hjj.com
secretName: tls-secret
rules:
- host: mail.hjj.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: iotdb-frontend-service
port:
#创建
[root@k8s1 nginx-ingress]# kubectl apply -f ingress-https.yaml
service/owncloud unchanged
ingress.networking.k8s.io/example-ingress configured
service/iotdb-frontend-service unchanged
ingress.networking.k8s.io/example-ingress created
#查看
[root@k8s1 nginx-ingress]# kubectl describe Ingress -n owncloud
Name: example-ingress
Labels: <none>
Namespace: owncloud
Address: 10.90.11.63
Ingress Class: nginx
Default backend: <default>
TLS:
tls-secret terminates www.hjj.com
Rules:
Host Path Backends
---- ---- --------
www.hjj.com
/ owncloud:80 (100.99.6.39:80)
Annotations: nginx.ingress.kubernetes.io/proxy-body-size: 2000M
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 2m13s (x3 over 8m43s) nginx-ingress-controller Scheduled for sync
[root@k8s1 nginx-ingress]# kubectl describe Ingress -n iotdb-web
Name: example-ingress
Labels: <none>
Namespace: iotdb-web
Address: 10.90.11.63
Ingress Class: nginx
Default backend: <default>
TLS:
tls-secret terminates mail.hjj.com
Rules:
Host Path Backends
---- ---- --------
mail.hjj.com
/ iotdb-frontend-service:9090 (100.99.6.25:9090)
Annotations: nginx.ingress.kubernetes.io/proxy-body-size: 2000M
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 2m13s (x2 over 2m20s) nginx-ingress-controller Scheduled for sync
2、测试http是否成功
#查看svc端口
[root@k8s1 nginx-ingress]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.1.102 <none> 80:31905/TCP,443:30929/TCP 23m
ingress-nginx-controller-admission ClusterIP 10.96.0.118 <none> 443/TCP 23m
进入windows编辑C:\Windows\System32\drivers\etc\hosts

进入谷歌访问

使用阿里云免费个人证书,访问时连接是安全的
kubectl create secret tls hjj-secret --key /opt/hjj/nginx-ingress/www.hejiajun.asia.key --cert /opt/hjj/nginx-ingress/www.hejiajun.asia_bundle.crt -n iotdb-web #记得添加命名空间,如果使用免费ssl证书不加会不安全
修改ingress-https.yaml文件加载ssl证书
---
apiVersion: v1
kind: Service
metadata:
name: iotdb-frontend-service
namespace: iotdb-web
spec:
ports:
- port: 9090
targetPort: 9090
protocol: TCP
name: http
nodePort: 31095
selector:
app: iotdb-frontend
type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: iotdb-web
spec:
ingressClassName: nginx
tls:
- hosts:
- www.hejiajun.asia
secretName: hjj-secret
rules:
- host: www.hejiajun.asia
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: iotdb-frontend-service
port:
number: 9090
kubectl apply -f ingress-https.yaml #更新ssl

注意




更多推荐


所有评论(0)