美文网首页
ingress-nginx常用

ingress-nginx常用

作者: 慕知 | 来源:发表于2021-04-06 21:01 被阅读0次

    https://kubernetes.github.io/ingress-nginx/

    image.png

    1,ingress 重定向

    [root@\ k8s_master~]# vim dep-v2.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: test
    spec:
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
            - name: nginx
              image: nginx
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: wordpress-nginx
    spec:
      ports:
        - name: http
          port: 80
          targetPort: 80
      selector:
        app: nginx
    
    
    
    
    
    [root@\ k8s_master~]# vim ngin_ingress.yaml
    kind: Ingress
    apiVersion: extensions/v1beta1
    metadata:
      name: ingress-ingress-nginx-tls
      annotations:
        kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/permanent-redirect: https://www.taobao.com
        #nginx.ingress.kubernetes.io/rewrite-target: https://www.baidu.com/s?wd=nginx
    spec:
      rules:
        - host: www.test-nginx.com
          http:
            paths:
              - path: /
                backend:
                  serviceName: wordpress-nginx
                  servicePort: 80
    
    
    
    
    
    [root@\ k8s_master~]# kubectl get ingress
    NAME                        CLASS    HOSTS                      ADDRESS         PORTS   AGE
    ingress-ingress             <none>   www.test.com,www.abc.com   192.168.15.33   80      4d9h
    ingress-ingress-nginx-tls   <none>   www.test-nginx.com         192.168.15.33   80      28m
    
    [root@\ k8s_master~]# kubectl get svc -n ingress-nginx 
    NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
    ingress-nginx-controller             NodePort    10.102.254.20   <none>        80:30654/TCP,443:31066/TCP   4d4h
    ingress-nginx-controller-admission   ClusterIP   10.111.163.84   <none>        443/TCP       
    
    
    
    # 访问http://www.test-nginx.com:30654/
    
    
    跳转到百度

    2,使用正则

    [root@\ k8s_master~]# vim ngin_ingress.yaml 
    kind: Ingress
    apiVersion: extensions/v1beta1
    metadata:
      name: ingress-ingress-nginx-tls
      annotations:
        kubernetes.io/ingress.class: "nginx"
        #nginx.ingress.kubernetes.io/permanent-redirect: https://www.taobao.com
        nginx.ingress.kubernetes.io/rewrite-target: https://www.baidu.com/s?wd=$1
    spec:
      rules:
        - host: www.test-nginx.com
          http:
            paths:
              - path: /search/(.+)
                backend:
                  serviceName: wordpress-nginx
                  servicePort: 80
    
    
    
    访问 http://www.test-nginx.com:30654/search/nginx
    
    
    image.png

    3,auth认证登录

    https://kubernetes.github.io/ingress-nginx/examples/auth/basic/

    1,
    [root@\ k8s_master~]# mkdir /auth
    [root@\ k8s_master~]# cd /auth/
    
    [root@\ k8s_master/auth]# htpasswd -c auth foo
    -bash: htpasswd: command not found
    [root@\ k8s_master/auth]# yum install -y httpd-tools 
    
    
    2,
    [root@\ k8s_master/auth]# htpasswd -c auth foo
    New password: 
    Re-type new password: 
    Adding password for user foo
    [root@\ k8s_master/auth]# ll
    total 4
    -rw-r--r--. 1 root root 42 Apr  6 20:46 auth
    [root@\ k8s_master/auth]# kubectl create secret generic basic-auth --from-file=auth
    secret/basic-auth created
    
    
    
    3, 查看
    [root@\ k8s_master/auth]# kubectl get secret basic-auth -o yaml
    apiVersion: v1
    data:
      auth: Zm9vOiRhcHIxJFYxTzFjL1YzJFM2UjQzN0c0VXQ5TERWY3BOYktNeC8K
    kind: Secret
    metadata:
      creationTimestamp: "2021-04-06T12:46:56Z"
      managedFields:
      - apiVersion: v1
        fieldsType: FieldsV1
        fieldsV1:
          f:data:
            .: {}
            f:auth: {}
          f:type: {}
        manager: kubectl-create
        operation: Update
        time: "2021-04-06T12:46:56Z"
      name: basic-auth
      namespace: default
      resourceVersion: "289474"
      uid: a4064334-aa56-4142-97b8-2447e234ccb4
    type: Opaque
    
    [root@\ k8s_master/auth]# cat auth 
    foo:$apr1$V1O1c/V3$S6R437G4Ut9LDVcpNbKMx/
    
    
    
    
    [root@\ k8s_master~]# vim ngin_ingress.yaml 
    kind: Ingress
    apiVersion: extensions/v1beta1
    metadata:
      name: ingress-ingress-nginx-tls
      annotations:
        kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/auth-type: basic
        nginx.ingress.kubernetes.io/auth-secret: basic-auth
    spec:
      rules:
        - host: www.test-nginx.com
          http:
            paths:
              - path: /
                backend:
                  serviceName: wordpress-nginx
                  servicePort: 80
    
    
    
    image.png image.png image.png

    相关文章

      网友评论

          本文标题:ingress-nginx常用

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