部署deployment和service
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
namespace: dalong
spec:
replicas: 2
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
spec:
containers:
- image: nginx:1.18
imagePullPolicy: Always
name: web
resources: {}
---
apiVersion: v1
kind: Service
metadata:
name: web
namespace: dalong
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web
type: NodePort
ingress.yaml如下,画红线部分一定要加
在另一台机器上配置host
获取ingress的入口,查看ingress-nginx-controller部署在哪台机器上,pod在work01上,ip为192.168.37.131
[root@k8s-worker01 tmpdir]# kubectl get pod -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-admission-create-lkbzx 0/1 Completed 0 12h 10.244.235.145 worker1 <none> <none>
ingress-nginx-admission-patch-5tlgc 0/1 Completed 0 12h 10.244.235.161 worker1 <none> <none>
ingress-nginx-controller-7fcc78fc94-xddz9 1/1 Running 3 12h 10.244.235.187 worker1 <none> <none>
//下面这行配置在hosts文件中 192.168.37.131即worker1 的ip
//通过 kubectl get pod -n ingress-nginx -o wide,查看pod所在的主机ip即worker1
192.168.37.131 app.dalong.com
1)通过nodeport访问 http://app.dalong.com:31970
31970端口号是ingress-nginx命名空间里的nodeport类型的service的端口号;因为安装的ingress的是没有与宿主机共享网络,就需要通过端口来访问ingress里的pod
2)如果要通过域名直接访问,不加端口号,则需要将安装ingres_deploy.yaml(非创建ingress实例的yaml)里添加hostNetwork:True,如下