作者:手机用户2502878113 | 来源:互联网 | 2023-06-29 21:31
kubernetesIngess是有2部分组成,IngressController和Ingress服务组成,常用的IngressController是ingress-nginx,工
kubernetes Ingess 是有2部分组成,Ingress Controller 和Ingress服务组成,常用的Ingress Controller 是ingress-nginx,工作的原理是:
Ingress Controller 会动态感知集群中的Ingress的规则变化,然后读取,动态生成Nginx的配置文件,最后注入到运行nginx的pod的中,然后会自动reload,配置生效。
用kubernetes Ingress 是由于它是7层调度,可以直接卸载https会话,代理的后端的pod可以直接使用明文的http协议。
而Service NodePort得类型,是4层得调度,做不到这点,然而现在https是一种趋势,所以在kubernetes 对外暴露服务得时候我们还是要选择Ingress。
下面我们来看下Ingress得部署:
首先创建一个文件夹专门放置Igress得yaml得文件,mkdir ingress
创建后端代理得pod得yaml文件,如下:
apiVersion: v1
kind: Service
metadata:
name: myapp-ding
namespace: default
spec:
selector:
app: myapp
release: ding
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-ding
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: ding
template:
metadata:
labels:
app: myapp
release: ding
spec:
containers:
- name: myapp-ding
image: ikubernetes/myapp:v2
ports:
- name: http
containerPort: 80
pod得yaml文件一定要有Service,
部署Ingress Controller
在kubernetes 得github上下载ingress得yaml文件,地址:https://github.com/kubernetes/ingress-nginx/tree/master/deploy
ingress得所需得文件:configmap.yaml ,namespace.yaml,rbac.yaml,tcp-services-configmap.yaml,with-rbac.yaml
同时在ingress得官方文档中介绍到,需要下载service-nodeport.yaml文件,这个文件得目的是为Ingress Controller 接入外部得流量,如果没有这个文件,是无法通过
外部访问得。这个文件其实就是为Ingress Controller 创建一个NodePort 类型得Service,这里我稍微修改了下service-nodeport.yaml,如下:
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
nodePort: 30080 添加了这行,固定下外部访问的端口
- name: https
port: 443
targetPort: 443
protocol: TCP
nodePort: 30443 添加了这行
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
开始安装,应用namespace.yaml kubectl apply -f namespace.yaml
其他得yaml为文件可以一起应用 cd ingress ,kubectl apply -f . 应用所有的文件
Ingress Controller 部署部署好了,现在要写ingress的规则,注入到ingress-nginx pod的配置文件中
vim ingress-myapp.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-ding
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx" 这里是说明ingress的类型使用的nginx,一定要说明这点,否则ingress Controller 不知道是配置成那种类型的配置文件
spec:
rules:
- host: test.ding.com 使用的是虚拟主机的来访问的
http:
paths:
- path:
backend:
serviceName: myapp-ding 代理的后端的pod的service,通过这个service来生成nginx的upstrm
servicePort: 80
kubectl apply -f ingress-myapp.yaml
访问的客户端的机器配置下域名解析
现在我们可以通过test.ding.com:30080来访问到后端代理的pod了
这里是使用http访问的,如果要用https,首先我们要创建一个证书,步骤如下
[root@master ingrss]# openssl genrsa -out tls.key 2048
Generating RSA private key, 2048 bit long modulus
.........................................................................................................................................................................................................................................................+++
..............................................................+++
e is 65537 (0x10001)
[root@master ingrss]# openssl req -new -x509 -key tls.key -out tls.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Hefei
Locality Name (eg, city) [Default City]:Hefei
Organization Name (eg, company) [Default Company Ltd]:test
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:test.ding.com
Email Address []:
证书生成好了,然后把证书转成secret,
kubectl create secret tls ding-ingress-secret --cert=tls.crt --key=tls.key
修改下 ingress-myapp.yaml 加入刚刚添加的secret,修改后的文件如下:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-ding
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
tls: 添加了tls这一段
- hosts:
- test.ding.com
secretName: ding-ingress-secret 这里结束
rules:
- host: test.ding.com
http:
paths:
- path:
backend:
serviceName: myapp-ding
servicePort: 80
现在我们可以通过https访问了
总结下,部署ingress,首先要部署下后端代理的pod,这组pod必须要有service,service的作用是用于ingress规则代理到后端pod的,通俗点就是这个service仅仅是给这组pod分组的,没有其他的左右。接着部署Ingress Controller,最后是写ingress的规则,让Ingress Controller 发现注入到ingress-nginx的pod中生成配置文件
最后补一张ingress-nginx pod里nignx的配置文件的图:
kubectl exec -n ingress-nginx -it nginx-ingress-controller-6dc8769b5-zljbw -- /bin/bash
好了,ingress部署完成,哪里有不对的地方希望各位朋友指出,大家相互学习!