美文网首页K8sk8s
kubernetes部署ingress-nginx-contro

kubernetes部署ingress-nginx-contro

作者: andrewkk | 来源:发表于2021-04-01 17:18 被阅读0次
    Ingress部署流程:
    1.部署Ingress
    1.1部署ingress-nginx
    1.2部署Service
    
    2.部署应用
    2.1部署Service与Pod
    2.2创建Ingress
    
    在Kubernetes中,服务和Pod的IP地址仅在集群内部网络内部使用,对于集群的应用是不可见的。
    为了使外部的应用能够访问集群内的服务,在Kubernetes目前提供了以下几种方案:
    1)NodePort
    2)LoadBalancer
    3)Ingress
    
    1)Ingress组成
    Ingress 是反向代理规则,用来规定 HTTP/S 请求应该被转发到哪个 Service 上,比如根据请求中不同的 Host 和 url 路径
    让请求落到不同的 Service 上;
    Ingress Controller 就是一个反向代理程序,它负责解析 Ingress 的反向代理规则,如果 Ingress 有增删改的变动,
    所有的 Ingress Controller 都会及时更新自己相应的转发规则,当 Ingress Controller 收到请求后就会根据这些规则
    将请求转发到对应的 Service;
    
    2)Ingress工作原理
    1)Ingress controller通过与Kubernetes api进行交互,动态的感知集群中Ingress规则的变化;
    2)然后读取它,按照自定义的规则,规则就是写明了哪个域名对应哪个service,生成一段nginx配置;
    3)再写到nginx-ingress-controller的pod里,这个Ingress controller的pod里运行着一个Nginx服务,
    控制器会把生成的nginx配置写入/etc/nginx.conf文件中;
    4)然后reload一下使配置生效。以此达到域名分别配置和动态更新的问题;
    
    3) Ingress可以解决什么问题?
    1)动态配置服务
    如果是按照传统方式,当新增加一个服务时,我们可能需要在流量入口部署一台反向代理服务器指向我们新的K8s服务,
    而如果使用了Ingress,则只需配置好这个服务,当服务启动时,便会自动注册到Ingress中,不需要额外的操作;
    2)减少不必要的端口映射
    配置过k8s的都清楚, 第一步是要关闭防火墙的, 主要原因是k8s的很多服务会以NodePort方式映射出去, 这样就相当于给宿主机打了很多孔, 
    既不安全也不优雅. 而Ingress可以避免这个问题, 除了Ingress自身服务可能需要映射出去, 其他服务都不用NodePort方式;
    
    
    Ingress作用:
    ingress 仅是用于定义流量转发和调度的通用格式的配置信息,
    它们需要转换为特定的具有http协议转发的和调度功能的应用程序(如nginx、haproxy、traefik等)
    的配置文件,并由响应 的应用程序生成响应的配置文件完成流量转发。
    
    
    1.部署ingress-nginx
    
    [16:57:13root@k8s-master-1 ~/nfs/lk/nginx-ingress]#cat ingress-nginx-controller.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:
          - "extensions"
        resources:
          - ingresses
        verbs:
          - get
          - list
          - watch
      - apiGroups:
          - ""
        resources:
          - events
        verbs:
          - create
          - patch
      - apiGroups:
          - "extensions"
        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:
          serviceAccountName: nginx-ingress-serviceaccount
          containers:
            - name: nginx-ingress-controller
              image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.24.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
    
    
    2.部署Service
    [16:58:27root@k8s-master-1 ~/nfs/lk/nginx-ingress]#cat ingress-svc.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: ingress
      namespace: ingress-nginx
    spec:
      selector:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
      ports:
      - name: http
        port: 80
        nodePort: 40080
      - name: https
        port: 443
        nodePort: 40443
      type: NodePort
    注释:此时已部署完ingress-nginx-controller,具有7层代理功能
    
    
    3.部署Pod与Service
    [16:58:48root@k8s-master-1 ~/nfs/lk/nginx-ingress]#cat nginx.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
      namespace: kevin
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
          rel: beta
      template:
        metadata:
          namespace: kevin
          labels:
            app: nginx
            rel: beta
        spec:
          containers:
          - name: myapp
            image: nginx:alpine
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
      namespace: kevin
    spec:
      selector:
        app: nginx
        rel: beta
      ports:
      - name: http
        port: 80
        targetPort: 80
    
    
    4.创建Ingress
    [16:58:30root@k8s-master-1 ~/nfs/lk/nginx-ingress]#cat myapp-ingress.yaml 
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: nginx
      namespace: kevin
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /
        kubernetes.io/ingress.class: "nginx"
    spec:
      rules:
      - host: www.kevinxxx.com
        http:
          paths:
          - path: /
            backend:
              serviceName: nginx
              servicePort: 80
    
    
    [17:06:08root@k8s-master-1 ~/nfs/lk/nginx-ingress]#kubectl get pods -n ingress-nginx
    NAME                                        READY   STATUS    RESTARTS   AGE
    nginx-ingress-controller-59b9f5d675-v758z   1/1     Running   0          65m
    [17:08:20root@k8s-master-1 ~/nfs/lk/nginx-ingress]#kubectl get svc -n ingress-nginx
    NAME      TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
    ingress   NodePort   10.66.113.207   <none>        80:40080/TCP,443:40443/TCP   64m
    [17:08:26root@k8s-master-1 ~/nfs/lk/nginx-ingress]#kubectl get pod -n kevin -o wide
    NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
    nginx-5db4499c6c-2wl4r   1/1     Running   0          23m   10.80.0.232   k8s-master-1   <none>           <none>
    nginx-5db4499c6c-7zlpf   1/1     Running   0          23m   10.80.2.32    k8s-node-1     <none>           <none>
    [17:08:31root@k8s-master-1 ~/nfs/lk/nginx-ingress]#kubectl get svc -n kevin
    NAME    TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
    nginx   ClusterIP   10.66.17.91   <none>        80/TCP    23m
    [17:08:37root@k8s-master-1 ~/nfs/lk/nginx-ingress]#kubectl  get ingress -n kevin
    NAME    CLASS    HOSTS              ADDRESS   PORTS   AGE
    nginx   <none>   www.kevinxxx.com             80      55m
    [17:08:42root@k8s-master-1 ~/nfs/lk/nginx-ingress]#kubectl describe ingress -n kevin
    Name:             nginx
    Namespace:        kevin
    Address:          
    Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
    Rules:
      Host              Path  Backends
      ----              ----  --------
      www.kevinxxx.com  
                        /   nginx:80 (10.80.0.232:80,10.80.2.32:80)
    Annotations:        kubernetes.io/ingress.class: nginx
                        nginx.ingress.kubernetes.io/rewrite-target: /
    Events:
      Type    Reason  Age   From                      Message
      ----    ------  ----  ----                      -------
      Normal  CREATE  55m   nginx-ingress-controller  Ingress kevin/nginx
    
    
    image.png image.png

    相关文章

      网友评论

        本文标题:kubernetes部署ingress-nginx-contro

        本文链接:https://www.haomeiwen.com/subject/nantkltx.html