• kubernetes 之ingress


    1、Ingress组件介绍

    • Ingress解决的是新的服务加入后,域名和服务的对应问题,基本上是一个ingress的对象,通过yaml进行创建和更新进行加载。
    • Ingress Controller是将Ingress这种变化生成一段Nginx的配置,然后将这个配置通过Kubernetes API写到Nginx的Pod中,然后reload.

    2、生成一个默认的后端,如果遇到解析不到的URL就转发到默认后端页面

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: default-http-backend
      labels:
        app: default-http-backend
      namespace: ingress-nginx
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: default-http-backend
        spec:
          terminationGracePeriodSeconds: 60
          containers:
          - name: default-http-backend
            # Any image is permissable as long as:
            # 1. It serves a 404 page at /
            # 2. It serves 200 on a /healthz endpoint
            image: registry.cn-hangzhou.aliyuncs.com/google_containers/defaultbackend:1.4
            livenessProbe:
              httpGet:
                path: /healthz
                port: 8080
                scheme: HTTP
              initialDelaySeconds: 30
              timeoutSeconds: 5
            ports:
            - containerPort: 8080
            resources:
              limits:
                cpu: 10m
                memory: 20Mi
              requests:
                cpu: 10m
                memory: 20Mi
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: default-http-backend
      namespace: ingress-nginx
      labels:
        app: default-http-backend
    spec:
      ports:
      - port: 80
        targetPort: 8080
      selector:
        app: default-http-backend

    [root@manager Ingress]# kubectl create -f default-backend.yaml
    deployment "default-http-backend" created
    service "default-http-backend" created
    [root@manager Ingress]#

    [root@manager Ingress]# kubectl get pods -n ingress-nginx -o wide
    NAME READY STATUS RESTARTS AGE IP NODE
    default-http-backend-5d8887b7f-l8r29 1/1 Running 0 28s 10.0.91.6 192.168.10.221

    3、部署Ingress Controller

    [root@manager Ingress]# cat nginx-ingress-controller.yaml 
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: nginx-ingress-lb
      labels:
        name: nginx-ingress-lb
      namespace: ingress-nginx
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            name: nginx-ingress-lb
          annotations:
            prometheus.io/port: '10254'
            prometheus.io/scrape: 'true'
        spec:
          terminationGracePeriodSeconds: 60
          hostNetwork: true
          containers:
          - image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:0.8.3
            name: nginx-ingress-lb
            readinessProbe:
              httpGet:
                path: /healthz
                port: 10254
                scheme: HTTP
            livenessProbe:
              httpGet:
                path: /healthz
                port: 10254
                scheme: HTTP
              initialDelaySeconds: 10
              timeoutSeconds: 1
            ports:
            - containerPort: 80
              hostPort: 80
            - containerPort: 443
              hostPort: 443
            env:
              - name: POD_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.name
              - name: POD_NAMESPACE
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
              - name: KUBERNETES_MASTER
                value: http://192.168.10.220:8080
            args:
            - /nginx-ingress-controller
            - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
    #        - --apiserver-host=http://192.168.10.220:8080

    [root@manager Ingress]# kubectl get pod -n ingress-nginx -o wide
    NAME READY STATUS RESTARTS AGE IP NODE
    default-http-backend-5d8887b7f-l8r29 1/1 Running 0 21m 10.0.91.6 192.168.10.221
    nginx-ingress-lb-gj4sg 1/1 Running 0 1m 192.168.10.221 192.168.10.221

     

    4、本实验通过nginx 容器,这里通过ingress 做入口访问svc

    [root@manager ~]# cat nginx-deployment.yaml 
    apiVersion: apps/v1beta2
    kind: Deployment
    metadata:
      name: nginx-deployment
      namespace: ingress-nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.10
            ports:
            - containerPort: 80

    [root@manager ~]# kubectl create -f nginx-deployment.yaml

    [root@manager ~]# cat nginx-service.yaml
    apiVersion: v1
    kind: Service
    metadata:
    name: nginx-service
    namespace: ingress-nginx
    labels:
    app: nginx
    spec:
    ports:
    - port: 88
    targetPort: 80
    selector:
    app: nginx


    [root@manager ~]# kubectl create -f nginx-service.yaml

    [root@manager ~]# cat ingress-nginx.yaml
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
    name: nginxservice
    namespace: ingress-nginx
    spec:
    rules:
    - host: test.io
    http:
    paths:
    - path: /
    backend:
    serviceName: nginx-service
    servicePort: 80
    - path: /
    # backend:
    # serviceName: kubernetes-dashboard
    # servicePort: 80
    [root@manager ~]# kubectl create -f ingress-nginx.yaml

    本机写host 文件进行访问,外部写host 文件,指向node ip 即可

  • 相关阅读:
    【PHP】最详细PHP从入门到精通(二)——PHP中的函数
    利用 MUI开发app, 如何实现侧滑菜单及其主体部分上下滑动
    JavaScript面向对象编程—this详解
    利用Hadoop streaming 进行词频统计
    Java 平时作业七
    Java 平时作业六
    Java 平时作业五
    Java 平时作业四
    Java 平时作业三
    JAVA 平时作业二
  • 原文地址:https://www.cnblogs.com/hellojackyleon/p/8420911.html
Copyright © 2020-2023  润新知