• k8s 对外发布(ingress)


    k8s中,service的作用体现在两个方面,对集群内部,它不断跟踪pod的变化,更新endpoint中对应pod的对象,提供了ip不断变化的pod的服务发现机制; 对集群外部,他类似负载均衡器,可以在集群内外部对pod进行访问

    对外发布方式

    在k8s中,Pod的IP地址和service的ClusterIP仅可以在集群网络内部使用,对于集群外的应用是不可见的。 为了使外部的应用能够访问集群内的服务,Kubernetes目前提供了以下几种方案:

    NodePort:将服务暴露在节点网络上,NodePort背后就是kube-proxy,kube-proxy是沟通服务网络,Pod网络和节点网络的桥梁

    测试环境使用还行,当有几十上百的服务在集群中运行时,NodePort的端口管理就是个灾难。 因为每个端口只能是一种服务,端口范围只能是 30000-32767

    LoadBalancer:通过设置LoadBalancer映射到云服务商提供的LoadBalancer地址。 这种用法仅用于在公有云服务提供商的云平台上设置 Service 的场景。 受限于云平台,且通常在云平台部署LoadBalancer还需要额外的费用
    在service提交后,Kubernetes就会调用CloudProvider在公有云上为你创建一个负载均衡服务,并且把被代理的Pod的IP地址配置给负载均衡服务做后端

    externalIPs: service允许为其分配外部IP,如果外部IP路由到集群中一个或多个Node上,Service会被暴露给这些externalIPs。通过外部IP进入到集群的流量,将会被路由到Service的Endpoint上

    Ingress: 只需一个或者少量的公网IP和LB,即可同时将多个HTTP服务暴露到外网,七层反向代理。 可以简单理解为service的service,它其实就是一组基于域名和URL路径,把用户的请求转发到一个或多个service的规则

    ingress组成

    ingress: ingress是一个API对象,通过yaml文件来配置,ingress对象的作用是定义请求如何转发到service的规则,可以理解为配置模板。 ingress通过http或https暴露集群内部service,给service提供外部URL、负载均衡、SSL/TLS以及基于域名的反向代理。 ingress要依靠 ingress-controller 来具体实现以上功能

    ingress-controller : 当做反向代理或者说是转发器。 ingress-controller是具体实现反向代理及负载均衡的程序,对ingress定义的规则进行解析,根据配置的规则来实现请求转发。

    ingress-controller并不是k8s自带的组件,实际上ingress-controller只是一个统称,用户可以选择不同的ingress-controller实现,目前,由k8s维护的ingress-controller只有google云的GCE与ingress-nginx两个, 其他还有很多第三方维护的ingress-controller,具体可以参考官方文档。 但是不管哪一种ingress-controller,实现的机制都大同小异,只是在具体配置上有差异。

    一般来说,ingress-controller的形式都是一个pod,里面跑着daemon程序和反向代理程序。 daemon负责不断监控集群的变化,根据 ingress对象生成配置并应用新配置到反向代理,比如ingress-nginx就是动态生成nginx配置,动态更新upstream,并在需要的时候reload程序应用新配置。 为了方便,后面的例子都以k8s官方维护的ingress-nginx为例

    Ingress-Nginx github 地址:https://github.com/kubernetes/ingress-nginx
    Ingress-Nginx 官方网站:https://kubernetes.github.io/ingress-nginx/

    ingress-controller才是负责具体转发的组件,通过各种方式将它暴露在集群入口,外部对集群的请求流量会先到 ingress-controller, 而ingress对象是用来告诉ingress-controller该如何转发请求,比如哪些域名、哪些URL要转发到哪些service等等

    ingress工作原理

    (1)ingress-controller通过和 kubernetes APIServer 交互,动态的去感知集群中ingress规则变化
    (2)然后读取它,按照自定义的规则,规则就是写明了哪个域名对应哪个service,生成一段nginx配置
    (3)再写到nginx-ingress-controller的pod里,这个ingress-controller的pod里运行着一个Nginx服务,控制器会把生成的 nginx配置写入 /etc/nginx.conf文件中,
    (4)然后reload一下使配置生效。 以此达到域名区分配置和动态更新的作用

    部署nginx-ingress-controller

    1. 1.部署ingress-controller pod及相关资源
    2. mkdir /mnt/ingress
    3. cd /mnt/ingress
    4. 官方下载地址:
    5. wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.25.0/deploy/static/mandatory.yaml
    6. 上面可能无法下载,可用国内的 gitee
    7. wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.25.0/deploy/static/mandatory.yaml
    8. wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml
    9. #mandatory.yaml文件中包含了很多资源的创建,包括namespace、ConfigMap、role,ServiceAccount等等所有部署ingress-controller需要的资源
    10. 2.修改ClusterRole资源配置
    11. vim mandatory.yaml
    12. ......
    13. apiVersion: rbac.authorization.k8s.io/v1beta1
    14. #RBAC相关资源从1.17版本开始改用rbac.authorization.k8s.io/v1,rbac.authorization.k8s.io/v1beta1在1.22版本即将弃用
    15. kind: ClusterRole
    16. metadata:
    17. name: nginx-ingress-clusterrole
    18. labels:
    19. app.kubernetes.io/name: ingress-nginx
    20. app.kubernetes.io/part-of: ingress-nginx
    21. rules:
    22. - apiGroups:
    23. - ""
    24. resources:
    25. - configmaps
    26. - endpoints
    27. - nodes
    28. - pods
    29. - secrets
    30. verbs:
    31. - list
    32. - watch
    33. - apiGroups:
    34. - ""
    35. resources:
    36. - nodes
    37. verbs:
    38. - get
    39. - apiGroups:
    40. - ""
    41. resources:
    42. - services
    43. verbs:
    44. - get
    45. - list
    46. - watch
    47. - apiGroups:
    48. - "extensions"
    49. - "networking.k8s.io" # (0.25版本)增加 networking.k8s.io Ingress 资源的 api
    50. resources:
    51. - ingresses
    52. verbs:
    53. - get
    54. - list
    55. - watch
    56. - apiGroups:
    57. - ""
    58. resources:
    59. - events
    60. verbs:
    61. - create
    62. - patch
    63. - apiGroups:
    64. - "extensions"
    65. - "networking.k8s.io" # (0.25版本)增加 networking.k8s.io/v1 Ingress 资源的 api
    66. resources:
    67. - ingresses/status
    68. verbs:
    69. - update

    ingress 暴露服务的方式

    Deployment+LoadBalancer 模式的 Service

    如果要把ingress部署在公有云,那用这种方式比较合适。用Deployment部署ingress-controller,创建一个 type为 LoadBalancer 的 service 关联这组 pod。大部分公有云,都会为 LoadBalancer 的 service 自动创建一个负载均衡器,通常还绑定了公网地址。 只要把域名解析指向该地址,就实现了集群服务的对外暴露

    DaemonSet+HostNetwork+nodeSelector

    用DaemonSet结合nodeselector来部署ingress-controller到特定的node上,然后使用HostNetwork直接把该pod与宿主机node的网络打通,直接使用宿主机的80/433端口就能访问服务。这时,ingress-controller所在的node机器就很类似传统架构的边缘节点,比如机房入口的nginx服务器。该方式整个请求链路最简单,性能相对NodePort模式更好。缺点是由于直接利用宿主机节点的网络和端口,一个node只能部署一个ingress-controller pod。 比较适合大并发的生产环境使用

    Deployment+NodePort模式的Service

    同样用deployment模式部署ingress-controller,并创建对应的service,但是type为NodePort。这样,ingress就会暴露在集群节点ip的特定端口上。由于nodeport暴露的端口是随机端口,一般会在前面再搭建一套负载均衡器来转发请求。该方式一般用于宿主机是相对固定的环境ip地址不变的场景
    NodePort方式暴露ingress虽然简单方便,但是NodePort多了一层NAT,在请求量级很大时可能对性能会有一定影响

    DaemonSet+HostNetwork+nodeSelector

    1. 3.指定 nginx-ingress-controller 运行在 node02 节点
    2. kubectl label node node02 ingress=true
    3. kubectl get nodes --show-labels
    4. NAME STATUS ROLES AGE VERSION LABELS
    5. master01 Ready control-plane,master 18d v1.20.11 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=
    6. node01 Ready 18d v1.20.11 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,emmm=a,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
    7. node02 Ready 18d v1.20.11 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,emmm=b,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=node02,kubernetes.io/os=linux
    8. 4.修改Deployment 为 DaemonSet 指定节点运行,并开启hostNetwork网络
    9. vim mandatory.yaml
    10. ...
    11. apiVersion: apps/v1
    12. # 修改 kind
    13. # kind: Deployment
    14. kind: DaemonSet
    15. metadata:
    16. name: nginx-ingress-controller
    17. namespace: ingress-nginx
    18. labels:
    19. app.kubernetes.io/name: ingress-nginx
    20. app.kubernetes.io/part-of: ingress-nginx
    21. spec:
    22. # 删除Replicas
    23. # replicas: 1
    24. selector:
    25. matchLabels:
    26. app.kubernetes.io/name: ingress-nginx
    27. app.kubernetes.io/part-of: ingress-nginx
    28. template:
    29. metadata:
    30. labels:
    31. app.kubernetes.io/name: ingress-nginx
    32. app.kubernetes.io/part-of: ingress-nginx
    33. annotations:
    34. prometheus.io/port: "10254"
    35. prometheus.io/scrape: "true"
    36. spec:
    37. # 使用主机网络
    38. hostNetwork: true
    39. # 选择节点运行
    40. nodeSelector:
    41. ingress: "true"
    42. serviceAccountName: nginx-ingress-serviceaccount
    43. ......
    44. 5、在所有 node 节点上传 nginx-ingress-controller 镜像压缩包 ingree.contro.tar.gz 到 /mnt目录,并解压和加载镜像
    45. cd /mnt
    46. tar zxvf ingree.contro.tar.gz
    47. docker load -i ingree.contro.tar
    48. 6、启动 nginx-ingress-controller
    49. kubectl apply -f mandatory.yaml
    50. #nginx-ingress-controller 已经运行 node02 节点
    51. kubectl get pod -n ingress-nginx -o wide
    52. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    53. nginx-ingress-controller-f28pz 1/1 Running 0 7m4s 192.168.111.9 node02
    54. kubectl get cm,daemonset -n ingress-nginx -owide
    55. NAME DATA AGE
    56. configmap/ingress-controller-leader-nginx 0 21m
    57. configmap/kube-root-ca.crt 1 22m
    58. configmap/nginx-configuration 0 22m
    59. configmap/tcp-services 0 22m
    60. configmap/udp-services 0 22m
    61. NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
    62. daemonset.apps/nginx-ingress-controller 1 1 1 1 1 ingress=true 22m nginx-ingress-controller quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.25.0 app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx
    63. 到 node02 节点查看
    64. netstat -lntp| grep nginx
    65. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 68532/nginx: master
    66. tcp 0 0 0.0.0.0:8181 0.0.0.0:* LISTEN 68532/nginx: master
    67. tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 68532/nginx: master
    68. tcp6 0 0 :::10254 :::* LISTEN 68493/nginx-ingress
    69. 由于配置了 hostnetwork,nginx 已经在 node 主机本地监听 80/443/8181 端口。其中 8181 是 nginx-controller 默认配置的一个 default backend(Ingress 资源没有匹配的 rule 对象时,流量就会被导向这个 default backend)。
    70. 这样,只要访问 node 主机有公网 IP,就可以直接映射域名来对外网暴露服务了。如果要 nginx 高可用的话,可以在多个 node上部署,并在前面再搭建一套 LVS+keepalived 做负载均衡
    71. 7.创建ingress规则
    72. #创建一个deploy 和 svc
    73. vim service-nginx.yaml
    74. apiVersion: apps/v1
    75. kind: Deployment
    76. metadata:
    77. name: nginx-app
    78. spec:
    79. replicas: 2
    80. selector:
    81. matchLabels:
    82. app: nginx
    83. template:
    84. metadata:
    85. labels:
    86. app: nginx
    87. spec:
    88. containers:
    89. - name: nginx
    90. image: nginx
    91. imagePullPolicy: IfNotPresent
    92. ports:
    93. - containerPort: 80
    94. ---
    95. apiVersion: v1
    96. kind: Service
    97. metadata:
    98. name: nginx-app-svc
    99. spec:
    100. type: ClusterIP
    101. ports:
    102. - protocol: TCP
    103. port: 80
    104. targetPort: 80
    105. selector:
    106. app: nginx
    107. vim ingress-app.yaml
    108. apiVersion: networking.k8s.io/v1
    109. kind: Ingerss
    110. metadata:
    111. name: nginx-app-ingress
    112. spec:
    113. rules:
    114. - host: www.emmm.com
    115. http:
    116. - path: /
    117. pathType: Prefix
    118. backend:
    119. service:
    120. name: nginx-app-svc
    121. port:
    122. number: 80
    123. kubectl apply -f service-nginx.yaml
    124. kubectl apply -f ingress-app.yaml
    125. kubectl get pods
    126. NAME READY STATUS RESTARTS AGE
    127. nginx-app-845d4d9dff-hqqg5 1/1 Running 0 11m
    128. nginx-app-845d4d9dff-zm4t9 1/1 Running 0 11m
    129. kubectl get ingress
    130. kubectl get ingress
    131. NAME CLASS HOSTS ADDRESS PORTS AGE
    132. nginx-app-ingress www.emmm.com 80 76s
    133. 8、测试访问
    134. //本地 host 添加域名解析
    135. vim /etc/hosts
    136. 192.168.111.7 master01
    137. 192.168.111.8 node01
    138. 192.168.111.9 node02
    139. #192.168.111.10 hub.emmm.com
    140. 192.168.111.10 stor01
    141. 192.168.111.9 www.emmm.com
    142. curl www.emmm.com
    143. <head>
    144. Welcome to nginx!
    145. Welcome to nginx!

    146. If you see this page, the nginx web server is successfully installed and

    147. working. Further configuration is required.

    148. For online documentation and support please refer to

    149. Commercial support is available at
    150. Thank you for using nginx.

    151. 9.查看nginx-ingress-controller
    152. kubectl get pod -n ingress-nginx -owide
    153. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    154. nginx-ingress-controller-f28pz 1/1 Running 0 90m 192.168.111.9 node02
    155. kubectl exec -it nginx-ingress-controller-f28pz -n ingress-nginx /bin/bash
    156. # more /etc/nginx/nginx.conf
    157. #可以看到从 start server www.emmm.com 到 end server www.emmm.com 之间包含了此域名用于反向代理的配置

    Deployment+NodePort模式的Service

    1. 1.下载 nginx-ingress-controller 和 ingress-nginx 暴露端口配置文件
    2. mkdir /mnt/ingress-nodeport
    3. cd /mnt/ingress-nodeport
    4. 官方下载地址:
    5. wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml
    6. wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
    7. 国内 gitee 资源地址:
    8. wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml
    9. wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
    10. 2、在所有 node 节点上传镜像包 ingress-controller-0.30.0.tar 到 /opt/ingress-nodeport 目录,并加载镜像
    11. docker load -i ingress-controller-0.30.0.tar
    12. 3、启动 nginx-ingress-controller
    13. kubectl apply -f mandatory.yaml
    14. kubectl apply -f service-nodeport.yaml
    15. -----------------------------------------------------------------------------------------
    16. //如果K8S Pod 调度失败,在 kubectl describe pod资源时显示:
    17. Warning FailedScheduling 18s (x2 over 18s) default-scheduler 0/2 nodes are available: 2 node(s) didn't match node selector
    18. 解决方案:
    19. 1. 给需要调度的node加上对应标签
    20. # 相对上面这个Yaml文件的例子
    21. kubectl label nodes node_name kubernetes.io/os=linux
    22. 2. 删除Yaml文件中的nodeSelector,如果对节点没有要求的话,直接删除节点选择器即可
    23. -----------------------------------------------------------------------------------------
    24. kubectl get pod,svc -n ingress-nginx
    25. NAME READY STATUS RESTARTS AGE
    26. pod/nginx-ingress-controller-54b86f8f7b-wc7r7 1/1 Running 0 104s
    27. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    28. service/ingress-nginx NodePort 10.96.113.131 80:31281/TCP,443:31688/TCP 10s
    29. Ingress HTTP 代理访问
    30. cd /mnt/ingress-nodeport
    31. #创建 deployment、Service、Ingress Yaml 资源
    32. vim ingress-nginx.yaml
    33. apiVersion: apps/v1
    34. kind: Deployment
    35. metadata:
    36. name: nginx-app
    37. spec:
    38. replicas: 2
    39. selector:
    40. matchLabels:
    41. name: nginx
    42. template:
    43. metadata:
    44. labels:
    45. name: nginx
    46. spec:
    47. containers:
    48. - name: nginx
    49. image: nginx
    50. imagePullPolicy: IfNotPresent
    51. ports:
    52. - containerPort: 80
    53. ---
    54. apiVersion: v1
    55. kind: Service
    56. metadata:
    57. name: nginx-svc
    58. spec:
    59. ports:
    60. - port: 80
    61. targetPort: 80
    62. protocol: TCP
    63. selector:
    64. name: nginx
    65. ---
    66. apiVersion: networking.k8s.io/v1
    67. kind: Ingress
    68. metadata:
    69. name: nginx-test
    70. spec:
    71. rules:
    72. - host: www.emmm.com
    73. http:
    74. paths:
    75. - path: /
    76. pathType: Prefix
    77. backend:
    78. service:
    79. name: nginx-svc
    80. port:
    81. number: 80
    82. kubectl apply -f ingress-nginx.yaml
    83. kubectl get svc,pods -o wide
    84. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
    85. service/kubernetes ClusterIP 10.96.0.1 443/TCP 19d
    86. service/nginx-svc ClusterIP 10.96.229.128 80/TCP 61s name=nginx
    87. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    88. pod/nginx-app-57dd86f5cc-9pj8h 1/1 Running 0 28s 10.244.2.95 node02
    89. pod/nginx-app-57dd86f5cc-txfqp 1/1 Running 0 27s 10.244.1.131 node01
    90. kubectl exec -it pod/nginx-app-57dd86f5cc-9pj8h bash
    91. # cd /usr/share/nginx/html/
    92. # echo 'this is web1' >> index.html
    93. kubectl exec -it pod/nginx-app-57dd86f5cc-txfqp bash
    94. # cd /usr/share/nginx/html/
    95. # echo 'this is web2' >> index.html
    96. #进行测试
    97. curl 10.96.229.128
    98. kubectl get pod -n ingress-nginx -owide
    99. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    100. nginx-ingress-controller-54b86f8f7b-wc7r7 1/1 Running 0 17m 10.244.1.130 node01
    101. kubectl get svc -n ingress-nginx
    102. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    103. ingress-nginx NodePort 10.96.113.131 80:31281/TCP,443:31688/TCP 16m
    104. 本地 host 添加域名解析
    105. vim /etc/hosts
    106. 192.168.111.8 www.emmm.com
    107. #测试
    108. www.emmm.com:31281
    109. ingress HTTP 访问虚拟主机
    110. mkdir /mnt/ingress-nodeport/vhost
    111. cd /mnt/ingress-nodeport/vhost
    112. #创建虚拟机1资源
    113. vim deployment1.yaml
    114. apiVersion: apps/v1
    115. kind: Deployment
    116. metadata:
    117. name: deployment1
    118. spec:
    119. replicas: 2
    120. selector:
    121. matchLabels:
    122. name: nginx1
    123. template:
    124. metadata:
    125. labels:
    126. name: nginx1
    127. spec:
    128. containers:
    129. - name: nginx1
    130. image: soscscs/myapp:v1
    131. imagePullPolicy: IfNotPresent
    132. ports:
    133. - containerPort: 80
    134. ---
    135. apiVersion: v1
    136. kind: Service
    137. metadata:
    138. name: svc-1
    139. spec:
    140. ports:
    141. - port: 80
    142. targetPort: 80
    143. protocol: TCP
    144. selector:
    145. name: nginx1
    146. kubectl apply -f deployment1.yaml
    147. vim deployment2.yaml
    148. apiVersion: apps/v1
    149. kind: Deployment
    150. metadata:
    151. name: deployment2
    152. spec:
    153. replicas: 2
    154. selector:
    155. matchLabels:
    156. name: nginx2
    157. template:
    158. metadata:
    159. labels:
    160. name: nginx2
    161. spec:
    162. containers:
    163. - name: nginx2
    164. image: soscscs/myapp:v2
    165. imagePullPolicy: IfNotPresent
    166. ports:
    167. - containerPort: 80
    168. ---
    169. apiVersion: v1
    170. kind: Service
    171. metadata:
    172. name: svc-2
    173. spec:
    174. ports:
    175. - port: 80
    176. targetPort: 80
    177. protocol: TCP
    178. selector:
    179. name: nginx2
    180. kubectl apply -f deployment2.yaml
    181. #创建ingress资源
    182. vim ingress-nginx.yaml
    183. apiVersion: networking.k8s.io/v1
    184. kind: Ingress
    185. metadata:
    186. name: ingress1
    187. spec:
    188. rules:
    189. - host: www1.emmm.com
    190. http:
    191. paths:
    192. - path: /
    193. pathType: Prefix
    194. backend:
    195. service:
    196. name: svc-1
    197. port:
    198. number: 80
    199. ---
    200. apiVersion: networking.k8s.io/v1
    201. kind: Ingress
    202. metadata:
    203. name: ingress2
    204. spec:
    205. rules:
    206. - host: www2.emmm.com
    207. http:
    208. paths:
    209. - path: /
    210. pathType: Prefix
    211. backend:
    212. service:
    213. name: svc-2
    214. port:
    215. number: 80
    216. kubectl apply -f ingress-nginx.yaml
    217. #测试访问
    218. kubectl get svc -n ingress-nginx
    219. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    220. ingress-nginx NodePort 10.96.113.131 80:31281/TCP,443:31688/TCP 29m
    221. curl www1.emmm.com:31281
    222. Hello MyApp | Version: v1 | Pod Name
    223. curl www2.emmm.com:31281
    224. Hello MyApp | Version: v2 | Pod Name
    225. Ingress HTTPS 代理访问
    226. mkdir /mnt/ingress-nodeport/https
    227. cd /mnt/ingress-nodeport/https
    228. #创建ssl证书
    229. openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
    230. #创建 secret 资源进行存储
    231. kubectl create secret tls tls-secret --key tls.key --cert tls.crt
    232. kubectl get secret
    233. NAME TYPE DATA AGE
    234. default-token-kr2xl kubernetes.io/service-account-token 3 19d
    235. mysecret Opaque 2 26h
    236. mysecret1 Opaque 2 26h
    237. nfs-client-provisioner-token-nszdh kubernetes.io/service-account-token 3 2d23h
    238. tls-secret kubernetes.io/tls 2 4s
    239. kubectl describe secret tls-secret
    240. Name: tls-secret
    241. Namespace: default
    242. Labels:
    243. Annotations:
    244. Type: kubernetes.io/tls
    245. Data
    246. ====
    247. tls.key: 1704 bytes
    248. tls.crt: 1143 bytes
    249. #创建 deployment、Service、Ingress Yaml 资源
    250. vim ingress-https.yaml
    251. apiVersion: apps/v1
    252. kind: Deployment
    253. metadata:
    254. name: nginx-app
    255. spec:
    256. replicas: 2
    257. selector:
    258. matchLabels:
    259. name: nginx
    260. template:
    261. metadata:
    262. labels:
    263. name: nginx
    264. spec:
    265. containers:
    266. - name: nginx
    267. image: nginx
    268. imagePullPolicy: IfNotPresent
    269. ports:
    270. - containerPort: 80
    271. ---
    272. apiVersion: v1
    273. kind: Service
    274. metadata:
    275. name: nginx-svc
    276. spec:
    277. ports:
    278. - port: 80
    279. targetPort: 80
    280. protocol: TCP
    281. selector:
    282. name: nginx
    283. ---
    284. apiVersion: networking.k8s.io/v1
    285. kind: Ingress
    286. metadata:
    287. name: nginx-https
    288. spec:
    289. tls:
    290. - hosts:
    291. - www3.kgc.com
    292. secretName: tls-secret
    293. rules:
    294. - host: www3.emmm.com
    295. http:
    296. paths:
    297. - path: /
    298. pathType: Prefix
    299. backend:
    300. service:
    301. name: nginx-svc
    302. port:
    303. number: 80
    304. kubectl apply -f ingress-https.yaml
    305. kubectl get svc -n ingress-nginx
    306. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    307. ingress-nginx NodePort 10.96.113.131 80:31281/TCP,443:31688/TCP 36m
    308. #访问测试
    309. 在宿主机的 C:\Windows\System32\drivers\etc\hosts 文件中添加 192.168.111.8 www3.emmm.com 记录。
    310. 使用谷歌浏览器访问 https://www3.emmm.com:31688
    311. #Nginx 进行 BasicAuth
    312. mkdir /mnt/ingress-nodeport/basic-auth
    313. cd /mnt/ingress-nodeport/basic-auth
    314. #生成用户密码认证文件,创建 secret 资源进行存储
    315. yum -y install httpd
    316. htpasswd -c auth emmmm #认证文件名必须为 auth
    317. kubectl create secret generic basic-auth --from-file=auth
    318. #创建 ingress 资源
    319. vim ingress-auth.yaml
    320. apiVersion: networking.k8s.io/v1
    321. kind: Ingress
    322. metadata:
    323. name: ingress-auth
    324. annotations:
    325. #设置认证类型basic
    326. nginx.ingress.kubernetes.io/auth-type: basic
    327. #设置secret资源名称basic-auth
    328. nginx.ingress.kubernetes.io/auth-secret: basic-auth
    329. #设置认证窗口提示信息
    330. nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - emmmm'
    331. spec:
    332. rules:
    333. - host: auth.emmmm.com
    334. http:
    335. paths:
    336. - path: /
    337. pathType: Prefix
    338. backend:
    339. service:
    340. name: nginx-svc
    341. port:
    342. number: 80
    343. //具体详细设置方法可参考官网https://kubernetes.github.io/ingress-nginx/examples/auth/basic/
    344. kubectl apply -f ingress-auth.yaml
    345. #访问测试
    346. kubectl get svc -n ingress-nginx
    347. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    348. ingress-nginx NodePort 10.96.113.131 80:31281/TCP,443:31688/TCP 48m
    349. echo '192.168.111.8 auth.emmmm.com' >> /etc/hosts
    350. 浏览器访问:http://auth.emmmm.com:31281
    351. Nginx 进行重写
    352. #metadata.annotations 配置说明
    353. ●nginx.ingress.kubernetes.io/rewrite-target: <字符串> #必须重定向流量的目标URI
    354. ●nginx.ingress.kubernetes.io/ssl-redirect: <布尔值> #指示位置部分是否仅可访问SSL(当Ingress包含证书时,默认为true)
    355. ●nginx.ingress.kubernetes.io/force-ssl-redirect: <布尔值> #即使Ingress未启用TLS,也强制重定向到HTTPS
    356. ●nginx.ingress.kubernetes.io/app-root: <字符串> #定义Controller必须重定向的应用程序根,如果它在'/'上下文中
    357. ●nginx.ingress.kubernetes.io/use-regex: <布尔值> #指示Ingress上定义的路径是否使用正则表达式
    358. vim ingress-rewrite.yaml
    359. apiVersion: networking.k8s.io/v1
    360. kind: Ingress
    361. metadata:
    362. name: nginx-rewrite
    363. annotations:
    364. nginx.ingress.kubernetes.io/rewrite-target: http://www1.emmm.com:31281
    365. spec:
    366. rules:
    367. - host: re.emmm.com
    368. http:
    369. paths:
    370. - path: /
    371. pathType: Prefix
    372. backend:
    373. #由于re.emmm.com只是用于跳转不需要真实站点存在,因此svc资源名称可随意定义
    374. service:
    375. name: nginx-svc
    376. port:
    377. number: 80
    378. kubectl apply -f ingress-rewrite.yaml
    379. echo '192.168.111.8 re.emmm.com' >> /etc/hosts
    380. 浏览器访问:http://re.kgc.com:31281

  • 相关阅读:
    论文精读:Medical Transformer: Gated Axial-Attention forMedical Image Segmentation
    鸿蒙OS开发:【一次开发,多端部署】(app市场首页)项目
    微服务--限流
    HTML学习笔记 | 青训营笔记
    总结40条常用Linux命令的基本使用
    ps人像怎么做渐隐的效果?
    39、一篇文章弄懂 Java 正则表达式中的量词、贪婪、勉强、独占和 String 的 matches 方法的底层【个人感觉非常值得学习】
    通用权限系统-Spring-Boot-Starter
    数组题目总结 ---- 田忌赛马
    虚拟补丁备忘单
  • 原文地址:https://blog.csdn.net/m0_66372974/article/details/139441018