ingress暴露服务部署

网友投稿 291 2022-10-23

ingress暴露服务部署

部署deployment pod应用

ingress 通过自有的控制器ingress-controller来动态加载nginx配置,使service可以将deployment下发的pod应用以~]# vim nginx-deploy.yaml apiVersion: apps/v1kind: Deploymentmetadata: name: front-end labels: app: nginxspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80

[root@k8s-master ~]# kubectl apply -f nginx-deploy.yaml[root@k8s-master ~]# kubectl get deployNAME READY UP-TO-DATE AVAILABLE AGEfront-end 3/3 3 3 177m[root@k8s-master ~]# kubectl get pod -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESfront-end-6b7b5c4bfc-48b8x 1/1 Running 0 177m 10.224.169.135 k8s-node2 front-end-6b7b5c4bfc-cj5xd 1/1 Running 0 177m 10.224.169.136 k8s-node2 front-end-6b7b5c4bfc-ns599 1/1 Running 0 177m 10.224.36.79 k8s-node1 [root@k8s-master ~]#

[root@k8s-master ~]# kubectl apply -f nginx-deploy.yaml[root@k8s-master ~]# kubectl get deployNAME READY UP-TO-DATE AVAILABLE AGEfront-end 3/3 3 3 177m[root@k8s-master ~]# kubectl get pod -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESfront-end-6b7b5c4bfc-48b8x 1/1 Running 0 177m 10.224.169.135 k8s-node2 front-end-6b7b5c4bfc-cj5xd 1/1 Running 0 177m 10.224.169.136 k8s-node2 front-end-6b7b5c4bfc-ns599 1/1 Running 0 177m 10.224.36.79 k8s-node1 [root@k8s-master ~]#

访问测试:[root@k8s-master ~]# curl -Lk 10.224.169.135Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer to

cat front-end-svc.yamlapiVersion: v1kind: Servicemetadata: name: nginxspec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80[root@k8s-master ks]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEfront-end-svc ClusterIP 10.103.129.150 80/TCP 3h6mkubernetes ClusterIP 10.96.0.1 443/TCP 279dnginx NodePort 10.99.254.97 80:30650/TCP 275d[root@k8s-master ks]#

cat front-end-svc.yamlapiVersion: v1kind: Servicemetadata: name: nginxspec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80[root@k8s-master ks]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEfront-end-svc ClusterIP 10.103.129.150 80/TCP 3h6mkubernetes ClusterIP 10.96.0.1 443/TCP 279dnginx NodePort 10.99.254.97 80:30650/TCP 275d[root@k8s-master ks]#测试访问 svc到pod是否正常:[root@k8s-master ks]# curl 10.103.129.150Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer to

ingres-controller控制器文件下载下载地址:~]# kubectl apply -f ingress-controller.yaml

修改两处:

1.添加hostNetwork: true 意思是让控制器的namespace使用和宿主机一样的网络,既让访问宿主机80和443直接就是映射到容器里的这两个端口。

ingess控制器生成的配置

2.修改镜像为dockerhub上面的景象地址,然后apply创建一下。

[root@k8s-master ~]# kubectl apply -f ingress-controller.yaml

下面开始创建ingress规则

[root@k8s-master ks]# cat ingress.yaml apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: minimal-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: /spec: rules: - host: example.long.com #宿主机设置本地hosts解析后可以访问本地址 paths: - path: /hello pathType: Prefix backend: service: name: nginx port: number: 80 [root@k8s-master ks]# kubectl apply -f ingress.yaml

控制器生成的配置

[root@k8s-master ks]# kubectl exec -it nginx-ingress-controller-4mpfv  -n ingress-nginx -- sh

以上配置说明我们的规则动态生成到配置中了。

[root@k8s-master ks]# kubectl get ingressWarning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 IngressNAME CLASS HOSTS ADDRESS PORTS AGEminimal-ingress example.long.com 80 6h59m[root@k8s-master ks]# kubectl describe ingress minimal-ingressWarning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 IngressName: minimal-ingressNamespace: defaultAddress: Default backend: default-()Rules: Host Path Backends ---- ---- -------- example.long.com #域名 /hello nginx:80 10.224.169.135:80,10.224.169.136:80,10.224.36.79:80)#这里是后端的应用podsAnnotations: nginx.ingress.kubernetes.io/rewrite-target: /Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal CREATE 19m nginx-ingress-controller Ingress default/minimal-ingress Normal CREATE 19m nginx-ingress-controller Ingress default/minimal-ingress[root@k8s-master ks]#

以下在宿主机客户端用浏览器进行测试,完成。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java集合Collection实现类解析ArrayList LinkedList及Vector
下一篇:移动网络接口介绍
相关文章

 发表评论

暂时没有评论,来抢沙发吧~