Ingress 实战
创建 Deployment
deploy-web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web创建
kubectl create -f deploy-web.yaml修改 html 以区分(可选)
kubectl exec -it web-xxxx -- bash
echo web-0 > /usr/share/nginx/html/index.html
kubectl exec -it web-xxxx -- bash
echo web-1 > /usr/share/nginx/html/index.html创建 Service
svc-ng.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-web
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 8888
targetPort: 80创建
kubectl create -f svc-web.yaml创建 Ingress
ingress-web.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-web
spec:
ingressClassName: nginx
rules:
- host: web.yunwei361.com
http:
paths:
- backend:
service:
name: svc-web
port:
number: 8888
path: /
pathType: Exact命令行创建(可选)
kubectl create ingress ingress-web --rule="web.yunwei361.com/=svc-web:8888" --class=nginx --dry-run=client -o yaml > ingress-web.yaml访问
方式一:临时映射
直接将 nginx-ingress controller 对应的pod端口映射到本机
kubectl -n nginx-ingress get pod
NAME READY STATUS RESTARTS AGE
nginx-ingress-7cf5476c47-c7mw6 1/1 Running 0 166mkubectl port-forward -n nginx-ingress nginx-ingress-7cf5476c47-c7mw6 8080:80 &curl --resolve web.yunwei361.com:8080:127.0.0.1 http://web.yunwei361.com:8080方式二:NodePort
svc-nodeport-ingress.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress
namespace: nginx-ingress
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
- port: 443
targetPort: 443
protocol: TCP
name: https
selector:
app: nginx-ingresskubectl create -f svc-nodeport-ingress.yaml修改本地hosts
192.168.1.29 web.yunwei361.com访问
curl web.yunwei361.com:<NodePort对应的端口>Last updated