k8s部署ingress-controller实战,以及path前缀配置的问题

ingress与ingress-controller

ingress与ingress-controller的相关简介就不多介绍了,本文只写实战环节

docker镜像准备

镜像下载

docker pull registry.cn-beijing.aliyuncs.com/google_registry/nginx-ingress-controller:0.30.0

有私有仓库的,可以重命名push到私有仓库。

部署ingress-controller pod及相关资源

给节点打上标签,告诉k8那个节点可以创建ingress-controller的pod,node-1是节点名称,我们给node-1打上isIngress="true"的标签,后续我们会使用到,亦可自行再yaml内指定具体的节点

kubectl  label   node  node-1 isIngress="true"

下载两个yaml文件

https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/service-nodeport.yaml

下载不了可直接使用下面内容

mandatory.yaml:

apiVersion: v1
kind: Namespace
metadata:
  name: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

---

kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-configuration
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: tcp-services
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: udp-services
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nginx-ingress-serviceaccount
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: nginx-ingress-clusterrole
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - endpoints
      - nodes
      - pods
      - secrets
    verbs:
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - nodes
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - services
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - events
    verbs:
      - create
      - patch
  - apiGroups:
      - "extensions"
      - "networking.k8s.io"
    resources:
      - ingresses
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - "extensions"
      - "networking.k8s.io"
    resources:
      - ingresses/status
    verbs:
      - update

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
  name: nginx-ingress-role
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
rules:
  - apiGroups:
      - ""
    resources:
      - configmaps
      - pods
      - secrets
      - namespaces
    verbs:
      - get
  - apiGroups:
      - ""
    resources:
      - configmaps
    resourceNames:
      # Defaults to "<election-id>-<ingress-class>"
      # Here: "<ingress-controller-leader>-<nginx>"
      # This has to be adapted if you change either parameter
      # when launching the nginx-ingress-controller.
      - "ingress-controller-leader-nginx"
    verbs:
      - get
      - update
  - apiGroups:
      - ""
    resources:
      - configmaps
    verbs:
      - create
  - apiGroups:
      - ""
    resources:
      - endpoints
    verbs:
      - get

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
  name: nginx-ingress-role-nisa-binding
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: nginx-ingress-role
subjects:
  - kind: ServiceAccount
    name: nginx-ingress-serviceaccount
    namespace: ingress-nginx

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: nginx-ingress-clusterrole-nisa-binding
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: nginx-ingress-clusterrole
subjects:
  - kind: ServiceAccount
    name: nginx-ingress-serviceaccount
    namespace: ingress-nginx

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-ingress-controller
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: ingress-nginx
      app.kubernetes.io/part-of: ingress-nginx
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
      annotations:
        prometheus.io/port: "10254"
        prometheus.io/scrape: "true"
    spec:
      # wait up to five minutes for the drain of connections
      terminationGracePeriodSeconds: 300
      serviceAccountName: nginx-ingress-serviceaccount
      nodeSelector:
        isIngress: "true"
      containers:
        - name: nginx-ingress-controller
          image: registry.cn-beijing.aliyuncs.com/google_registry/nginx-ingress-controller:0.30.0
          args:
            - /nginx-ingress-controller
            - --configmap=$(POD_NAMESPACE)/nginx-configuration
            - --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
            - --udp-services-configmap=$(POD_NAMESPACE)/udp-services
            - --publish-service=$(POD_NAMESPACE)/ingress-nginx
            - --annotations-prefix=nginx.ingress.kubernetes.io
          securityContext:
            allowPrivilegeEscalation: true
            capabilities:
              drop:
                - ALL
              add:
                - NET_BIND_SERVICE
            # www-data -> 101
            runAsUser: 101
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
            - name: https
              containerPort: 443
              protocol: TCP
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 10
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 10
          lifecycle:
            preStop:
              exec:
                command:
                  - /wait-shutdown

---

apiVersion: v1
kind: LimitRange
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  limits:
  - min:
      memory: 90Mi
      cpu: 100m
    type: Container

文件内包括创建namespace、serviceAccount、serviceAccount权限、ingress-controller的deployment等等。

ingress-nginx服务,ingress对外暴露并接收请求的统一入口,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
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

---

 

运行上面两个文件

kubectl apply -f mandatory.yaml

kubectl apply -f service-nodeport.yaml

查看对应namespace下的pod与service,至此,我们已经创建好ingress-controller了,记住这两个对外暴露的端口,我们后续有用,一个是http的一个是https的。

 创建ingress,配置前缀匹配规则

以tensorboard为例子,创建了两个pod与service,service名称/端口分别是tensorboard-task39-service/20021,tensorboard-task40-service/20020,创建代码就省略了。

创建两个ingress文件,tensorboard-ingress-test1.yaml,tensorboard-ingress-test2.yaml。

tensorboard-ingress-test1.yaml配置指向tensorboard-task39-service/20021

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tensorboard-ingress-test1
spec:
  ingressClassName: nginx
  rules:
  - host: tensorboard-ingress.org
    http:
      paths:
      - path: /tensorboard/39
        pathType: Prefix
        backend:
          service:
            name: tensorboard-task39-service
            port: 
              number: 20021

 tensorboard-ingress-test2.yaml配置指向tensorboard-task40-service/20020

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tensorboard-ingress-test2
spec:
  ingressClassName: nginx
  rules:
  - host: tensorboard-ingress.org
    http:
      paths:
      - path: /tensorboard/40
        pathType: Prefix
        backend:
          service:
            name: tensorboard-task40-service
            port: 
              number: 20020

两份配置同样使用自己创建的域名tensorboard-ingress.org,

访问

访问前需要修改本地host,自建域名并不能被正常解析,把k8集群ip与域名配置打hosts文件内即可。

分别访问,可以得到不同的显示,这里就不展示了。

tensorboard-ingress.org:31271/tensorboard/39/

tensorboard-ingress.org:31271/tensorboard/40/

总结分析

1、生成ingress并访问,需要使用域名:ingress-nginx端口才能访问,即我们创建ingress-controller步骤所生成的service的对外暴露端口,生成多个ingress使用多个域名,访问URL也仅仅是域名改变,端口还是那个端口。

2、多个ingress可使用同一个域名,但是path部分配置,每个ingress配置需要配置不同的path规则区分需要访问的service和pod。

3、对应path的配置,想要使用同一个域名,不同path跳到不同的pod,需要pod内的应用或接口支持,例如tensorboard-ingress.org:31271/tensorboard/39/,实际上在tensorboard-task39-service所对应的pod内请求localhost:port/tensorboard/39/,ingress并不会帮你解析/tensorboard/39/这个path,只会原样的替你转发到对应的pod内请求。这个是很多文章都没有说的,只教你如何配置不同前缀去访问不同的pod。

部署 k8s 中的 ingress-controller v1.1.0,可以按照以下步骤进行操作: 1. 首先,确保 Kubernetes 集群已经可用并且已经正确设置。 2. 下载 ingress-controller v1.1.0 的安装文件。可以通过访问官方仓库或者 GitHub 并找到相关的发布版本进行下载。 3. 解压并配置 ingress-controller。通常可以通过编辑 YAML 文件来配置 ingress-controller,在其中指定一些关键参数,如使用的 ingress 类型、负载均衡器类型等。 4. 部署 ingress-controller。通过使用 kubectl 命令行工具,执行 `kubectl apply -f <配置文件>` 来完成 ingress-controller部署。 5. 确认 ingress-controller 已经成功部署。可以使用 `kubectl get pods -n <命名空间>` 命令来确认 ingress-controller 的运行状态。 6. 配置 ingress 规则。根据实际需求,编辑定义 ingress 规则的 YAML 文件并部署到 Kubernetes 中。 7. 确认 ingress 规则已经生效。可以通过 `kubectl get ingress -n <命名空间>` 命令来查看已部署ingress 规则,并确认其状态为 "RUNNING"。 8. 测试 ingress-controller。通过访问 ingress 规则中指定的域名或路径来确认 ingress-controller 是否成功地将请求转发到相应的服务。 这些步骤可以帮助您在 Kubernetes 中部署 ingress-controller v1.1.0,并通过 ingress 规则进行请求转发。在部署过程中,记得根据实际需求进行必要的配置和调整,以确保 ingress-controller 能够正常工作。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值