入门级实操教程!从概念到部署,全方位了解K8S Ingress!

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

入门级实操教程!从概念到部署,全方位了解K8S Ingress!

RancherLabs   2019-12-14 我要评论

Kubernetes Ingress用于添加规则,以将流量从外部路由到Kubernetes集群的服务中。在本文中你将了解ingress 的概念,以及用于路由外部流量到Kubernetes deployment的ingress controller。

通常情况下,自定义Nginx或HAproxy Kubernetes部署将作为服务被暴露,它们用于将外部流量代理到内部集群的服务中。其中,路由规则将会bake到Pod中,并作为configmap添加。Kubernetes ingress的行为与此类似,只是路由规则将作为Kubernetes ingress对象维护。它具有动态路由规则配置的巨大优势,因此无需重新部署proxy pods。

Kubernetes Ingress入门浅析

想要顺利开始使用Kubernetes Ingress,你需要了解以下两个关键概念:

1、 Kubernetes Ingress

2、 Kubernetes Ingress Controller

让我们来逐一了解。

Kubernetes Ingress

Kubernetes Ingress是一个原生的Kubernetes资源,你可以设置规则来从外部路由流量到集群内部的服务端点。它需要一个Ingress Controller来路由ingress对象所指定的规则。Ingress 对象如下所示:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: dev
spec:
  rules:
  - host: test.apps.example.com
    http:
      paths:
      - backend:
          serviceName: hello-service
          servicePort: 80

上面的声明意味着,对test.apps.example.com的所有调用都应该hit名为hello-service的服务,这一服务位于dev命名空间中。

关于Ingress对象,你需要了解的关键事项如下:

  1. 你应该在你所部署服务的命名空间内创建ingress规则。如果在其他没有ingress对象的命名空间中,你将无法路由流量到其中的服务内。

  2. 一个ingress对象需要一个ingress controller来路由流量

  3. 外部流量将不会hit ingress API,而是hit ingress controller服务。

Kubernetes Ingress Controller

Ingress controller是一个典型的部署在集群中的代理服务,它只是暴露给服务的Kubernetes部署。以下是可用于Kubernetes的Ingress Controller:

  • Nginx Ingress Controller

  • Traefik

  • HAproxy

  • Contour

  • GKE Ingress Controller

目前,Nginx是大多数企业的选择。以下是Nginx Ingress Controller的工作原理:

  1. 在Nginx controller pod内部的nginx.conf文件是一个go 模板,它可以与Kubernetes Ingress API通信并实时获得流量路由的最新值。

  2. Nginx controller与Kubernetes ingress API 通信以检查是否为流量路由创建了规则。

  3. 如果它发现了任何ingress规则,它将应用到Nginx Controller配置,也就是使用go模板在pod内的nginx.conf文件。

如果你使用exec连接到pod并检查/etc/nginx/nginx.conf文件,则可以看到在conf文件中应用的ingress对象中指定的所有规则。

以下的架构图将解释在一个Kubernetes集群上的ingress设置。

接下来,我们详细看看如何使用Nginx Ingress Controller在Kubernetes中设置Ingress。

前期准备

  • 一个Kubernetes集群

  • 安装好的kubectl并已对Kubernetes集群进行身份验证

  • Kubernetes集群的管理员访问权限

  • 指向ingress controller负载均衡器的有效域

如果你在谷歌云上,请为你的账户分配管理员权限以启用集群角色。

ACCOUNT=$(gcloud info --format='value(config.account)')
kubectl create clusterrolebinding owner-cluster-admin-binding \
    --clusterrole cluster-admin \
    --user $ACCOUNT

请注意:本教程已在Google Cloud GKE集群上尝试过。理论上,它可在所有云环境中使用。如果你真的遇到任何错误,则可能需要在设置中进行一些调整。

设置Nginx Ingress Controller

有两个nginx ingress controller:

  • Kubernetes社区的Nginx ingress controller: https://github.com/kubernetes/ingress-nginx

  • Nginx公司的Nginx ingress controller: https://github.com/nginxinc/kubernetes-ingress

我们将使用Kubernetes社区的nginx controller。

Ingress controller需要特定的命名空间、服务账户、集群角色绑定、configmap等。因此,你需要使用官方ingress repo中的yaml文件来创建所提到的Kubernetes对象。

官方repo:

https://github.com/kubernetes/ingress-nginx/tree/masterhttps://img.qb5200.com/download-x/deploy

让我们使用mandatory.yaml文件部署ingress controller,你可以在官方repo找到它。它有nginx所需的Kubernetes对象列表。

让我们使用kubectl创建Nginx controller deployment:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/masterhttps://img.qb5200.com/download-x/deploy/static/mandatory.yaml

检查ingress controller pod以确保它是否正确设置:

kubectl get pods -n ingress-nginx

为Ingress Controller设置 LoadBalancer 服务

下一步是创建一个LoadBalancer类型的服务,以在集群外部暴露nginx controller部署。

Step1:在本地创建项目目录,然后切换到该目录。

mkdir ingress-deployment && cd ingress-deployment

Step2:创建一个名为nginx-ingress.yaml的文件

vi nginx-ingress.yaml

Step3:复制以下内容到文件

请注意:label下的annotation对于nginx controller部署集成非常重要

kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  externalTrafficPolicy: Local
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  ports:
    - name: http
      port: 80
      targetPort: http
    - name: https
      port: 443
      targetPort: https

Step4:创建ingress 服务

kubectl apply -f nginx-ingress.yaml

Step5:检查已创建的服务是否已连接到外部负载均衡器

kubectl get svc -n ingress-nginx

将域名映射到Loadbalancer IP

为了让我们的ingress的设置运转起来,我们需要映射一个域名到负载均衡器IP。你可以用两种方式,完成此操作。

单个DNS映射

你可以将单个域作为A record直接映射到负载均衡器IP,使用这一功能,你只能为ingress controller提供一个域,并可以基于多个路径进行流量路由。

例如:

www.example.com --> Loadbalancer IP

您可以使用此模型进行基于路径的路由。

以下有几个例子:

http://www.example.com/app1
http://www.example.com/app2
http://www.example.com/app1/api
http://www.example.com/app2/api

通配符DNS映射

如果你映射一个通配符DNS到负载均衡器,你就可以通过ingress拥有动态DNS端点。

例如:

 *.apps.example.com

这样,你可以通过单个ingress controller拥有多个动态子域,并且每个DNS有自己基于路径的路由。

例如:

#URL one
 
http:/https://img.qb5200.com/download-x/demo1.apps.example.com/api
http:/https://img.qb5200.com/download-x/demo1.apps.example.com/api/v1
http:/https://img.qb5200.com/download-x/demo1.apps.example.com/api/v2
 
#URL two
 
http:/https://img.qb5200.com/download-x/demo2.apps.example.com/api
http:/https://img.qb5200.com/download-x/demo2.apps.example.com/api/v1
http:/https://img.qb5200.com/download-x/demo2.apps.example.com/api/v2

出于演示目的,我们已将通配符DNS映射到LoadBalancer IP。你可以根据你的DNS提供商进行此设置。

设置一个Demo 应用程序

出于测试的目的,我们将部署一个demo应用程序并且添加一个ClusterIP服务到应用程序上。

Step1:创建一个名为dev的命名空间

kubectl create namespace dev

Step2:创建一个名为hello-app.yaml的文件

Step3:复制以下内容到文件并保存

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-app
  namespace: dev
spec:
  selector:
    matchLabels:
      app: hello
  replicas: 3
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: hello
        image: "gcr.io/google-samples/hello-app:2.0"

Step4:使用kubectl创建deployment

kubectl create -f hello-app.yaml

检查deployment状态

Step5:创建一个名为hello-app-service.yaml的文件

Step6:复制以下内容到文件并保存


apiVersion: v1
kind: Service
metadata:
  name: hello-service
  namespace: dev
  labels:
    app: hello
spec:
  type: ClusterIP
  selector:
    app: hello
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP

Step7:使用kubectl创建服务

kubectl create -f hello-app-service.yaml

检查服务状态

kubectl get svc -n dev

创建Kubernetes Ingress对象

现在让我们使用一个DNS创建一个Ingress对象来访问我们的hello app。Ingress对象可以设置路由规则。

Ingress controller pod会连接到Ingress API来检查规则,并且会相应地更新其nginx.conf。

Step1:创建一个名为ingress.yaml的文件

Step2:复制以下内容到文件并保存

使用你的域名替换test.apps.example.info。此处,我们假设你已经有*.apps.example.info格式的通配符域名。


apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: dev
spec:
  rules:
  - host: test.apps.example.info
    http:
      paths:
      - backend:
          serviceName: hello-service
          servicePort: 80

Step3:描述已创建的ingress对象,它用于检查配置

kubectl describe ingress  -n dev

现在,如果你尝试访问test.apps.example.info域(用你的域名代替它),你应该能够访问我们部署的app。

原文链接:

https:/https://img.qb5200.com/download-x/devopscube.com/kubernetes-ingress-tutorial/

https:/https://img.qb5200.com/download-x/devopscube.com/setup-ingress-kubernetes-nginx-controller/

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们