K8SNginx-Ingress-Controller部署,用域名的方式访问服务

Posted 键客李大白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K8SNginx-Ingress-Controller部署,用域名的方式访问服务相关的知识,希望对你有一定的参考价值。



一、创建资源


完整的yaml清单文件在后面。

[root@lidabai-master app]# kubectl apply -f nginx-ingress-controller.yaml
serviceaccount/nginx-ingress-serviceaccount created

【K8S】Nginx-Ingress-Controller部署,用域名的方式访问服务_kubernetes

二、查看服务状态


查看服务状态是否正常,刚才创建的Pod是放在kube-system名称空间的。

[root@lidabai-master app]# kubectl -n kube-system  get pod

【K8S】Nginx-Ingress-Controller部署,用域名的方式访问服务_kubernetes_02

服务状态正常!


三、创建测试的服务


$ kubectl apply  -f pod-test.yaml
$ vim pod-test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginxx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
protocol: TCP

查看服务

[root@lidabai-master app]# kubectl get svc
nginx-svc ClusterIP 10.108.224.202 <none> 80/TCP 26h

【K8S】Nginx-Ingress-Controller部署,用域名的方式访问服务_云原生_03


四、创建Ingress

[root@lidabai-master app]# kubectl apply -f ingress-test.yaml
[root@lidabai-master app]# cat ingress-test.yaml
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-ngi
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: lidabai.nginx.com #访
http:
paths:
- path: /
pathType: Prefix # Prefix
backend:
service:
name: nginx-svc #service
port:
number: 80

查看ingress

[root@lidabai-master app]# kubectl get ingress
NAME

(c)2006-2024 SYSTEM All Rights Reserved IT常识