K8S安装(四): 在K8S集群中部署应用示例 ,通过Ingress对外暴露应用服务
目标:搭建一个tomcat服务器并对外通过Ingress暴露
步骤1: 编写tomcat的Deployment和Service
--- apiVersion: apps/v1 kind: Deployment metadata: name: app-tomcat labels: app: app-tomcat spec: selector: matchLabels: app: app-tomcat template: metadata: labels: app: app-tomcat spec: containers: - name: tomcat-container1 image: tomcat imagePullPolicy: IfNotPresent ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: tomcat-service spec: type: ClusterIP selector: app: app-tomcat ports: - protocol: TCP port: 80 targetPort: 8080
步骤二:编写Ingress 规则(特别注意:请在annotations中指定你的ingress是何种,此处我用的nginx-ingress所以是Nginx,否则无法通过配置的域名test.123.com访问)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-web-test
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
defaultBackend:
service:
name: tomcat-service
port:
number: 80
rules:
- host: test.123.com
http:
paths:
- path: "/"
pathType: Prefix
backend:
service:
name: tomcat-service
port:
number: 80
步骤三:请在你的宿主机或者外部机器中修改hosts文件,添加解析记录,IP为Ingress所在节点的主机IP.
192.168.50.52 test.123.com
输入网址,即可访问!