-
k8s之Service详解-Ingress使用
环境准备
搭建ingress环境
#创建文件夹 [root@master ~]# mkdir ingress-controller [root@master ~]# cd ingress-controller/ [root@master ingress-controller]# #获取ingress-nginx,本次案例使用的是0.30版本(不挂代理可能无法访问,这里我把yaml文件内容复制上来,需要的可以自行复制粘贴) [root@master ingress-controller]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml [root@master ingress-controller]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml [root@master ingress-controller]# ls mandatory.yaml service-nodeport.yaml
mandatory.yaml
View Code
service-nodeport.yaml
View Code
#创建ingress-nginx [root@master ingress-controller]# kubectl apply -f ./ namespace/ingress-nginx created configmap/nginx-configuration created configmap/tcp-services created configmap/udp-services created serviceaccount/nginx-ingress-serviceaccount created clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created role.rbac.authorization.k8s.io/nginx-ingress-role created rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created deployment.apps/nginx-ingress-controller created limitrange/ingress-nginx created service/ingress-nginx created #查看ingress-nginx [root@master ingress-controller]# kubectl get pod -n ingress-nginx NAME READY STATUS RESTARTS AGE nginx-ingress-controller-7f74f657bd-kzvrq 1/1 Running 0 2m7s #查看service [root@master ingress-controller]# kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx NodePort 10.106.160.9 <none> 80:32000/TCP,443:32421/TCP 5m48s
准备service和pod
为了方便后续的实验,创建下图所示的模型
创建tomcat-nginx.yaml
View Code
使用配置文件
[root@master ingress-controller]# cd .. [root@master ~]# vim tomcat-nginx.yaml [root@master ~]# kubectl create -f tomcat-nginx.yaml deployment.apps/nginx-deployment created deployment.apps/tomcat-deployment created service/nginx-service created service/tomcat-service created [root@master ~]# kubectl get svc -n dev NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-service ClusterIP None <none> 80/TCP 20s tomcat-service ClusterIP None <none> 8080/TCP 20s
Http代理
创建ingress-http.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-http namespace: dev spec: rules: - host: nginx.test.com http: paths: - path: / backend: serviceName: nginx-service servicePort: 80 - host: tomcat.test.com http: paths: - path: / backend: serviceName: tomcat-service servicePort: 8080
使用配置文件
[root@master ~]# vim ingress-http.yaml [root@master ~]# kubectl create -f ingress-http.yaml ingress.extensions/ingress-http created [root@master ~]# kubectl get ing ingress-http -n dev NAME HOSTS ADDRESS PORTS AGE ingress-http nginx.test.com,tomcat.test.com 80 13s [root@master ~]# kubectl describe ing ingress-http -n dev Name: ingress-http Namespace: dev Address: 10.106.160.9 Default backend: default-http-backend:80 (<none>) Rules: Host Path Backends ---- ---- -------- nginx.test.com / nginx-service:80 (10.244.1.33:80,10.244.2.13:80,10.244.2.16:80) tomcat.test.com / tomcat-service:8080 (10.244.2.14:8080,10.244.2.17:8080,10.244.2.18:8080) Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{},"name":"ingress-http","namespace":"dev"},"spec":{"rules":[{"host":"nginx.test.com","http":{"paths":[{"backend":{"serviceName":"nginx-service","servicePort":80},"path":"/"}]}},{"host":"tomcat.test.com","http":{"paths":[{"backend":{"serviceName":"tomcat-service","servicePort":8080},"path":"/"}]}}]}} Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal CREATE 18m nginx-ingress-controller Ingress dev/ingress-http Normal UPDATE 38s (x2 over 18m) nginx-ingress-controller Ingress dev/ingress-http
修改本机的hosts文件(位置:C:\Windows\System32\drivers\etc),添加如下内容
master虚拟机的IP地址 nginx.test.com
masete虚拟机的IP地址 tomcat.test.com
查看ingress为service提供的端口号
[root@master ~]# kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx NodePort 10.106.160.9 <none> 80:32000/TCP,443:32421/TCP 95m
在浏览器中测试访问,发现能够访问通
nginx:
tomcat:
Https代理
创建证书
[root@master ~]# openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/C=CN/ST=BJ/L=BJ/O=nginx/CN=test.com" Generating a 2048 bit RSA private key ............................+++ ..............................+++ writing new private key to 'tls.key' -----
创建密钥
[root@master ~]# kubectl create secret tls tls-secret --key tls.key --cert tls.crt
secret/tls-secret created
创建ingress-https.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-https namespace: dev spec: tls: - hosts: - nginx.test.com - tomcat.test.com secretName: tls-secret #指定密钥 rules: - host: nginx.test.com http: paths: - path: / backend: serviceName: nginx-service servicePort: 80 - host: tomcat.test.com http: paths: - path: / backend: serviceName: tomcat-service servicePort: 8080
使用配置文件
[root@master ~]# vim ingress-https.yaml [root@master ~]# kubectl create -f ingress-https.yaml ingress.extensions/ingress-https created [root@master ~]# kubectl get ing ingress-https -n dev NAME HOSTS ADDRESS PORTS AGE ingress-https nginx.test.com,tomcat.test.com 10.106.160.9 80, 443 52s [root@master ~]# kubectl describe ing ingress-https -n dev Name: ingress-https Namespace: dev Address: 10.106.160.9 Default backend: default-http-backend:80 (<none>) TLS: tls-secret terminates nginx.test.com,tomcat.test.com Rules: Host Path Backends ---- ---- -------- nginx.test.com / nginx-service:80 (10.244.1.33:80,10.244.2.13:80,10.244.2.16:80) tomcat.test.com / tomcat-service:8080 (10.244.2.14:8080,10.244.2.17:8080,10.244.2.18:8080) Annotations: Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal CREATE 79s nginx-ingress-controller Ingress dev/ingress-https Normal UPDATE 38s nginx-ingress-controller Ingress dev/ingress-https
获取端口,左边的是http使用的端口,右边是https使用的端口,因此要使用的端口是32421
[root@master ~]# kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx NodePort 10.106.160.9 <none> 80:32000/TCP,443:32421/TCP 129m
使用浏览器访问
nginx:
tomcat:
出处:https://www.cnblogs.com/Ayanamidesu/p/15136379.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式