kubernetes之ingress部署

创建nginx服务
[root@k8s-master k8s-yaml]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: registry.cn-shanghai.aliyuncs.com/jovi/nginx:alpine
ports:
- containerPort: 80

执行命令
kubectl apply -f nginx-deployment.yaml

创建service本地复制均衡
[root@k8s-master k8s-yaml]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ClusterIP
clusterIP: 10.0.171.239 #手动分配的集群IP
type: NodePort #将复制均衡IP自动暴露到本地均衡跟下面的nodePort 端口对应
selector:
app: nginx #对应之前的 nginx-deployment.yaml 应用
ports:
- protocol: TCP
port: 8084 # port 80 是集群分配的Ip
targetPort: 80 # targetPort 是后端节点IP
nodePort: 30030 #映射的是物理机的Ip
执行命令
kubectl apply -f nginx-service.yaml


在本地做host解析
windows 或linux hosts
192.168.66.5 my-service
访问: http://my-service:30030/


创建ingress
[root@k8s-master k8s-yaml]# cat nginx-ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-ingress-for-nginx # Ingress 的名字,仅用于标识
spec:
rules: # Ingress 中定义 L7 路由规则
- host: a.demo.kuboard.cn # 根据 virtual hostname 进行路由(请使用您自己的域名)
http:
paths: # 按路径进行路由
- path: /
backend:
serviceName: my-service # 指定后端的 Service 为之前创建的 nginx-service
servicePort: 80

执行命令
kubectl apply -f nginx-ingress.yaml
查看生成的应用


在本地做host解析
windows 或linux hosts
192.168.66.5 k8s-master foo.bar.com my-service a.demo.kuboard.cn

访问
curl http://a.demo.kuboard.cn:30030

总结:
1、创建一个deployment资源用于运行nginx,pod副本为3个;
2、为后端的nginx pod创建service服务,名称为myapp-ingress-svc
3、创建ingress规则,通过ingress把后端的pod暴露出去
注意:这里对于为nginx创建的svc服务,并没有起到调度作用,只是收集pod资源,用于分类。因为实际调度过程中,流量是直接通过ingress规则调度到后端的pod,而没有经过svc服务,svc只是提供一个收集pod的作用。

报错1:


为什么会找不到:官网给出的解释: Note: Depending on the Ingress controller you are using, you may need to create a default-http-backend Service.
Default Backend
An Ingress with no rules sends all traffic to a single default backend. The default backend is typically a configuration option of the Ingress controller and is not specified in your Ingress resources.
If none of the hosts or paths match the HTTP request in the Ingress objects, the traffic is routed to your default backend.

翻译:
控制器。注意:根据您正在使用的Ingress控制器,您可能需要创建一个default-http-backend服务。
违约后端
没有规则的入口将所有流量发送到一个默认后端。默认后端通常是Ingress控制器的一个配置选项,在您的Ingress资源中没有指定。
如果Ingress对象中的主机或路径都不匹配HTTP请求,则流量将被路由到默认后端。

原文地址:https://www.cnblogs.com/zoulixiang/p/13554933.html