「走进k8s」Kubernetes1.15.1的Ingress TLS 与 Traefik路径转发(36)

上次说了 traefik 的安装使用以及简单的 ingress 的配置方法,这次一起了解 ingress tls 以及 path 路径在 ingress 对象中的使用方法。

(一)traefik的TLS 认证

搞过网站的老铁应该都知道,有时候一些环境必须使用https,需要https就需要增加一个ssl的证书,正常的都是通过腾讯云或者阿里云上去申请,这里是学习环境我们就自己生成一个证书。

  • ① 证书生成
openssl req -newkey rsa:2048 -nodes -keyout tls.key -x509 -days 365 -out tls.crt

kubectl 创建一个 secret 对象来存储上面的证书

kubectl create secret generic traefik-cert --from-file=tls.crt --from-file=tls.key -n kube-system

key 和 crt 迁移到指定的目录下

mkdir /ssl

mv tls.key /ssl/

mv tls.crt /ssl/  

cd /ssl

  • ② 配置 Traefik

之前使用 Traefik 的默认配置,现在我们来配置 Traefik,让其支持 https

cd ~

vi traefik.toml

添加配置信息

defaultEntryPoints = ["http", "https"]

[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
      entryPoint = "https"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]
      [[entryPoints.https.tls.certificates]]
      CertFile = "/ssl/tls.crt"
      KeyFile = "/ssl/tls.key"

配置了 http 和 https 两个入口,并且配置了将 http 服务强制跳转到 https 服务,通过 traefik 进来的服务都是 https 的,要访问 https 服务,当然就得配置对应的证书了,指定了 CertFile 和 KeyFile 两个文件,可以将上面的 traefik.toml 配置文件通过一个 ConfigMap 对象挂载到 traefik pod 中去

kubectl create configmap traefik-conf --from-file=traefik.toml -n kube-system

修改traefik.yaml 文件

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
  labels:
    k8s-app: traefik-ingress-lb
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: traefik-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: traefik-ingress-lb
        name: traefik-ingress-lb
    spec:
      serviceAccountName: traefik-ingress-controller
      terminationGracePeriodSeconds: 60
      volumes:
      - name: ssl
        secret:
          secretName: traefik-cert
      - name: config
        configMap:
          name: traefik-conf
      tolerations:
      - operator: "Exists"
      containers:
      - image: traefik
        name: traefik-ingress-lb
        volumeMounts:
        - mountPath: "/ssl"
          name: ssl
        - mountPath: "/config"
          name: config
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: https
          containerPort: 443
          hostPort: 443
        - name: admin
          containerPort: 8080
        args:
        - --configfile=/config/traefik.toml
        - --api
        - --kubernetes
        - --logLevel=INFO
---
kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
  namespace: kube-system
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - protocol: TCP
      port: 80
      name: web
    - protocol: TCP
      port: 8080
      name: admin
  type: NodePort

运行查看效果

kubectl apply -f traefik.yaml

kubectl get pod -n kube-system

注意网上很多的文章都是直接光修改了traefik.yaml 这一块,其实我将对应的service也配置上去了,这样才生效。

https://traefik.idig8.com 本地windows配置host文件,上次说过的。

(二)通过traefik进行转向,通过路径不同

  • ① 创建3个Deployment 和对应3个service

这个image 是参考网上一个大神已经打包好的,运行起来可以直接在页面打印 server Name和主机名称

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: svc1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: svc1
    spec:
      containers:
      - name: svc1
        image: cnych/example-web-service
        env:
        - name: APP_SVC
          value: svc1
        ports:
        - containerPort: 8080
          protocol: TCP

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: svc2
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: svc2
    spec:
      containers:
      - name: svc2
        image: cnych/example-web-service
        env:
        - name: APP_SVC
          value: svc2
        ports:
        - containerPort: 8080
          protocol: TCP

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: svc3
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: svc3
    spec:
      containers:
      - name: svc3
        image: cnych/example-web-service
        env:
        - name: APP_SVC
          value: svc3
        ports:
        - containerPort: 8080
          protocol: TCP

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: svc1
  name: svc1
spec:
  type: ClusterIP
  ports:
  - port: 8080
    name: http
  selector:
    app: svc1

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: svc2
  name: svc2
spec:
  type: ClusterIP
  ports:
  - port: 8080
    name: http
  selector:
    app: svc2

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: svc3
  name: svc3
spec:
  type: ClusterIP
  ports:
  - port: 8080
    name: http
  selector:
    app: svc3

创建脚本

kubectl create -f backend.yaml

  • ② 创建ingress 对象来访问上面的3个服务
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-web-app
  annotations:
    kubernetes.io/ingress.class: "traefik"
spec:
  rules:
  - host: example.idig8.com
    http:
      paths:
      - path: /s1
        backend:
          serviceName: svc1
          servicePort: 8080
      - path: /s2
        backend:
          serviceName: svc2
          servicePort: 8080
      - path: /
        backend:
          serviceName: svc3
          servicePort: 8080

这里有个细节一定要注意:如果有多个path的话,根一定要放到最后(path: /),也就是上面所有都不满足的话,才执行最下面这个,如果你把根路径放到最上面的话,下面的s1和s2都没有效果了。

执行脚本

 kubectl create -f example-ingress.yaml

kubectl describe ingress

直接没有在系统的命名空间下

kubectl get ingress

  • ③ 访问查看转向

本地 hosts 里面给域名 example.idig8.com 添加对应的 hosts 解析

example.idig8.com/s1

example.idig8.com/s2

example.idig8.com/s3

traefik.idig8.com 查看效果

  • ④ 如果每个ingress 需要单独的证书的话

tls:
– secretName: traefik-cert 单独配置吧

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-web-app
  annotations:
    kubernetes.io/ingress.class: "traefik"
spec:
  tls:
    - secretName: traefik-cert
  rules:
  - host: example.idig8.com
    http:
      paths:
      - path: /s1
        backend:
          serviceName: svc1
          servicePort: 8080
      - path: /s2
        backend:
          serviceName: svc2
          servicePort: 8080
      - path: /
        backend:
          serviceName: svc3
          servicePort: 8080

PS:上面就是traefik 和 path的使用方法,通过配置ingress 填写对应的规则就可以了,不是很复杂。也描述了traefik 关于https证书的使用。

>>原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
>>原文链接地址:「走进k8s」Kubernetes1.15.1的Ingress TLS 与 Traefik路径转发(36)
上一篇: 下一篇:

发表评论

电子邮件地址不会被公开。 必填项已用*标注