• 添加 Kubernetes ConfigMap配置来自定义端口与服务的映射关系

    • 配置文件, 有二个在默认空间下web服务和api服务分别映射到自定义端口 9001, 9002

       apiVersion: v1
      kind: ConfigMap
      metadata:
      name: mysite-configmap
      data:
      9000: "default/web:8080"
      9001: "default/api:8080"
    • 在kubernetes Master服务器应用 ConfigMap 配置
      kubectl apply -f mysite-configmap.yml
  • 配置 Default Backend
    • kind: Deployment
      apiVersion: extensions/v1beta1
      metadata:
      name: default-http-backend
      spec:
      revisionHistoryLimit: 10
      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: defaultbackend:1.0
      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
      kubectl apply -f default-backend-deployment.yml
      ---
      
      apiVersion: v1
      kind: Service
      metadata:
      name: default-http-backend
      labels:
      app: default-http-backend
      spec:
      ports:
      - port: 8080
      protocol: TCP
      targetPort: 8080
      selector:
      app: default-http-backend
      kubectl apply -f default-backend-service.yml
  • 配置Ingress
    • 配置 Ingress Deployment, 暴露自定义的端口, 并指定 tcp-services-configmap 来导入我们在上一步配置的端口映射

      • 配置文件,

         apiVersion: extensions/v1beta1
        kind: Deployment
        metadata:
        name: nginx-ingress-deployment
        labels:
        k8s-app: nginx-ingress-lb
        spec:
        revisionHistoryLimit: 10
        replicas: 1
        template:
        metadata:
        labels:
        k8s-app: nginx-ingress-lb
        name: nginx-ingress-lb
        spec:
        terminationGracePeriodSeconds: 60
        containers:
        - image: nginx-ingress-controller:0.8.3
        name: nginx-ingress-lb
        imagePullPolicy: Always
        readinessProbe:
        httpGet:
        path: /healthz
        port: 10254
        scheme: HTTP
        livenessProbe:
        httpGet:
        path: /healthz
        port: 10254
        scheme: HTTP
        initialDelaySeconds: 10
        timeoutSeconds: 1
        # use downward API
        env:
        - name: POD_NAME
        valueFrom:
        fieldRef:
        fieldPath: metadata.name
        - name: POD_NAMESPACE
        valueFrom:
        fieldRef:
        fieldPath: metadata.namespace
        ports:
        - containerPort: 9000
        protocol: TCP
        - containerPort: 9001
        protocol: TCP args:
        - /nginx-ingress-controller
        - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
        - --tcp-services-configmap=$(POD_NAMESPACE)/mysite-configmap
      • 应用配置
        kubectl apply -f nginx-ingress-deployment.yml
    • 配置 Ingress Service, 配置自定义端口与ConfigMap的端口映射关系及服务名称 
      • 配置文件

         ---
        
         apiVersion: v1
        kind: Service
        metadata:
        name: nginx-ingress-lb
        labels:
        k8s-app: nginx-ingress-lb
        spec:
        type: NodePort
        ports:
        - port: 9000
        protocol: TCP
        targetPort: 9000
        nodePort: 30005
        name: web
        - port: 9001
        protocol: TCP
        targetPort: 9001
        nodePort: 30006
        name: api selector:
        k8s-app: nginx-ingress-lb
      • 应用配置
        kubectl apply -f nginx-ingress-service.yml
  • 配置Nginx 反向代理
    • 添加 Upstream 配置

       upstream web {
      server my-server-1:3005 max_fails=1 fail_timeout=10s;
      server my-server-2:3005 max_fails=1 fail_timeout=10s;
      } upstream api {
      server my-server-1:3006 max_fails=1 fail_timeout=10s;
      server my-server-2:3006 max_fails=1 fail_timeout=10s;
      } server {
      listen 80;
      listen 443 ssl; ssl_certificate /etc/nginx/conf.d/cert/wildcard.mysite.pem;
      ssl_certificate_key /etc/nginx/conf.d/cert/wildcard.mysite.key;
      location / {
      proxy_pass http://web;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      } location ~^/(api) {
      proxy_pass http://api;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; if ($http_origin ~* (^(https?://(?:.+\.)?mysite\.com)$)) {
      set $cors "CORS";
      set $cors_method "${cors}_${request_method}";
      } if ($cors_method = "CORS_OPTIONS") {
      add_header 'Access-Control-Allow-Origin' '$http_origin';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
      #
      # Custom headers and headers various browsers *should* be OK with but aren't
      #
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      #
      # Tell client that this pre-flight info is valid for 20 days
      #
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain charset=UTF-8';
      add_header 'Content-Length' 0;
      return 204;
      } if ($cors = "CORS") {
      add_header 'Access-Control-Allow-Origin' '$http_origin';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      }
      }
      }

Kubernetes - 配置Nginx-Ingress 作为服务发现的更多相关文章

  1. kubernetes 简介:kube-dns 和服务发现

    服务发现 kubernetes 提供了 service 的概念可以通过 VIP 访问 pod 提供的服务,但是在使用的时候还有一个问题:怎么知道某个应用的 VIP?比如我们有两个应用,一个 app,一 ...

  2. 15、基于consul+consul-template+registrator+nginx实现自动服务发现

    一.架构图 二.组件介绍 1.Registrator Registrator:一个由Go语言编写的,针对docker使用的,通过检查本机容器进程在线或者停止运行状态,去注册服务的工具.所以我们要做的实 ...

  3. Kubernetes 系列(三):Kubernetes使用Traefik Ingress暴露服务

    一.Kubernetes 服务暴露介绍 从 kubernetes 1.2 版本开始,kubernetes提供了 Ingress 对象来实现对外暴露服务:到目前为止 kubernetes 总共有三种暴露 ...

  4. Kubernetes如何使用kube-dns实现服务发现

    大纲: •       Kubernetes中如何发现服务 •       如何发现Pod提供的服务 •       如何使用Service发现服务 •       如何使用kube-dns发现服务 ...

  5. Kubernetes 部署 Nginx Ingress Controller 之 nginxinc/kubernetes-ingress

    更新:这里用的是 nginxinc/kubernetes-ingress ,还有个 kubernetes/ingress-nginx ,它们的区别见 Differences Between nginx ...

  6. kubernetes进阶(三)服务发现-coredns

    服务发现,说白了就是服务(应用)之间相互定位的过程. 服务发现需要解决的问题: 1.服务动态性强--容器在k8s中ip变化或迁移 2.更新发布频繁--版本迭代快 3.支持自动伸缩--大促或流量高峰 我 ...

  7. Kubernetes中使用ClusterDNS进行服务发现

    在k8s集群中,服务是运行在Pod中的,Pod的发现和副本间负载均衡是我们面临的问题.我们使用Service解决了负载均衡的问题,但是集群环境中,service经常伴随着ip的变动而变动,得益于kub ...

  8. nginx动态配置及服务发现那些事

    Reference: http://xiaorui.cc/2016/10/16/nginx%E5%8A%A8%E6%80%81%E9%85%8D%E7%BD%AE%E5%8F%8A%E6%9C%8D% ...

  9. Prometheus在Kubernetes下的服务发现机制

    Prometheus作为容器监控领域的事实标准,随着以Kubernetes为核心的云原生热潮的兴起,已经得到了广泛的应用部署.灵活的服务发现机制是Prometheus和Kubernetes两者得以连接 ...

  10. Kubernetes 服务发现

    目录 什么是服务发现? 环境变量 DNS 服务 Linux 中 DNS 查询原理 Kubernetes 中 DNS 查询原理 调试 DNS 服务 存根域及上游 DNS 什么是服务发现? 服务发现就是一 ...

随机推荐

  1. Codeforces Round #324 (Div. 2) B

    B. Kolya and Tanya time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. poj 2186 强连通入门题目

    每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...

  3. bzoj1266 [AHOI2006]上学路线route floyd+最小割

    1266: [AHOI2006]上学路线route Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 2490  Solved: 898[Submit][S ...

  4. JAVA中GC时finalize()方法是不是一定会被执行?

    在回答上面问题之前,我们一定要了解JVM在进行垃圾回收时的机制,首先: 一.可达性算法  要知道对象什么时候死亡,我们需要先知道JVM的GC是如何判断对象是可以回收的.JAVA是通过可达性算法来来判断 ...

  5. 分享一下我写的.net 2.0的orm类,实现mvc。可以用于webform等环境中,这是orm的原理部分。

    using System;using System.Collections.Generic;using System.Configuration;using System.Data;using Sys ...

  6. 废弃sqlite代码,备查

    using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using T ...

  7. iOS开发者证书申请及应用上线发布详解

    一个小教程登录开发者中心:http://developer.apple.com/ 第零部分:本地生成密钥1.打开mac的钥匙串访问  2.选择钥匙串的证书助理(有些可能是英文的)   3.点击继续后存 ...

  8. Appium+python自动化9-SDK Manager【转载】

    前言 SDK Manager到有哪些东西是必须安装的呢? 一.SDK Manager 1.双击打开SDK Manager界面

  9. Fiddler抓包8-打断点(bpu)【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/fiddler/ 前言 先给大家讲一则小故事,在我们很小的时候是没有手机的,那时候跟女神 ...

  10. linux nbd & qemu-nbd

    网络块设备:  Network Block Device   可以将一个远程主机的磁盘空间,当作一个块设备来使用.就像一块硬盘一样. 使用它,你可以很方便的将另一台服务器的硬盘空间,增加到本地服务器上 ...