ToB企服应用市场:ToB评测及商务社交产业平台

标题: kubernetes之一文详解Ingress;(一) [打印本页]

作者: 温锦文欧普厨电及净水器总代理    时间: 2022-9-16 17:25
标题: kubernetes之一文详解Ingress;(一)
1.为什么需要Ingress


2.什么是Ingress以及Ingress的资源规范

2.1什么是Ingress?

在kubernetes之中,Service资源和Pod资源的IP地址仅能用于集群网络的内部通信,所有的网络流量都无法穿透边界路由器(Edge Router)以实现集群内外通信,尽管可以使用NodePort或者LoadBalancer类型通过节点引入外部流量,但它依然是4层流量转发。
Ingress是kubernetes标准的资源类型,它主要用来定义流量转发规则,但是Ingress资源并不能实现流量的转发以及调度,它仅仅是一组流量路由的规则集合,这些规则要真正发挥作用还需要使用Ingress-Controller,由Ingress-Controller控制器读取对应的Ingress规则,然后完成流量的路由以及转发;
2.2什么是Ingress-Controller?

Ingress Controller可以由任何具有反向代理(HTTP/HTTPS)功能的服务程序实现,如Nginx、Traefik、Envoy、Haproxy。Ingress-Controller通过Pod的形式运行在kubernetes集群上,它能够与集群上的Pod直接通信,这样就可以让用户的流量经过Ingress-Controller直接调度到后端的Pod。
Ingress-Controller类似于Nginx服务,它负责读取Ingress的规则,然后转换将规则转换为nginx.conf配置文件。这样就可以根据对应的规则来实现流量的调度。同时它还会实时感知后端Serrvice对应Pod变化,当Pod发生变动后,Ingress-Controller会再次结合Ingress的规则,进而完成对应配置的动态更新。

需要注意的是:  使用Ingress资源进行流量分发的时候,Ingress-Controller可基于某个Ingress资源定义的规则将客户端的请求流量直接转发至与Service对应的后端的Pod资源之上。这种机制会绕过Service资源,从而省去了由kube-proxy实现的端口代理开销。实际的访问是Clinet--->Ingress Controller----->通过Ingress规则---->借助Service识别目标有哪些Pod。
2.3Ingress配置资源规范
  1. apiVersion: networking.k8s.io/v1   # 资源所属的API群组和版本
  2. kind: Ingress   # 资源类型
  3. metadata:   # 元数据
  4.      name:  <string>   # 资源名称
  5.          namespace: <string>  # 名称空间
  6. spec:     
  7.    ingressClassName: "nginx"   # 适用的Ingress控制器类别,须明确指明
  8.    rules: <[]object>    # Ingress规则列表
  9.    - host: <string>      # 虚拟主机的FQDN,俗称域名
  10.      http: <object>
  11.            paths: <[]object>      # 虚拟主机的PATH定义列表
  12.            - path: <string>    # 匹配以什么开头类似于nginx中的location
  13.                   pathType: <string>    # Prefix前缀匹配,不区分大小写 Exact。精确匹配URL,区分大小写
  14.                   backend: <object>     # 后端
  15.                       service: <object>   #关联后端的Service
  16.                               name: <string>   # 后端Service的名称
  17.                                   port: <object>   #后端Service的端口
  18.                                      name:      # 端口名称
  19.                                          number:   # 端口号
  20.                                   
复制代码
3.Ingress的安装与配置

3.1Ingress-Controller的安装;

Ingress-Controller的GitHub地址: https://github.com/kubernetes/ingress-nginx
Ingress-Nginx的annotaions: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#permanent-redirect
3.1.1下载配置文件
  1. root@ks-master01-10:~# wget https://github.com/kubernetes/ingress-nginx/blob/main/deploy/static/provider/kind/deploy.yaml
复制代码
3.1.2配置文件如下

有几处需要修改
  1. root@ks-master01-10:~# cat  deploy.yaml
  2. apiVersion: v1
  3. kind: Namespace
  4. metadata:
  5.   labels:
  6.     app.kubernetes.io/instance: ingress-nginx
  7.     app.kubernetes.io/name: ingress-nginx
  8.   name: ingress-nginx
  9. ---
  10. apiVersion: v1
  11. automountServiceAccountToken: true
  12. kind: ServiceAccount
  13. metadata:
  14.   labels:
  15.     app.kubernetes.io/component: controller
  16.     app.kubernetes.io/instance: ingress-nginx
  17.     app.kubernetes.io/name: ingress-nginx
  18.     app.kubernetes.io/part-of: ingress-nginx
  19.     app.kubernetes.io/version: 1.3.0
  20.   name: ingress-nginx
  21.   namespace: ingress-nginx
  22. ---
  23. apiVersion: v1
  24. kind: ServiceAccount
  25. metadata:
  26.   labels:
  27.     app.kubernetes.io/component: admission-webhook
  28.     app.kubernetes.io/instance: ingress-nginx
  29.     app.kubernetes.io/name: ingress-nginx
  30.     app.kubernetes.io/part-of: ingress-nginx
  31.     app.kubernetes.io/version: 1.3.0
  32.   name: ingress-nginx-admission
  33.   namespace: ingress-nginx
  34. ---
  35. apiVersion: rbac.authorization.k8s.io/v1
  36. kind: Role
  37. metadata:
  38.   labels:
  39.     app.kubernetes.io/component: controller
  40.     app.kubernetes.io/instance: ingress-nginx
  41.     app.kubernetes.io/name: ingress-nginx
  42.     app.kubernetes.io/part-of: ingress-nginx
  43.     app.kubernetes.io/version: 1.3.0
  44.   name: ingress-nginx
  45.   namespace: ingress-nginx
  46. rules:
  47. - apiGroups:
  48.   - ""
  49.   resources:
  50.   - namespaces
  51.   verbs:
  52.   - get
  53. - apiGroups:
  54.   - ""
  55.   resources:
  56.   - configmaps
  57.   - pods
  58.   - secrets
  59.   - endpoints
  60.   verbs:
  61.   - get
  62.   - list
  63.   - watch
  64. - apiGroups:
  65.   - ""
  66.   resources:
  67.   - services
  68.   verbs:
  69.   - get
  70.   - list
  71.   - watch
  72. - apiGroups:
  73.   - networking.k8s.io
  74.   resources:
  75.   - ingresses
  76.   verbs:
  77.   - get
  78.   - list
  79.   - watch
  80. - apiGroups:
  81.   - networking.k8s.io
  82.   resources:
  83.   - ingresses/status
  84.   verbs:
  85.   - update
  86. - apiGroups:
  87.   - networking.k8s.io
  88.   resources:
  89.   - ingressclasses
  90.   verbs:
  91.   - get
  92.   - list
  93.   - watch
  94. - apiGroups:
  95.   - ""
  96.   resourceNames:
  97.   - ingress-controller-leader
  98.   resources:
  99.   - configmaps
  100.   verbs:
  101.   - get
  102.   - update
  103. - apiGroups:
  104.   - ""
  105.   resources:
  106.   - configmaps
  107.   verbs:
  108.   - create
  109. - apiGroups:
  110.   - coordination.k8s.io
  111.   resourceNames:
  112.   - ingress-controller-leader
  113.   resources:
  114.   - leases
  115.   verbs:
  116.   - get
  117.   - update
  118. - apiGroups:
  119.   - coordination.k8s.io
  120.   resources:
  121.   - leases
  122.   verbs:
  123.   - create
  124. - apiGroups:
  125.   - ""
  126.   resources:
  127.   - events
  128.   verbs:
  129.   - create
  130.   - patch
  131. ---
  132. apiVersion: rbac.authorization.k8s.io/v1
  133. kind: Role
  134. metadata:
  135.   labels:
  136.     app.kubernetes.io/component: admission-webhook
  137.     app.kubernetes.io/instance: ingress-nginx
  138.     app.kubernetes.io/name: ingress-nginx
  139.     app.kubernetes.io/part-of: ingress-nginx
  140.     app.kubernetes.io/version: 1.3.0
  141.   name: ingress-nginx-admission
  142.   namespace: ingress-nginx
  143. rules:
  144. - apiGroups:
  145.   - ""
  146.   resources:
  147.   - secrets
  148.   verbs:
  149.   - get
  150.   - create
  151. ---
  152. apiVersion: rbac.authorization.k8s.io/v1
  153. kind: ClusterRole
  154. metadata:
  155.   labels:
  156.     app.kubernetes.io/instance: ingress-nginx
  157.     app.kubernetes.io/name: ingress-nginx
  158.     app.kubernetes.io/part-of: ingress-nginx
  159.     app.kubernetes.io/version: 1.3.0
  160.   name: ingress-nginx
  161. rules:
  162. - apiGroups:
  163.   - ""
  164.   resources:
  165.   - configmaps
  166.   - endpoints
  167.   - nodes
  168.   - pods
  169.   - secrets
  170.   - namespaces
  171.   verbs:
  172.   - list
  173.   - watch
  174. - apiGroups:
  175.   - coordination.k8s.io
  176.   resources:
  177.   - leases
  178.   verbs:
  179.   - list
  180.   - watch
  181. - apiGroups:
  182.   - ""
  183.   resources:
  184.   - nodes
  185.   verbs:
  186.   - get
  187. - apiGroups:
  188.   - ""
  189.   resources:
  190.   - services
  191.   verbs:
  192.   - get
  193.   - list
  194.   - watch
  195. - apiGroups:
  196.   - networking.k8s.io
  197.   resources:
  198.   - ingresses
  199.   verbs:
  200.   - get
  201.   - list
  202.   - watch
  203. - apiGroups:
  204.   - ""
  205.   resources:
  206.   - events
  207.   verbs:
  208.   - create
  209.   - patch
  210. - apiGroups:
  211.   - networking.k8s.io
  212.   resources:
  213.   - ingresses/status
  214.   verbs:
  215.   - update
  216. - apiGroups:
  217.   - networking.k8s.io
  218.   resources:
  219.   - ingressclasses
  220.   verbs:
  221.   - get
  222.   - list
  223.   - watch
  224. ---
  225. apiVersion: rbac.authorization.k8s.io/v1
  226. kind: ClusterRole
  227. metadata:
  228.   labels:
  229.     app.kubernetes.io/component: admission-webhook
  230.     app.kubernetes.io/instance: ingress-nginx
  231.     app.kubernetes.io/name: ingress-nginx
  232.     app.kubernetes.io/part-of: ingress-nginx
  233.     app.kubernetes.io/version: 1.3.0
  234.   name: ingress-nginx-admission
  235. rules:
  236. - apiGroups:
  237.   - admissionregistration.k8s.io
  238.   resources:
  239.   - validatingwebhookconfigurations
  240.   verbs:
  241.   - get
  242.   - update
  243. ---
  244. apiVersion: rbac.authorization.k8s.io/v1
  245. kind: RoleBinding
  246. metadata:
  247.   labels:
  248.     app.kubernetes.io/component: controller
  249.     app.kubernetes.io/instance: ingress-nginx
  250.     app.kubernetes.io/name: ingress-nginx
  251.     app.kubernetes.io/part-of: ingress-nginx
  252.     app.kubernetes.io/version: 1.3.0
  253.   name: ingress-nginx
  254.   namespace: ingress-nginx
  255. roleRef:
  256.   apiGroup: rbac.authorization.k8s.io
  257.   kind: Role
  258.   name: ingress-nginx
  259. subjects:
  260. - kind: ServiceAccount
  261.   name: ingress-nginx
  262.   namespace: ingress-nginx
  263. ---
  264. apiVersion: rbac.authorization.k8s.io/v1
  265. kind: RoleBinding
  266. metadata:
  267.   labels:
  268.     app.kubernetes.io/component: admission-webhook
  269.     app.kubernetes.io/instance: ingress-nginx
  270.     app.kubernetes.io/name: ingress-nginx
  271.     app.kubernetes.io/part-of: ingress-nginx
  272.     app.kubernetes.io/version: 1.3.0
  273.   name: ingress-nginx-admission
  274.   namespace: ingress-nginx
  275. roleRef:
  276.   apiGroup: rbac.authorization.k8s.io
  277.   kind: Role
  278.   name: ingress-nginx-admission
  279. subjects:
  280. - kind: ServiceAccount
  281.   name: ingress-nginx-admission
  282.   namespace: ingress-nginx
  283. ---
  284. apiVersion: rbac.authorization.k8s.io/v1
  285. kind: ClusterRoleBinding
  286. metadata:
  287.   labels:
  288.     app.kubernetes.io/instance: ingress-nginx
  289.     app.kubernetes.io/name: ingress-nginx
  290.     app.kubernetes.io/part-of: ingress-nginx
  291.     app.kubernetes.io/version: 1.3.0
  292.   name: ingress-nginx
  293. roleRef:
  294.   apiGroup: rbac.authorization.k8s.io
  295.   kind: ClusterRole
  296.   name: ingress-nginx
  297. subjects:
  298. - kind: ServiceAccount
  299.   name: ingress-nginx
  300.   namespace: ingress-nginx
  301. ---
  302. apiVersion: rbac.authorization.k8s.io/v1
  303. kind: ClusterRoleBinding
  304. metadata:
  305.   labels:
  306.     app.kubernetes.io/component: admission-webhook
  307.     app.kubernetes.io/instance: ingress-nginx
  308.     app.kubernetes.io/name: ingress-nginx
  309.     app.kubernetes.io/part-of: ingress-nginx
  310.     app.kubernetes.io/version: 1.3.0
  311.   name: ingress-nginx-admission
  312. roleRef:
  313.   apiGroup: rbac.authorization.k8s.io
  314.   kind: ClusterRole
  315.   name: ingress-nginx-admission
  316. subjects:
  317. - kind: ServiceAccount
  318.   name: ingress-nginx-admission
  319.   namespace: ingress-nginx
  320. ---
  321. apiVersion: v1
  322. data:
  323.   allow-snippet-annotations: "true"
  324. kind: ConfigMap
  325. metadata:
  326.   labels:
  327.     app.kubernetes.io/component: controller
  328.     app.kubernetes.io/instance: ingress-nginx
  329.     app.kubernetes.io/name: ingress-nginx
  330.     app.kubernetes.io/part-of: ingress-nginx
  331.     app.kubernetes.io/version: 1.3.0
  332.   name: ingress-nginx-controller
  333.   namespace: ingress-nginx
  334. ---
  335. apiVersion: v1
  336. kind: Service
  337. metadata:
  338.   labels:
  339.     app.kubernetes.io/component: controller
  340.     app.kubernetes.io/instance: ingress-nginx
  341.     app.kubernetes.io/name: ingress-nginx
  342.     app.kubernetes.io/part-of: ingress-nginx
  343.     app.kubernetes.io/version: 1.3.0
  344.   name: ingress-nginx-controller
  345.   namespace: ingress-nginx
  346. spec:
  347.   ipFamilies:
  348.   - IPv4
  349.   ipFamilyPolicy: SingleStack
  350.   ports:
  351.   - appProtocol: http
  352.     name: http
  353.     port: 80
  354.     protocol: TCP
  355.     targetPort: http
  356.   - appProtocol: https
  357.     name: https
  358.     port: 443
  359.     protocol: TCP
  360.     targetPort: https
  361.   selector:
  362.     app.kubernetes.io/component: controller
  363.     app.kubernetes.io/instance: ingress-nginx
  364.     app.kubernetes.io/name: ingress-nginx
  365.   type: NodePort    # 修改为ClusterIP
  366. ---
  367. apiVersion: v1
  368. kind: Service
  369. metadata:
  370.   labels:
  371.     app.kubernetes.io/component: controller
  372.     app.kubernetes.io/instance: ingress-nginx
  373.     app.kubernetes.io/name: ingress-nginx
  374.     app.kubernetes.io/part-of: ingress-nginx
  375.     app.kubernetes.io/version: 1.3.0
  376.   name: ingress-nginx-controller-admission
  377.   namespace: ingress-nginx
  378. spec:
  379.   ports:
  380.   - appProtocol: https
  381.     name: https-webhook
  382.     port: 443
  383.     targetPort: webhook
  384.   selector:
  385.     app.kubernetes.io/component: controller
  386.     app.kubernetes.io/instance: ingress-nginx
  387.     app.kubernetes.io/name: ingress-nginx
  388.   type: ClusterIP
  389. ---
  390. apiVersion: apps/v1
  391. kind: Deployment   #  Deployment需替换为DaemonSet。使用DaemonSet确保每个节点都部署;
  392. metadata:
  393.   labels:
  394.     app.kubernetes.io/component: controller
  395.     app.kubernetes.io/instance: ingress-nginx
  396.     app.kubernetes.io/name: ingress-nginx
  397.     app.kubernetes.io/part-of: ingress-nginx
  398.     app.kubernetes.io/version: 1.3.0
  399.   name: ingress-nginx-controller
  400.   namespace: ingress-nginx
  401. spec:
  402.   minReadySeconds: 0
  403.   revisionHistoryLimit: 10
  404.   selector:
  405.     matchLabels:
  406.       app.kubernetes.io/component: controller
  407.       app.kubernetes.io/instance: ingress-nginx
  408.       app.kubernetes.io/name: ingress-nginx
  409.   strategy:
  410.     rollingUpdate:
  411.       maxUnavailable: 1
  412.     type: RollingUpdate
  413.   template:
  414.     metadata:
  415.       labels:
  416.         app.kubernetes.io/component: controller
  417.         app.kubernetes.io/instance: ingress-nginx
  418.         app.kubernetes.io/name: ingress-nginx
  419.     spec:
  420.       containers:
  421.       - args:
  422.         - /nginx-ingress-controller
  423.         - --election-id=ingress-controller-leader
  424.         - --controller-class=k8s.io/ingress-nginx
  425.         - --ingress-class=nginx
  426.         - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
  427.         - --validating-webhook=:8443
  428.         - --validating-webhook-certificate=/usr/local/certificates/cert
  429.         - --validating-webhook-key=/usr/local/certificates/key
  430.         - --watch-ingress-without-class=true
  431.         - --publish-status-address=localhost
  432.         env:
  433.         - name: POD_NAME
  434.           valueFrom:
  435.             fieldRef:
  436.               fieldPath: metadata.name
  437.         - name: POD_NAMESPACE
  438.           valueFrom:
  439.             fieldRef:
  440.               fieldPath: metadata.namespace
  441.         - name: LD_PRELOAD
  442.           value: /usr/local/lib/libmimalloc.so
  443.         image: registry.k8s.io/ingress-nginx/controller:v1.3.0@sha256:d1707ca76d3b044ab8a28277a2466a02100ee9f58a86af1535a3edf9323ea1b5
  444.         imagePullPolicy: IfNotPresent
  445.         lifecycle:
  446.           preStop:
  447.             exec:
  448.               command:
  449.               - /wait-shutdown
  450.         livenessProbe:
  451.           failureThreshold: 5
  452.           httpGet:
  453.             path: /healthz
  454.             port: 10254
  455.             scheme: HTTP
  456.           initialDelaySeconds: 10
  457.           periodSeconds: 10
  458.           successThreshold: 1
  459.           timeoutSeconds: 1
  460.         name: controller
  461.         ports:
  462.         - containerPort: 80
  463.           hostPort: 80
  464.           name: http
  465.           protocol: TCP
  466.         - containerPort: 443
  467.           hostPort: 443
  468.           name: https
  469.           protocol: TCP
  470.         - containerPort: 8443
  471.           name: webhook
  472.           protocol: TCP
  473.         readinessProbe:
  474.           failureThreshold: 3
  475.           httpGet:
  476.             path: /healthz
  477.             port: 10254
  478.             scheme: HTTP
  479.           initialDelaySeconds: 10
  480.           periodSeconds: 10
  481.           successThreshold: 1
  482.           timeoutSeconds: 1
  483.         resources:
  484.           requests:
  485.             cpu: 100m
  486.             memory: 90Mi
  487.         securityContext:
  488.           allowPrivilegeEscalation: true
  489.           capabilities:
  490.             add:
  491.             - NET_BIND_SERVICE
  492.             drop:
  493.             - ALL
  494.           runAsUser: 101
  495.         volumeMounts:
  496.         - mountPath: /usr/local/certificates/
  497.           name: webhook-cert
  498.           readOnly: true
  499.       dnsPolicy: ClusterFirst   # 替换为优先使用集群内的DNS解析服务
  500.            hostNetwork: true     # 自行添加,80和443监听在宿主机节点
  501.       nodeSelector:       # 节点选择器,(选择哪些节点部署Ingress默认所有)
  502.         ingress-ready: "true"
  503.         kubernetes.io/os: linux
  504.       serviceAccountName: ingress-nginx
  505.       terminationGracePeriodSeconds: 0
  506.       tolerations:
  507.       - effect: NoSchedule
  508.         key: node-role.kubernetes.io/master
  509.         operator: Equal
  510.       - effect: NoSchedule
  511.         key: node-role.kubernetes.io/control-plane
  512.         operator: Equal
  513.       volumes:
  514.       - name: webhook-cert
  515.         secret:
  516.           secretName: ingress-nginx-admission
  517. ---
  518. apiVersion: batch/v1
  519. kind: Job
  520. metadata:
  521.   labels:
  522.     app.kubernetes.io/component: admission-webhook
  523.     app.kubernetes.io/instance: ingress-nginx
  524.     app.kubernetes.io/name: ingress-nginx
  525.     app.kubernetes.io/part-of: ingress-nginx
  526.     app.kubernetes.io/version: 1.3.0
  527.   name: ingress-nginx-admission-create
  528.   namespace: ingress-nginx
  529. spec:
  530.   template:
  531.     metadata:
  532.       labels:
  533.         app.kubernetes.io/component: admission-webhook
  534.         app.kubernetes.io/instance: ingress-nginx
  535.         app.kubernetes.io/name: ingress-nginx
  536.         app.kubernetes.io/part-of: ingress-nginx
  537.         app.kubernetes.io/version: 1.3.0
  538.       name: ingress-nginx-admission-create
  539.     spec:
  540.       containers:
  541.       - args:
  542.         - create
  543.         - --host=ingress-nginx-controller-admission,ingress-nginx-controller-admission.$(POD_NAMESPACE).svc
  544.         - --namespace=$(POD_NAMESPACE)
  545.         - --secret-name=ingress-nginx-admission
  546.         env:
  547.         - name: POD_NAMESPACE
  548.           valueFrom:
  549.             fieldRef:
  550.               fieldPath: metadata.namespace
  551.         image: registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.3.0@sha256:549e71a6ca248c5abd51cdb73dbc3083df62cf92ed5e6147c780e30f7e007a47
  552.         imagePullPolicy: IfNotPresent
  553.         name: create
  554.         securityContext:
  555.           allowPrivilegeEscalation: false
  556.       nodeSelector:
  557.         kubernetes.io/os: linux
  558.       restartPolicy: OnFailure
  559.       securityContext:
  560.         fsGroup: 2000
  561.         runAsNonRoot: true
  562.         runAsUser: 2000
  563.       serviceAccountName: ingress-nginx-admission
  564. ---
  565. apiVersion: batch/v1
  566. kind: Job
  567. metadata:
  568.   labels:
  569.     app.kubernetes.io/component: admission-webhook
  570.     app.kubernetes.io/instance: ingress-nginx
  571.     app.kubernetes.io/name: ingress-nginx
  572.     app.kubernetes.io/part-of: ingress-nginx
  573.     app.kubernetes.io/version: 1.3.0
  574.   name: ingress-nginx-admission-patch
  575.   namespace: ingress-nginx
  576. spec:
  577.   template:
  578.     metadata:
  579.       labels:
  580.         app.kubernetes.io/component: admission-webhook
  581.         app.kubernetes.io/instance: ingress-nginx
  582.         app.kubernetes.io/name: ingress-nginx
  583.         app.kubernetes.io/part-of: ingress-nginx
  584.         app.kubernetes.io/version: 1.3.0
  585.       name: ingress-nginx-admission-patch
  586.     spec:
  587.       containers:
  588.       - args:
  589.         - patch
  590.         - --webhook-name=ingress-nginx-admission
  591.         - --namespace=$(POD_NAMESPACE)
  592.         - --patch-mutating=false
  593.         - --secret-name=ingress-nginx-admission
  594.         - --patch-failure-policy=Fail
  595.         env:
  596.         - name: POD_NAMESPACE
  597.           valueFrom:
  598.             fieldRef:
  599.               fieldPath: metadata.namespace
  600.         image: registry.k8s.io/ingress-nginx/kube-webhook-certgen:v1.3.0@sha256:549e71a6ca248c5abd51cdb73dbc3083df62cf92ed5e6147c780e30f7e007a47
  601.         imagePullPolicy: IfNotPresent
  602.         name: patch
  603.         securityContext:
  604.           allowPrivilegeEscalation: false
  605.       nodeSelector:
  606.         kubernetes.io/os: linux
  607.       restartPolicy: OnFailure
  608.       securityContext:
  609.         fsGroup: 2000
  610.         runAsNonRoot: true
  611.         runAsUser: 2000
  612.       serviceAccountName: ingress-nginx-admission
  613. ---
  614. apiVersion: networking.k8s.io/v1
  615. kind: IngressClass
  616. metadata:
  617.   labels:
  618.     app.kubernetes.io/component: controller
  619.     app.kubernetes.io/instance: ingress-nginx
  620.     app.kubernetes.io/name: ingress-nginx
  621.     app.kubernetes.io/part-of: ingress-nginx
  622.     app.kubernetes.io/version: 1.3.0
  623.   name: nginx
  624. spec:
  625.   controller: k8s.io/ingress-nginx
  626. ---
  627. apiVersion: admissionregistration.k8s.io/v1
  628. kind: ValidatingWebhookConfiguration
  629. metadata:
  630.   labels:
  631.     app.kubernetes.io/component: admission-webhook
  632.     app.kubernetes.io/instance: ingress-nginx
  633.     app.kubernetes.io/name: ingress-nginx
  634.     app.kubernetes.io/part-of: ingress-nginx
  635.     app.kubernetes.io/version: 1.3.0
  636.   name: ingress-nginx-admission
  637. webhooks:
  638. - admissionReviewVersions:
  639.   - v1
  640.   clientConfig:
  641.     service:
  642.       name: ingress-nginx-controller-admission
  643.       namespace: ingress-nginx
  644.       path: /networking/v1/ingresses
  645.   failurePolicy: Fail
  646.   matchPolicy: Equivalent
  647.   name: validate.nginx.ingress.kubernetes.io
  648.   rules:
  649.   - apiGroups:
  650.     - networking.k8s.io
  651.     apiVersions:
  652.     - v1
  653.     operations:
  654.     - CREATE
  655.     - UPDATE
  656.     resources:
  657.     - ingresses
  658.   sideEffects: None
  659. root@ks-master01-10:~#  kubectl apply -f deploy.yaml
复制代码
3.1.3查看Ingress-Controller运行是否正常

Pod运行正常,
  1. root@ks-master01-10:~# kubectl get pods -n ingress-nginx
  2. NAME                                      READY   STATUS      RESTARTS      AGE
  3. ingress-nginx-admission-create--1-hcfr2   0/1     Completed   0             2d23h
  4. ingress-nginx-admission-patch--1-6tdvp    0/1     Completed   0             2d23h
  5. ingress-nginx-controller-jtwc6            1/1     Running     4 (18h ago)   2d23h
  6. ingress-nginx-controller-x9psq            1/1     Running     4 (18h ago)   2d23h
复制代码
3.1.4查看Service
  1. root@ks-master01-10:~# kubectl get svc
  2. NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
  3. kubernetes   ClusterIP   10.96.0.x      <none>        443/TCP   14d
  4. nginx-svc    ClusterIP   10.96.xx.102   <none>        80/TCP    23m
复制代码
3.2使用Ingress发布应用

条件1: 创建应用
条件2: 创建Service
条件3: 创建Ingress资源
3.2.1创建应用
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   name: nginx-deploy
  5.   namespace: default
  6. spec:
  7.   replicas: 2
  8.   selector:
  9.     matchLabels:
  10.       app: nginx-test
  11.   template:
  12.     metadata:
  13.       labels:
  14.         app: nginx-test
  15.     spec:
  16.       containers:
  17.       - name: nginx
  18.         image: nginx:1.16
  19.         imagePullPolicy: IfNotPresent
  20.         ports:
  21.         - name: http
  22.           containerPort: 80
复制代码
查看Pod
  1. root@ks-master01-10:~# kubectl get pods -l app=nginx-test
  2. NAME                            READY   STATUS    RESTARTS   AGE
  3. nginx-deploy-86cf6979bd-b6fld   1/1     Running   0          2m3s
  4. nginx-deploy-86cf6979bd-pz4s8   1/1     Running   0          2m3s
复制代码
3.2.2创建Service

Ingress资源仅通过Service资源识别相应的Pod资源获取其IP和端口,而后Ingress控制器即可直接使用各Pod对象的IP地址与它直接进行通信,而不经由Service资源代理和调度,因此Service资源的ClusterIP对Ingress控制器一无所用,不过若集群内的其他Pod客户端需要与其通信,那么ClusterIP还是很有必要;
  1. root@ks-master01-10:~# cat nginx-deploy-svc.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5.   name: nginx-svc
  6.   namespace: default
  7. spec:
  8.   selector:
  9.     app: nginx-test
  10.   ports:
  11.   - port: 80
  12.     targetPort: 80
  13. root@ks-master01-10:~# kubectl apply -f nginx-deploy-svc.yaml
  14. service/nginx-svc created
复制代码
查看Service
  1. root@ks-master01-10:~# kubectl get svc
  2. NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
  3. nginx-svc    ClusterIP   10.96.79.102   <none>        80/TCP    4s
复制代码
可以看到后端的端点是2个nginx的Pod
  1. root@ks-master01-10:~# kubectl describe svc nginx-svc
  2. Name:              nginx-svc
  3. Namespace:         default
  4. Labels:            <none>
  5. Annotations:       <none>
  6. Selector:          app=nginx-test
  7. Type:              ClusterIP
  8. IP Family Policy:  SingleStack
  9. IP Families:       IPv4
  10. IP:                10.96.79.102
  11. IPs:               10.96.79.102
  12. Port:              <unset>  80/TCP
  13. TargetPort:        80/TCP
  14. Endpoints:         192.168.1.72:80,192.168.2.21:80
  15. Session Affinity:  None
复制代码
3.2.3集群内测试

因为是ClusterIP,在集群内访问测试是没有问题的;
  1. root@ks-master01-10:~# curl -I 10.96.79.102
  2. HTTP/1.1 200 OK
  3. Server: nginx/1.16.1
  4. Date: Wed, 24 Aug 2022 05:06:42 GMT
  5. Content-Type: text/html
  6. Content-Length: 612
  7. Last-Modified: Tue, 13 Aug 2019 10:05:00 GMT
  8. Connection: keep-alive
  9. ETag: "5d528b4c-264"
  10. Accept-Ranges: bytesÍ
复制代码
3.2.4创建Ingress资源发布应用;

通过Ingrees资源的FQDN主机或者URL的路径等类似发布的服务,只有用户的访问请求能够匹配到其.spec.rules.host字段定义的主机才能被相应的规则所处理;
  1. root@ks-master01-10:~# vim nginx-deploy-ingress.yaml
  2. apiVersion: networking.k8s.io/v1
  3. kind: Ingress
  4. metadata:
  5.   name: nginx-test
  6.   namespace: default
  7. spec:
  8.   ingressClassName: "nginx"
  9.   rules:
  10.   - host: haitang.nginx.com
  11.     http:
  12.       paths:
  13.       - path: /
  14.         pathType: Prefix
  15.         backend:
  16.           service:
  17.             name: nginx-svc
  18.             port:
  19.               number: 80
  20. root@ks-master01-10:~# kubectl apply -f nginx-deploy-ingress.yaml
  21. ingress.networking.k8s.io/nginx-test created
  22. # 可以已经将Pod识别为后端的端点;
  23. root@ks-master01-10:~# kubectl describe ingress nginx-test
  24. Name:             nginx-test
  25. Namespace:        default
  26. Address:         
  27. Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
  28. Rules:
  29.   Host               Path  Backends
  30.   ----               ----  --------
  31.   haitang.nginx.com  
  32.                      /   nginx-svc:80 (192.168.1.72:80,192.168.2.21:80)
  33. Annotations:         <none>
  34. Events:
  35.   Type    Reason  Age   From                      Message
  36.   ----    ------  ----  ----                      -------
  37.   Normal  Sync    8s    nginx-ingress-controller  Scheduled for sync
  38.   Normal  Sync    8s    nginx-ingress-controller  Scheduled for sync
  39.   
  40. # 查看Ingress资源
  41. root@ks-master01-10:~# kubectl get ingress
  42. NAME         CLASS   HOSTS               ADDRESS               PORTS   AGE
  43. nginx-test   nginx   haitang.nginx.com   xx.xx.xx.xx     80      13m
复制代码
3.2.5集群外测试访问;

需配置/etc/hosts访问即可;
  1. sh-3.2# curl -I  haitang.nginx.com
  2. HTTP/1.1 200 OK
  3. Date: Wed, 24 Aug 2022 05:04:15 GMT
  4. Content-Type: text/html
  5. Content-Length: 612
  6. Connection: keep-alive
  7. Last-Modified: Tue, 13 Aug 2019 10:05:00 GMT
  8. ETag: "5d528b4c-264"
  9. Accept-Ranges: bytes
复制代码
当然网页访问也是没有问题


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4