k8s(1.28.2)部署ingress-nginx-controller(1.9.0)
部署ingress-nginx-controller-1.9,并进行ingress规则测试。
·
1.部署ingress-nginx-controller
继在三台虚拟机部署k8s后,需要部署ingress-nginx-controller,才能使设置的ingress规则生效。
1.1下载yaml
# 下载ingress-nginx-controller的deploy.yaml
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.9.0/deploy/static/provider/baremetal/deploy.yaml
# 如果deploy.yaml中的镜像拉取不下来,查找可用的ingress-nginx-controller和kube-webhook-certgen镜像
docker search ingress-nginx-controller
docker search kube-webhook-certgen
# ---------------------手动拉取镜像需要在每个节点都要执行----------------------
# 选择可用的镜像拉取,版本号为 v1.9.0 和 v20230407,都是较新的版本
docker pull xxx/ingress-nginx-controller:v1.9.0
docker pull xxx/kube-webhook-certgen:v20230407
# docker tag 镜像
docker tag xxx/kube-webhook-certgen:v20230407 k8s/ingress-nginx/kube-webhook-certgen:v20230407
docker tag xxx/ingress-nginx-controller:v1.9.0 k8s/ingress-nginx-controller:v1.9.0
# 如果有搭建的docker registry服务器,可将拉取的镜像 push 至服务器,就不需要手动在每个节点拉取镜像了。
1.2修改yaml
1.2.1三处image的修改
(上面步骤docker tag 的镜像名叫什么,对应的image就改为什么)
ingress-nginx-controller有一处
kube-webhook-certgen有两处
1.2.2在相应位置添加
添加 hostNetwork: true。
若不添加,后续使用 域名:nodeport 访问;添加之后,直接使用域名访问。
执行
#修改完之后执行
kubectl apply -f deploy.yaml
kubectl get all -n ingress-nginx
显示如下,则成功
2.测试
2.1执行测试的yaml
两个yaml文件如下
# nginx-tomcat-test.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: test
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
namespace: test
spec:
replicas: 3
selector:
matchLabels:
app: tomcat-pod
template:
metadata:
labels:
app: tomcat-pod
spec:
containers:
- name: tomcat
image: tomcat:8.0-alpine
ports:
- containerPort: 8080
# nginx-tomcat-svc-ingress.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: test
spec:
selector:
app: nginx-pod
clusterIP: None
type: ClusterIP
ports:
- port: 80
targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-service
namespace: test
spec:
selector:
app: tomcat-pod
clusterIP: None
type: ClusterIP
ports:
- port: 8080
targetPort: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
namespace: test
spec:
ingressClassName: nginx
rules:
- host: "tomcat.demo.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: tomcat-service
port:
number: 8080
- host: "nginx.demo.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx-service
port:
number: 80
# 执行
kubectl apply -f nginx-tomcat-deploy.yaml
kubectl apply -f nginx-tomcat-svc-ingress.yaml
# 查看
kubectl get all -n test
kubectl get ingress -n test
显示:
2.2浏览器访问
设置windows下的hosts文件,添加
192.168.255.143 tomcat.demo.com nginx.demo.com
浏览器访问,显示如下页面,就算大功告成 !
清理资源
kubectl delete -f nginx-tomcat-svc-ingress.yaml
kubectl delete -f nginx-tomcat-deploy.yaml
上面给出的是ingress的http代理,https代理待后续给出。。。
更多推荐
已为社区贡献4条内容
所有评论(0)