k8s-ingress-nginx搭建

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
简介: k8s-ingress-nginx搭建

首先区分开ingress和ingress-controller


前者个人理解是一种集群中服务之间访问控制策略


后者是client访问集群时得一种方式如下图

思维导图(部署方式不同,略有差别)

暴露服务还分几种方式


一 .Deployment+LoadBalancer模式的Service


客户端不能直接访问集群服务,创建ingress controller 的多副本模式 整套需要创建2个负载均衡 一个是proxy到ingress controller,一个是进来集群到后端服务的ingress的反向代理 需绑定2个svc。


二.Deployment+NodePort模式的Service


client直接访问集群服务,通过标签匹配node节点,创建的资源同上,只不过ingress controller的svc需修改成nodeport模式,node节点直接暴露服务,但是只能是30000-32767不方便客户使用。


三.DaemonSet+HostNetwork+nodeSelector (本次实验使用的方式)


通过标签匹配,选定node节点,使用主机网络模式,与上面的流程不同之处,整套流程创建一个ingress controller 绑定一个ingress代理一个svc绑定deployment

有一定依耐性,必须先启动Ingress Controller 服务

1.创建Ingress Controller

vim mandatory.yaml 
##190行之后定义适合自己的模式
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
kind: DaemonSet
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:
      hostNetwork: true
      # wait up to five minutes for the drain of connections
      terminationGracePeriodSeconds: 300
      serviceAccountName: nginx-ingress-serviceaccount
      nodeSelector:
        kubernetes.io/os: linux
        nodes-rola: edg
      containers:
        - name: nginx-ingress-controller
          image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.29.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


如果是cluster,nodeport的话 还需要一个svc代理Ingress Controller

vim mandatory-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 443
  selector:
    app.kubernetes.io/name: ingress-nginx
  type: ClusterIP


这里我们用的主机网络模式 所以不用创建svc


2.创建转发规则ingress

vim ingress-nginx.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-mynginx
  namespace: default ##修改命名空间
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: mynginx.com
    http:
      paths:
      - path:
        backend:
          serviceName: service-nginx ##绑定后端服务
          servicePort: 80


3.创建后端web服务

vim my-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: service-nginx ##与上面的ingress需一致,绑定关系 
  namespace: default  ##命名空间
spec:
  selector:
    app: mynginx   ##绑定下面的deployment启的pod
  ports:
  - name: http
    port: 80
    targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mynginx ##deployment的名字 无依赖关系
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mynginx ##这里是deployment和svc绑定关系,下面pod也得一致
  template:
    metadata:
      labels:
        app: mynginx ##与上面得deployment绑定,才能让deploy管理pod
    spec:
      containers:
      - name: mycontainer
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - name: nginx 
          containerPort: 80
相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
6月前
|
Kubernetes Docker 容器
k8s安装Ingress-Nginx
首先,需要匹配Ingress-nginx版本和kubernetes版本。 在[https://github.com/kubernetes/ingress-nginx](https://github.com/kubernetes/ingress-nginx)可以找到,如下图所示: 笔者用的k8s版本是v1.21.2,需要安装Ingress-nginx的v1.3.1版本,下载如下文件:
1177 0
|
4月前
|
负载均衡 应用服务中间件 API
深入理解 Nginx 与 Kong 的配置与实践
在微服务架构中,Nginx 用于配置负载均衡,如示例所示,定义上游`pay-service`包含不同权重的服务节点。Kong API 网关则通过service和route进行服务管理和路由,与Nginx的upstream和location类似。通过Kong的命令行接口,可以创建upstream、target、service和route,实现对后端服务的负载均衡和请求管理。Nginx和Kong协同工作,提供高效、灵活的API管理和流量控制。
129 1
深入理解 Nginx 与 Kong 的配置与实践
|
6月前
|
Kubernetes 应用服务中间件 nginx
nginx-ingress学习笔记
nginx-ingress学习笔记
136 0
|
6月前
|
Kubernetes 负载均衡 应用服务中间件
Nginx Ingress
Nginx Ingress是Kubernetes的一个开源控制器,用于管理和配置外部访问Kubernetes集群中的服务。它可以提供负载均衡、SSL终结和基于名称的虚拟托管等功能,使得Kubernetes集群中的服务可以更加方便地对外提供服务。
125 2
|
11月前
|
Kubernetes 容器
005_部署nginx-ingress
005_部署nginx-ingress
121 0
|
11月前
|
应用服务中间件 nginx
nginx部署问题
nginx部署问题
70 0
|
Kubernetes 网络协议 应用服务中间件
k8s--Ingress 介绍、ingress-nginx(二)
k8s--Ingress 介绍、ingress-nginx
|
Kubernetes 应用服务中间件 nginx
k8s--Ingress 介绍、ingress-nginx(一)
k8s--Ingress 介绍、ingress-nginx
|
安全 应用服务中间件 Shell
|
Kubernetes 负载均衡 网络协议
编排系统K8S Ingress-nginx介绍
在Kubernetes编排系统中,容器服务和Pod的IP地址仅允许在集群网络内部使用,对于集群外的应用是不可见的。为了使外部的应用能够访问集群内的服务,在Kubernetes平台中目前提供了以下3种通用型方案:
215 0