在k8s集群中安装ingress-nginx controller

简介

k8s中有多种ingress controller,在这儿介绍ingress-nginx controller的安装和使用

  1. ingress-nginx controller
  2. Apache APISIX Ingress 控制器
  3. HAProxy Ingress Controller
  4. Istio Ingress Controller

参考资料

  1. k8s官方参考文档
  2. ingress-nginx controller官方文档

安装环境

系统:mac pro
k8s:kind(kubernetes in docker),kind安装过程见https://kind.sigs.k8s.io/docs/user/quick-start/

安装过程

  1. 安装ingress-nginx controller
    yaml文件地址:https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/cloud/deploy.yaml
  2. 安装web服务,这儿以一个nginx集群为例
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ingress-nginx
  name: "nginx"
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
      name: "nginx"
    spec:
      containers:
      - image: "nginx"
        name: "nginx"
        ports:
          - containerPort: 80
---

kind: Service
apiVersion: v1
metadata:
  namespace: ingress-nginx
  name: "nginx-service"
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  1. 安装ingress,将web service配置、转发规则、域名等信息加载到ingress-nginx controller中的nginx中
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: ingress-nginx
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx # 这一步需要指定ingressClassName,不然信息无法同步到nginx配置中,也会报 ingress does not contain a valid Ingress Class  错误
#  defaultBackend:
#    service:
#      name: nginx-service
#      port:
#        number: 80
  rules:
  - http:
      paths:
      - path: /test
        pathType: Prefix
        backend:
          service:
            name: nginx-service
            port:
              number: 80
  1. 暴露ingress-nginx controller到本机中,然后通过http://localhost:8080/test 即可访问web服务(这儿是第二步安装的nginx集群)
    kubectl port-forward --namespace=ingress-nginx service/ingress-nginx-controller 8080:80
    如果报 failed calling webhook validate.nginx.ingress.kubernetes.io 错误,可以修改ingress-nginx controller 部署文件的webhooks failurePolicy 为Ignore
webhooks:
  - name: validate.nginx.ingress.kubernetes.io
    matchPolicy: Equivalent
    rules:
      - apiGroups:
          - networking.k8s.io
        apiVersions:
          - v1
        operations:
          - CREATE
          - UPDATE
        resources:
          - ingresses
    failurePolicy: Ignore
    sideEffects: None
    admissionReviewVersions:
      - v1
    clientConfig:
      service:
        namespace: ingress-nginx
        name: ingress-nginx-controller-admission
        path: /networking/v1/ingresses

你可能感兴趣的:(在k8s集群中安装ingress-nginx controller)