四层转发:tcp/ip:端口
七层转发:http/https

service实现四层转发
ingress实现七层转发

ingress 是七层转发,会根据请求的主机名以及路径来决定把请求转发到相应的服务
clinet—ingress—url—service—(endpoint)—pod

ingress.yaml

  1. api
  2. kind
  3. metadata
  4. spec
  5. rules
  6. - host
  7. http
  8. ports
  9. - backend
  10. tls
  11. - hosts
  12. secretName
  13. status
  14. loadBalancer
  15. ingress

Service-NodePort

Service的NodePort方式是把端口号对外暴露,通过ip+端口号进行访问

Service-NodePort缺陷

在每个节点上都会起个端口,在访问时候通过任务节点,通过节点Ip+暴露端口实现访问

意味着每个端口号只能使用一次,一个端口对应一个应用

实际访问中都是用域名,根据不同域名跳转到不同端口服务中,ingress就是实现这个目标的

Ingress和Pod的关系

图片.png

Ingress工作流程

图片.png

部署ingress Controller,实现域名访问

vi ingress-controller.yaml

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: ingress-nginx
  5. labels:
  6. app.kubernetes.io/name: ingress-nginx
  7. app.kubernetes.io/part-of: ingress-nginx
  8. ---
  9. kind: ConfigMap
  10. apiVersion: v1
  11. metadata:
  12. name: nginx-configuration
  13. namespace: ingress-nginx
  14. labels:
  15. app.kubernetes.io/name: ingress-nginx
  16. app.kubernetes.io/part-of: ingress-nginx
  17. ---
  18. kind: ConfigMap
  19. apiVersion: v1
  20. metadata:
  21. name: tcp-services
  22. namespace: ingress-nginx
  23. labels:
  24. app.kubernetes.io/name: ingress-nginx
  25. app.kubernetes.io/part-of: ingress-nginx
  26. ---
  27. kind: ConfigMap
  28. apiVersion: v1
  29. metadata:
  30. name: udp-services
  31. namespace: ingress-nginx
  32. labels:
  33. app.kubernetes.io/name: ingress-nginx
  34. app.kubernetes.io/part-of: ingress-nginx
  35. ---
  36. apiVersion: v1
  37. kind: ServiceAccount
  38. metadata:
  39. name: nginx-ingress-serviceaccount
  40. namespace: ingress-nginx
  41. labels:
  42. app.kubernetes.io/name: ingress-nginx
  43. app.kubernetes.io/part-of: ingress-nginx
  44. ---
  45. apiVersion: rbac.authorization.k8s.io/v1beta1
  46. kind: ClusterRole
  47. metadata:
  48. name: nginx-ingress-clusterrole
  49. labels:
  50. app.kubernetes.io/name: ingress-nginx
  51. app.kubernetes.io/part-of: ingress-nginx
  52. rules:
  53. - apiGroups:
  54. - ""
  55. resources:
  56. - configmaps
  57. - endpoints
  58. - nodes
  59. - pods
  60. - secrets
  61. verbs:
  62. - list
  63. - watch
  64. - apiGroups:
  65. - ""
  66. resources:
  67. - nodes
  68. verbs:
  69. - get
  70. - apiGroups:
  71. - ""
  72. resources:
  73. - services
  74. verbs:
  75. - get
  76. - list
  77. - watch
  78. - apiGroups:
  79. - ""
  80. resources:
  81. - events
  82. verbs:
  83. - create
  84. - patch
  85. - apiGroups:
  86. - "extensions"
  87. - "networking.k8s.io"
  88. resources:
  89. - ingresses
  90. verbs:
  91. - get
  92. - list
  93. - watch
  94. - apiGroups:
  95. - "extensions"
  96. - "networking.k8s.io"
  97. resources:
  98. - ingresses/status
  99. verbs:
  100. - update
  101. ---
  102. apiVersion: rbac.authorization.k8s.io/v1beta1
  103. kind: Role
  104. metadata:
  105. name: nginx-ingress-role
  106. namespace: ingress-nginx
  107. labels:
  108. app.kubernetes.io/name: ingress-nginx
  109. app.kubernetes.io/part-of: ingress-nginx
  110. rules:
  111. - apiGroups:
  112. - ""
  113. resources:
  114. - configmaps
  115. - pods
  116. - secrets
  117. - namespaces
  118. verbs:
  119. - get
  120. - apiGroups:
  121. - ""
  122. resources:
  123. - configmaps
  124. resourceNames:
  125. # Defaults to "<election-id>-<ingress-class>"
  126. # Here: "<ingress-controller-leader>-<nginx>"
  127. # This has to be adapted if you change either parameter
  128. # when launching the nginx-ingress-controller.
  129. - "ingress-controller-leader-nginx"
  130. verbs:
  131. - get
  132. - update
  133. - apiGroups:
  134. - ""
  135. resources:
  136. - configmaps
  137. verbs:
  138. - create
  139. - apiGroups:
  140. - ""
  141. resources:
  142. - endpoints
  143. verbs:
  144. - get
  145. ---
  146. apiVersion: rbac.authorization.k8s.io/v1beta1
  147. kind: RoleBinding
  148. metadata:
  149. name: nginx-ingress-role-nisa-binding
  150. namespace: ingress-nginx
  151. labels:
  152. app.kubernetes.io/name: ingress-nginx
  153. app.kubernetes.io/part-of: ingress-nginx
  154. roleRef:
  155. apiGroup: rbac.authorization.k8s.io
  156. kind: Role
  157. name: nginx-ingress-role
  158. subjects:
  159. - kind: ServiceAccount
  160. name: nginx-ingress-serviceaccount
  161. namespace: ingress-nginx
  162. ---
  163. apiVersion: rbac.authorization.k8s.io/v1beta1
  164. kind: ClusterRoleBinding
  165. metadata:
  166. name: nginx-ingress-clusterrole-nisa-binding
  167. labels:
  168. app.kubernetes.io/name: ingress-nginx
  169. app.kubernetes.io/part-of: ingress-nginx
  170. roleRef:
  171. apiGroup: rbac.authorization.k8s.io
  172. kind: ClusterRole
  173. name: nginx-ingress-clusterrole
  174. subjects:
  175. - kind: ServiceAccount
  176. name: nginx-ingress-serviceaccount
  177. namespace: ingress-nginx
  178. ---
  179. apiVersion: apps/v1
  180. kind: Deployment
  181. metadata:
  182. name: nginx-ingress-controller
  183. namespace: ingress-nginx
  184. labels:
  185. app.kubernetes.io/name: ingress-nginx
  186. app.kubernetes.io/part-of: ingress-nginx
  187. spec:
  188. replicas: 1
  189. selector:
  190. matchLabels:
  191. app.kubernetes.io/name: ingress-nginx
  192. app.kubernetes.io/part-of: ingress-nginx
  193. template:
  194. metadata:
  195. labels:
  196. app.kubernetes.io/name: ingress-nginx
  197. app.kubernetes.io/part-of: ingress-nginx
  198. annotations:
  199. prometheus.io/port: "10254"
  200. prometheus.io/scrape: "true"
  201. spec:
  202. hostNetwork: true
  203. # wait up to five minutes for the drain of connections
  204. terminationGracePeriodSeconds: 300
  205. serviceAccountName: nginx-ingress-serviceaccount
  206. nodeSelector:
  207. kubernetes.io/os: linux
  208. containers:
  209. - name: nginx-ingress-controller
  210. image: lizhenliang/nginx-ingress-controller:0.30.0
  211. args:
  212. - /nginx-ingress-controller
  213. - --configmap=$(POD_NAMESPACE)/nginx-configuration
  214. - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
  215. - --udp-services-configmap=$(POD_NAMESPACE)/udp-services
  216. - --publish-service=$(POD_NAMESPACE)/ingress-nginx
  217. - --annotations-prefix=nginx.ingress.kubernetes.io
  218. securityContext:
  219. allowPrivilegeEscalation: true
  220. capabilities:
  221. drop:
  222. - ALL
  223. add:
  224. - NET_BIND_SERVICE
  225. # www-data -> 101
  226. runAsUser: 101
  227. env:
  228. - name: POD_NAME
  229. valueFrom:
  230. fieldRef:
  231. fieldPath: metadata.name
  232. - name: POD_NAMESPACE
  233. valueFrom:
  234. fieldRef:
  235. fieldPath: metadata.namespace
  236. ports:
  237. - name: http
  238. containerPort: 80
  239. protocol: TCP
  240. - name: https
  241. containerPort: 443
  242. protocol: TCP
  243. livenessProbe:
  244. failureThreshold: 3
  245. httpGet:
  246. path: /healthz
  247. port: 10254
  248. scheme: HTTP
  249. initialDelaySeconds: 10
  250. periodSeconds: 10
  251. successThreshold: 1
  252. timeoutSeconds: 10
  253. readinessProbe:
  254. failureThreshold: 3
  255. httpGet:
  256. path: /healthz
  257. port: 10254
  258. scheme: HTTP
  259. periodSeconds: 10
  260. successThreshold: 1
  261. timeoutSeconds: 10
  262. lifecycle:
  263. preStop:
  264. exec:
  265. command:
  266. - /wait-shutdown
  267. ---
  268. apiVersion: v1
  269. kind: LimitRange
  270. metadata:
  271. name: ingress-nginx
  272. namespace: ingress-nginx
  273. labels:
  274. app.kubernetes.io/name: ingress-nginx
  275. app.kubernetes.io/part-of: ingress-nginx
  276. spec:
  277. limits:
  278. - min:
  279. memory: 90Mi
  280. cpu: 100m
  281. type: Container

kubectl apply -f ingress-controller.yaml
kubectl get pods -n ingress-nginx

创建ingress规则

要访问到对应的service,还得配置相应的规则 rules
# vi ingress-http.yaml

  1. apiVersion: networking.k8s.io/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: example-ingress
  5. spec:
  6. rules:
  7. - host: example.ingredemo.com
  8. http:
  9. paths:
  10. - path: /
  11. backend:
  12. serviceName: web
  13. servicePort: 80

kubectl apply -f ingress-http.yaml
kubectl get pods -n ingress-nginx -o wide
netstat -antp | grep 80 # 到对应的node上查看80是否被监听

到windows hosts中添加域名映射,实现访问