ingress是一个管理kubernetes集群南北流量(集群外部到内部)的api对象,典型的有HTTP。并且可以提供:LB负载均衡的能力。
Ingress Controller是一种能读懂ingress配置,并将其翻译成自己配置文件的应用程序。
一、Ingress nginx部署:
网上有的链接无法下载,找到目前可以下载
wget https://github.com/kubernetes/ingress-nginx/blob/nginx-0.26.1/deploy/static/mandatory.yaml
里面的image你懂的无法下载,改为阿里云的image:registry.aliyuncs.com/google_containers/nginx-ingress-controller:0.26.1
启动配置清单kubectl apply -f mandatory.yaml
namespace/ingress-nginx created
configmap/nginx-configuration created
。。。。。
查看pod是否运行
kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-7fb85bc8bb-nxcq6 0/1 ContainerCreating 0
二、创建nginx-ingress-service.yaml文件
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress-controller
namespace: ingress-nginx
spec:
type: NodePort
ports:
- port: 80
name: http
nodePort: 30080
- port: 443
name: https
nodePort: 30443
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
此时基于nodeport型的nginx- ingress-controller的service创建成功
kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-ingress-controller NodePort 10.98.170.206 <none> 80:30080/TCP
三、定义后端分组service:myapp-svc
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
namespace: default
spec:
selector:
app: myapp
release: beta
clusterIP: "None"
ports:
- port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: beta
template:
metadata:
labels:
app: myapp
release: canary
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
kubectl apply -f myapp-svc-headless.yaml
kubectl get svc 查询创建信息
四、通过Ingress把myapp-svc发布出去
cat ingress-myapp.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-myapp
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: my.jiang.com
http:
paths:
- path:
backend:
serviceName: myapp-svc
servicePort: 80
namespace要和deployment和要发布的service处于同一个名称空间
host:表示访问这个域名,就会转发到后端myapp-deploy管理的pod上
kubectl apply -f ingress-myapp.yaml
kubectl get ingress查询信息
在主机上创建hosts记录
my.jiang.com 192.168.X.X
用curl my.jiang.com:30080可以看到网站信息