• k8s ingressd的http对外暴露网站


    1.ingress对外暴露网站

    • 创建

      kubectl apply -f web-ingress.yaml
      
    • 查看

      kubectl get ingress
      

      测试:本地电脑绑定hosts记录对应ingress里面配置的域名

      例如:<Ingress Controller Pod所在Node IP> foo.bar.com

    • 示例yaml配置文件

      [root@k8s-master ingress]# vim web-ingress.yaml 
      [root@k8s-master ingress]# cat web-ingress.yaml 
      apiVersion: networking.k8s.io/v1     # 指定ingresses的api接口
      kind: Ingress       # 使用ingress模块
      metadata:
        name: web1        # ingresses 名称
      spec:
        rules:
        - host: web.scajy.cn   # ingresses的域名
          http:
            paths:
            - path: /
              pathType: Prefix
              backend:
                service:
                  name: web1    # 关联的service服务的名称
                  port:
                    number: 80
      

    2. 案例

    2.1 我们先配置一个web的pod和service的服务yaml文件

    [root@k8s-master ingress]# vim web.yaml 
    [root@k8s-master ingress]# cat web.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web1
      namespace: default
    spec:
      replicas: 3 # Pod副本预期数量
      selector:
        matchLabels:
          app: web1
      template:
        metadata:
          labels:
            app: web1 # Pod副本的标签
        spec:
          containers:
          - name: web
            image: nginx:1.18
    
    
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: web1
      name: web1
    spec:
      type: ClusterIP # 服务类型
      ports:
      - port: 80 # Service端口
        protocol: TCP # 协议
        targetPort: 80 # 容器端口
      selector:
        app: web1 # 指定关联Pod的标签
    
    

    2.2 启动pod和service的服务yaml文件

    [root@k8s-master ingress]# kubectl apply -f web.yaml 
    deployment.apps/web created
    service/web created
    

    2.3 检查服务是否正常

    [root@k8s-master ingress]# kubectl get pod,service
    NAME                         READY   STATUS              RESTARTS   AGE
    pod/nginx-6799fc88d8-h4r2z   1/1     Running             0          28m
    pod/web1-d59d4b5f5-2xs8z     0/1     ContainerCreating   0          13s
    pod/web1-d59d4b5f5-42btp     0/1     ContainerCreating   0          13s
    pod/web1-d59d4b5f5-z4wcb     0/1     ContainerCreating   0          13s
    
    NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        47m
    service/nginx        NodePort    10.101.67.75   <none>        80:31622/TCP   28m
    service/web1         ClusterIP   10.104.55.65   <none>        80/TCP         13s
    
    [root@k8s-master ingress]# curl 10.104.55.65
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    

    2.4 配置ingress对外暴露

    [root@k8s-master ingress]# vim web-ingress.yaml 
    [root@k8s-master ingress]# cat web-ingress.yaml 
    apiVersion: networking.k8s.io/v1     # 指定ingresses的api接口
    kind: Ingress       # 使用ingress模块
    metadata:
      name: web1        # ingresses 名称
    spec:
      rules:
      - host: web.scajy.cn   # ingresses的域名
        http:
          paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web1    # 关联的service服务的名称
                port:
                  number: 80
    

    2.5 启动ingress配置文件

    [root@k8s-master ingress]# kubectl apply -f web-ingress.yaml 
    ingress.networking.k8s.io/web1 created
    

    2.6 验证服务是否正常

    [root@k8s-master ingress]# kubectl get ingress
    Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
    NAME   CLASS    HOSTS          ADDRESS   PORTS   AGE
    web1   <none>   web.scajy.cn             80      16m
    

    2.7 绑定hosts记录

    [root@k8s-master ~]#  kubectl get pods -n ingress-nginx -o wide
    NAME                                       READY   STATUS    RESTARTS   AGE     IP              NODE        NOMINATED NODE   READINESS GATES
    nginx-ingress-controller-5dc64b58f-ctjfd   1/1     Running   0          2m45s   192.168.0.202   k8s-node1   <none>           <none>
    

    image

    2.8 浏览器测试

    image

  • 相关阅读:
    数组越界的现象
    找出数组中的最大值
    Django Form和ModelForm组件
    AJAX
    Mysql数据库概述
    Django中的session和cookie及分页设置
    Django-admin组件使用
    Django中间件
    MySQL视图、触发器、事务、存储过程、函数
    机器学习基础算法__python实现(基于numpy等基础库)
  • 原文地址:https://www.cnblogs.com/scajy/p/15533334.html
Copyright © 2020-2023  润新知