1 配置前提
集群master带有eip | 必须满足 |
---|---|
集群安装nginx组件 | 必须满足 |
2 确认信息
2.1 ingress-nginx组件检查pod
kubectl get pod -A | grep ingress-nginx
2.2 ingress-nginx组件检查deployment
kubectl get deployment -A | grep ingress-nginx
2.3 ingress-nginx组件检查service
kubectl get svc -A | grep ingress-nginx
2.4 ingress-nginx组件检查serviceaccount
kubectl get serviceaccount -A | grep ingress-nginx
2.5 ingress-nginx组件检查clusterrole
kubectl get clusterrole -A | grep ingress-nginx
3 demo用例
3.1 创建nginx-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: default
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
kubectl apply -f nginx-demo.yaml
3.2 创建对外ingress(nginx-ingress-demo.yaml)
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
ingress.kubernetes.io/proxy-body-size: "0"
ingress.kubernetes.io/ssl-redirect: "true"
kubernetes.io/ingress.class: nginx-ingress-controller
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
generation: 1
name: nginx-demo
namespace: default
spec:
rules:
- host: demo.adp.aliyunpoc.com
http:
paths:
- backend:
serviceName: nginx
servicePort: 80
path: /
status:
loadBalancer:
ingress:
- {}
kubectl apply -f nginx-ingress-demo.yaml
3.3 检查校验
kubectl get ingress -A
说明: 配置hosts映射关系进行访问,配置需要访问所在主机的hosts文件
网络可达的EIP test.myk8s.aliyunpoc.com
4 自定义ingress
4.1 确认发布服务
kubectl get svc -A | grep gra
说明: 查看备注命名空间,服务名和端口信息
4.2 配置ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
ingress.kubernetes.io/proxy-body-size: "0"
ingress.kubernetes.io/ssl-redirect: "true"
kubernetes.io/ingress.class: nginx-ingress-controller
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
generation: 1
name: ingress的名称(标记作用)
namespace: 服务所在的命名空间
spec:
rules:
- host: demo.adp.aliyunpoc.com(自定义域名)
http:
paths:
- backend:
serviceName: 需要暴露的服务名
servicePort: 服务的内部端口
path: /
status:
loadBalancer:
ingress:
- {}
kubectl apply -f ingress的文件名
备注: 通过浏览器访问时同理,配置访问主机的hosts文件浏览器打开域名即可访问到服务页面。