Ingress配置(极简)

ingress组件可以理解为k8s的访问入口, 用来转发请求. 这点要与ingress-controller区分开来, ingress只有与ingress-controller配合使用才能转发请求. ingress相当于转发请求的配置文件, 而ingress-controller则相当于nginx进程, 实际转发请求的程序.

ingress -> nginx conf
ingress-controller -> nginx process

官方文档给出的示例图

目前Ingress-controller主要使用nginxtraefik, 这篇文章主要介绍ingress-nginx配置

安装ingress-nginx相关组件

下载部署文件并安装

curl -L -o ingress-nginx.yaml https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml

kubectl apply -f ingress-nginx.yaml

目前新版本的controller也包括webhook组件, 这里不需要那么多功能, 还是以0.30.0为准

查看pod是否部署成功

kubectl get po -n ingress-nginx

部署ingress入口的访问方式

vim ingress-service.yaml
#########################################
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
#########################################    

kubectl apply -f ingress-service.yaml

这里通过NodePort方式暴露服务入口, 主要用于测试. 如果用于大规模集群, 可以专门指定3台主机通过hostPort部署, 减少网络转发造成的损耗.

查看服务部署情况

kubectl get svc -n ingress-nginx

部署用于测试的两个service服务

部署服务一

vim apple.yaml
#########################################
kind: Pod
apiVersion: v1
metadata:
  name: apple-app
  labels:
    app: apple
spec:
  containers:
    - name: apple-app
      image: hashicorp/http-echo
      args:
        - "-text=apple"

---

kind: Service
apiVersion: v1
metadata:
  name: apple-service
spec:
  selector:
    app: apple
  ports:
    - port: 5678
#########################################


kubectl apply -f apple.yaml

部署服务二

vim banana.yaml
#########################################
kind: Pod
apiVersion: v1
metadata:
  name: banana-app
  labels:
    app: banana
spec:
  containers:
    - name: banana-app
      image: hashicorp/http-echo
      args:
        - "-text=banana"

---

kind: Service
apiVersion: v1
metadata:
  name: banana-service
spec:
  selector:
    app: banana
  ports:
    - port: 5678
#########################################


kubectl apply -f banana.yaml

查看部署情况

部署ingress(配置转发规则)

vim ingress.yaml
#########################################
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: test.ingress.com
    http:
      paths:
        - path: /apple
          backend:
            serviceName: apple-service
            servicePort: 5678
        - path: /banana
          backend:
            serviceName: banana-service
            servicePort: 5678
#########################################


kubectl apply -f ingress.yaml

验证

curl -H "Host: test.ingress.com" http://192.168.104.62:30591/apple
curl -H "Host: test.ingress.com" http://192.168.104.62:30591/banana

192.168.104.62:30591这个是ingress-controller访问入口, 一般会在集群部署nginx转发这个地址

展示评论