k8s版本1.17.X 左右
wget https://kuboard.cn/install-script/v1.18.x/nginx-ingress.yaml
kukectl apply -f nginx-ingress.yaml
这个部署一定会自动部署在节点上,试过指点部署在master上没反应,还是会部署在其它节点上
nginx 的pod最好部署和ingress-controller在同一个节点上,不然域名解析好像不会生效,我这里是单点,多点当我没说
也可能是我dns没理解好
不在同一节点报错可能:curl: (7) Failed to connect to www.123.cn port 80: 拒绝连接
生产环境也可参考https://github.com/nginxinc/kubernetes-ingress/blob/v1.5.3/docs/installation.md
还有一种ingress-controller要翻墙的安装或者外网买个服务搭建个docker私人仓库再换个镜像,暂未在国内找到新镜像https://blog.csdn.net/hualinux/article/details/107642281
创建文件 nginx-deployment.yaml 最好都指定节点安装和ingress-controller在同一个节点上: nodeName: 123
vim nginx-deployment.yaml
已复制到剪贴板!
文件内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.0
创建文件 nginx-service.yaml
vim nginx-service.yaml
文件内容如下
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- name: nginx-port
protocol: TCP
port: 80
nodePort: 32600
targetPort: 80
type: NodePort
已复制到剪贴板!
创建文件 nginx-ingress.yaml
vim nginx-ingress.yaml
已复制到剪贴板!
1
文件内容如下
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-ingress-for-nginx # Ingress 的名字,仅用于标识
spec:
rules: # Ingress 中定义 L7 路由规则
- host: www.123.cn # 根据 virtual hostname 进行路由(请使用您自己的域名)
http:
paths: # 按路径进行路由
- path: /
backend:
serviceName: nginx-service # 指定后端的 Service 为之前创建的 nginx-service
servicePort: 80
执行命令
kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml
kubectl apply -f nginx-ingress.yaml
检查执行结果
kubectl get ingress -o wide
可查看到名称为 my-ingress-for-nginx 的 Ingress。
从互联网访问
# 请使用您自己的域名
curl www.123.cn