使用wget下载v1.0.0的deploy.yaml,该处可能由于网络的原因下载不下来,大家可以在第二步下载我改好的文件;
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.0/deploy/static/provider/baremetal/deploy.yaml接下来我们需要修改deploy.yaml里面的镜像文件,由于k8s.gcr.io相关的镜像国内都进行屏蔽了,这里我采用了从docker官方去下载相关的镜像文件,需要将k8s.gcr.io/ingress-nginx/controller:v1.0.0和k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.0镜像替换为willdockerhub/ingress-nginx-controller:v1.0.0和jettech/kube-webhook-certgen:v1.5.2,这里需要注意的是kube-webhook-certgen有两处不要漏替换,此外需要在args处增加--watch-ingress-without-class=true配置,这里将修改好的文件v1.0.0-deploy.yaml上传到网盘,链接 提取码: sksc ;
image.pngimage.png部署nginx-ingress-controller;
kubectl apply -f v1.0.0-deploy.yaml检查nginx-ingress-controller创建的情况,这个我们会发现该Service是一个NodePort类型,并且被随机分配两个端口,分别是32368和32577,后续我们就需要通过这个端口访问改地址信息;
#查看相关pod状态kubectl get pods -n ingress-nginx -owide
#查看service
kubectl get service -n ingress-nginx
image.png
创建Deployment和Service,这里我们就是就使用之前的nginx-deployment.yaml和nginx-service.yaml;
apiVersion: apps/v1kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: backend
replicas: 3
template:
metadata:
labels:
app: backend
spec:
containers:
- name: nginx
image: nginx:latest
resources:
limits:
memory: "128Mi"
cpu: "128m"
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
#定义后端pod标签为app=backend
selector:
app: backend
ports:
#service端口号
- port: 80
#pod的端口号
targetPort: 80
kubectl apply -f nginx-service.yaml
kubectl apply -f nginx-nodeport-service.yaml
验证Service创建情况,并且可以通过Service地址访问相关服务;
#查看service状况kubectl get svc
#访问服务
curl
新建ingress策略nodeport-ingress.yaml,创建对应资源;
apiVersion: networking.k8s.io/v1kind: Ingress
metadata:
name: nodeport-ingress
namespace: default
spec:
rules:
- host: aa.bb.cc
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: nginx-service
port:
number: 80
#创建ingress资源
kubectl apply -f nodeport-ingress.yaml
#检查ingress资源
kubectl get ingress
image.png