Kubernetes - 配置Nginx-Ingress 作为服务发现
- 添加 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
- 配置文件, 有二个在默认空间下web服务和api服务分别映射到自定义端口 9001, 9002
- 配置 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: 20Mikubectl 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-backendkubectl 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
- 配置文件
- 配置 Ingress Deployment, 暴露自定义的端口, 并指定 tcp-services-configmap 来导入我们在上一步配置的端口映射
- 配置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';
}
}
}
- 添加 Upstream 配置
Kubernetes - 配置Nginx-Ingress 作为服务发现的更多相关文章
- kubernetes 简介:kube-dns 和服务发现
服务发现 kubernetes 提供了 service 的概念可以通过 VIP 访问 pod 提供的服务,但是在使用的时候还有一个问题:怎么知道某个应用的 VIP?比如我们有两个应用,一个 app,一 ...
- 15、基于consul+consul-template+registrator+nginx实现自动服务发现
一.架构图 二.组件介绍 1.Registrator Registrator:一个由Go语言编写的,针对docker使用的,通过检查本机容器进程在线或者停止运行状态,去注册服务的工具.所以我们要做的实 ...
- Kubernetes 系列(三):Kubernetes使用Traefik Ingress暴露服务
一.Kubernetes 服务暴露介绍 从 kubernetes 1.2 版本开始,kubernetes提供了 Ingress 对象来实现对外暴露服务:到目前为止 kubernetes 总共有三种暴露 ...
- Kubernetes如何使用kube-dns实现服务发现
大纲: • Kubernetes中如何发现服务 • 如何发现Pod提供的服务 • 如何使用Service发现服务 • 如何使用kube-dns发现服务 ...
- Kubernetes 部署 Nginx Ingress Controller 之 nginxinc/kubernetes-ingress
更新:这里用的是 nginxinc/kubernetes-ingress ,还有个 kubernetes/ingress-nginx ,它们的区别见 Differences Between nginx ...
- kubernetes进阶(三)服务发现-coredns
服务发现,说白了就是服务(应用)之间相互定位的过程. 服务发现需要解决的问题: 1.服务动态性强--容器在k8s中ip变化或迁移 2.更新发布频繁--版本迭代快 3.支持自动伸缩--大促或流量高峰 我 ...
- Kubernetes中使用ClusterDNS进行服务发现
在k8s集群中,服务是运行在Pod中的,Pod的发现和副本间负载均衡是我们面临的问题.我们使用Service解决了负载均衡的问题,但是集群环境中,service经常伴随着ip的变动而变动,得益于kub ...
- 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% ...
- Prometheus在Kubernetes下的服务发现机制
Prometheus作为容器监控领域的事实标准,随着以Kubernetes为核心的云原生热潮的兴起,已经得到了广泛的应用部署.灵活的服务发现机制是Prometheus和Kubernetes两者得以连接 ...
- Kubernetes 服务发现
目录 什么是服务发现? 环境变量 DNS 服务 Linux 中 DNS 查询原理 Kubernetes 中 DNS 查询原理 调试 DNS 服务 存根域及上游 DNS 什么是服务发现? 服务发现就是一 ...
随机推荐
- [LeetCode169]Majority Element
Majority Element Total Accepted: 58500 Total Submissions: 163658My Submissions Question Solution Gi ...
- Codeforces Round #359 (Div. 2) B
B. Little Robber Girl's Zoo time limit per test 2 seconds memory limit per test 256 megabytes input ...
- .net 网站首页,本次的项目中用到的一个网站首页中统计网页访问量的工具方法,我觉得它应该在pagebase里面,拿来用一下
需要建立一个根文件夹 ~/xml/couter.txt #region 网站访问量 protected void pageviews() { int count ...
- 2018多校第三场 hdu6331 M :Walking Plan
题目链接 hdu6331 自我吐槽,这场多校大失败,开局签到因输入输出格式写错,wa了3发.队友C题wa了1个小时,还硬说自己写的没错,结果我随便造了个小数据,他都没跑对.然后跑对了后又进入了无限的卡 ...
- 学习webservice
客户端测试页: WebService代码: using System; using System.Collections.Generic; using System.Linq; using Syste ...
- OpenGL 之 坐标变换
http://www.cnblogs.com/irvinow/archive/2009/11/20/1606496.html 创建OpenGL模型过程: OPENGL坐标变换很有特点,为了简单描述先定 ...
- linux用户态定时器的使用---19【转】
转自:http://www.cnblogs.com/zxouxuewei/p/5095288.html 原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuew ...
- hdu 5102(巧妙的搜索)
The K-th Distance Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- poj 1389(离散化+计算几何)
Area of Simple Polygons Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3412 Accepted ...
- 【原创】配置Windows Live Writer,写cnblogs博客
20180115更新补充: 现在live writer已经改名open live writer了,需要去下载的到地址:http://openlivewriter.org/ 引言 以前写博客一般都是联网 ...