ingress 部署

(1) 部署ingress-controller

 kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

由于网络问题,pull镜像可能会失败,可预先将镜像pull下来。
(2) 部署ingress service(nodeport类型)

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml

(3) 创建deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}
  template:
    metadata:
      labels:
        app: app-demo
        tier: frontend
    spec:
      containers:
      - name: tomcat-damo
        image: tomcat
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080

(4) 为pod创建service

apiVersion: v1
kind: service
metadata:
  name: tomcat
spec:
  ports:
  - port: 8080 # service 所需的端口号
    targetPort: 8080 # 后端pod的端口号
  selector:
    tier: frontend # 筛选具有tier=frontend label的pod

(5) 创建ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: tomcat.example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat
          servicePort: 8080

(6) 配置域名
master节点IP:192.168.1.150

# C:\Windows\System32\drivers\etc\hosts
192.168.1.150  tomcat.example.com

查看ingress-nginx service的端口映射:

# kubectl get svc -n ingress-nginx 
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.109.237.14   <none>        80:30363/TCP,443:31375/TCP   18h

通过 http://tomcat.example.com:30363 访问tomcat服务:
tomcat

Logo

开源、云原生的融合云平台

更多推荐