Nginx Ingress Controller简单使用教程

Nginx Ingress Controller简单使用教程)

  • 1. Nginx Ingress Controller概述
  • 2. 部署Nginx Ingress Controller
    • 2.1 下载yaml文件
    • 2.2 修改mandatory.yaml文件
    • 2.3 给节点打标签
    • 2.4 部署
  • 3. 示例验证
    • 3.1 使用以下yaml文件部署Tomcat
    • 3.2 创建访问tomcat服务的Ingress
    • 3.3 访问tomcat服务
  • 4. 总结

1. Nginx Ingress Controller概述

Nginx Ingress Controller 是 Kubernetes Ingress Controller 的一种实现,作为反向代理将外部流量导入集群内部,实现将 Kubernetes 内部的 Service 暴露给外部,这样我们就能通过公网或内网直接访问集群内部的服务。

2. 部署Nginx Ingress Controller

2.1 下载yaml文件

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

2.2 修改mandatory.yaml文件

`apiVersion`: apps/v1
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:
      serviceAccountName: nginx-ingress-serviceaccount
      hostNetwork: true
      nodeSelector:
        custom/ingress-controller-ready: "true"
      containers:
        - name: nginx-ingress-controller
          #image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.25.1
          image: registry.cn-hangzhou.aliyuncs.com/kubeapps/quay-nginx-ingress-controller:0.25.1
          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 -> 33
            runAsUser: 33
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - name: http
              containerPort: 80
            - name: https
              containerPort: 443
          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

主要修改如下几个地方:

  1. 将Kind从deployment修改为DaemonSet
    kind: DaemonSet
  2. 注释或删除掉
    #replicas: 1
  3. 添加:
    hostNetwork: true
  4. 添加:
    nodeSelector:
    custom/ingress-controller-ready: “true”

2.3 给节点打标签

#请将k8s-node1替换为你自己k8s集群中某个节点的hostname

kubectl label nodes k8s-node1 custom/ingress-controller-ready=true

2.4 部署

kubectl apply -f mandatory-daemonset.yaml

3. 示例验证

3.1 使用以下yaml文件部署Tomcat

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myweb
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: harbor/tomcat:8.5-jre8
        ports:
        - containerPort: 80

3.2 创建访问tomcat服务的Ingress

apiVersion: networking.k8s.io/v1beta1 # for versions before 1.14 use extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: tomcat
          servicePort: 80

3.3 访问tomcat服务

使用运行nginx ingress controllers的节点的IP进行访问:http://{node-ip}
Nginx Ingress Controller简单使用教程_第1张图片

4. 总结

虽然完成了验证,但是对Kubernetes的Ingress和Ingress Controllers仍然只是了解一些皮毛而已,还需要继续深入的去学习。

你可能感兴趣的:(Kubernetes)