莫问天涯路几重,轻衫侧帽且从容。这篇文章主要讲述#yyds干货盘点#kubernetes入门基础-Ingress的概念与使用相关的知识,希望能为你提供帮助。
一、相关概念
- NodePort方式的缺点是会占用很多集群的端口,那么集群服务器变多的时候,这个缺点愈发明显
- LoadBalancer的缺点是每个Service需要一个LoadBalancer,浪费、麻烦,并且需要k8s之外的设备支持
文章图片
- Ingress:k8s中的一个抽象,作用是定义请求如何转发到Service的规则
- Ingress controller:具体实现反向代理及负载均衡的程序,对Ingress定义的规则进行解析,根据配置规则来实现转发,实现方式有很多种,比如nginx、Controller、Haproxy等等
-
- 用户编写Ingress规则,说明哪个域名对应k8s集群的哪个Seivice
-
- Ingress控制器动态感知Ingress服务规则的变化,然后生成一段对应的Nginx反向代理配置
-
- Ingress控制器将会生成的Nginx配置写入到一个运行着的Nginx服务中,并且动态更新
-
- 到此为止,其实真正在工作的就是一个Nginx了,内部配置了用户定义的请求转发规则
将以下两个资源清单文件保存到服务器
# 创建目录
mkdir ingress-controller
# 进入资源目录
cd ingress-controller
# mandatory
wget https://github.com/kubernetes/ingress-nginx/blob/nginx-0.30.0/deploy/static/mandatory.yaml
# service-nodeport文件
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
执行kubectl创建资源命令
kubectl apply -f ./
2. 查看Ingress Controller
查看Pod:
kubectl get pods -n ingress-nginx
,返回以下内容NAMEREADYSTATUSRESTARTSAGE
nginx-ingress-controller-5bb8fb4bb6-qnbdv1/1Running03m23s
查看Service:
kubectl get svc -n ingress-nginx
,返回以下内容NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE
ingress-nginxNodePort10.99.166.208<
none>
80:30796/TCP,443:31737/TCP5m22s
3. 创建Deployment和Service
创建3个Nginx和3个tomcat应用和对应的两个Service,创建
tomcat-nginx.yaml
文件,添加以下内容:apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80---apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: tomcat-pod
template:
metadata:
labels:
app: tomcat-pod
spec:
containers:
- name: tomcat
image: tomcat:8.5-jre10-slim
ports:
- containerPort: 8080---apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: dev
spec:
selector:
app: nginx-pod
clusterIP: None
type: ClusterIP
ports:
- port: 80
targetPort: 80---apiVersion: v1
kind: Service
metadata:
name: tomcat-service
namespace: dev
spec:
selector:
app: tomcat-pod
clusterIP: None
type: ClusterIP
ports:
- port: 8080
targetPort: 8080
4. 创建ingress-http
创建
ingress-http.yaml
,添加以下内容apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-http
namespace: dev
spec:
rules:
- host: nginx.jkdev.cn
http:
paths:
- path: /
backend:
serviceName: nginx-service
servicePort: 80
- host: tomcat.jkdev.cn
http:
paths:
- path: /
backend:
serviceName: tomcat-service
servicePort: 8080
使用以下命令进行创建
kubectl create -f ingress-http.yaml
创建之后使用以下命令查看
kubectl get ingress -n dev
# 或
kubectl get ing -n dev
或使用以下命令指定ingress名称查看
kubectl get ingress ingress-http -n dev
# 或
kubectl get ing ingress-http -n dev
或使用
kubectl describe ingress -n dev
指令查看详细信息,返回以下详细内容Name:ingress-http
Namespace:dev
Address:10.99.166.208
Default backend:default-http-backend:80 (<
error: endpoints "default-http-backend" not found>
)
Rules:
HostPathBackends
----------------
nginx.jkdev.cn
/nginx-service:80 (10.244.1.20:80,10.244.1.21:80,10.244.1.24:80)
tomcat.jkdev.cn
/tomcat-service:8080 (10.244.1.22:8080,10.244.1.23:8080,10.244.1.25:8080)
Annotations:<
none>
Events:
TypeReasonAgeFromMessage
-------------------------
NormalCREATE21mnginx-ingress-controllerIngress dev/ingress-http
NormalUPDATE21mnginx-ingress-controllerIngress dev/ingress-http
5. 创建ingress-https
生成ssl证书以及密钥
# 生成证书
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/C=CN/ST=GZ/O=nginx/CN=jkdev.cn"
# 创建密钥
kubectl create secret tls tls-secret --key tls.key --cert tls.crt
添加
ingress-https.yaml
,添加以下内容apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-https
namespace: dev
spec:
tls:
- hosts:
- nginx.jkdev.cn
- tomcat.jkdev.cn
secretName: tls-secret # 指定密钥,必须和创建的密钥名称一致
rules:
- host: nginx.jkdev.cn
http:
paths:
- path: /
backend:
serviceName: nginx-service
servicePort: 80
- host: tomcat.jkdev.cn
http:
paths:
- path: /
backend:
serviceName: tomcat-service
servicePort: 8080
三、Nginx转发到k8s使用Nginx将来自外网的请求转发给k8s集群
upstream default_backend_traefix
server 10.99.166.208:80 max_fails=3 fail_timeout=10s;
server
listen 80;
server_name *.jkdev.cn;
location /
proxy_pass http://default_backend_traefix;
proxy_set_header Host $http_host;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
推荐阅读
- MySql笔记
- NBU的stroage policy执行deuplication动作时报错
- Linux快照利器(Timeshift,备份和还原效果杠杠的!)
- #yyds干货盘点#ceph安装可视化dashboard
- JAVA实现短网址服务的原理是什么()
- 使用Trib工具自动化部署Redis Cluster三主三从交叉复制集群#yyds干货盘点#
- #yyds干活盘点# 5 Css3 文本效果
- Set VM SCSI Controller Bus Sharing Mode
- # yyds干货盘点 # 手把手教你使用JavaScript打造一款扫雷游戏