我是靠谱客的博主 美满航空,最近开发中收集的这篇文章主要介绍traefik https配置,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

转自:https://blog.csdn.net/lusyoe/article/details/81298231

在kubernetes中使用traefik暴露服务,添加https支持

使用购买的证书或者私签证书

私签证书:

# 私钥
openssl genrsa -out rsa_private_key.pem 2048

# 生成公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

openssl req -new -out ca-req.csr -key rsa_private_key.pem

openssl x509 -req -in ca-req.csr -out ca-cert.pem -signkey rsa_private_key.pem -days 3650

traefik配置

购买证书的traefik.toml文件:

apiVersion: v1
kind: ConfigMap
metadata:
  name: traefik-conf
  namespace: kube-system
data:
  traefik.toml: |
    defaultEntryPoints = ["http","https"]
    [entryPoints]
      [entryPoints.http]
       address = ":80"
       compress = true
         [entryPoints.http.redirect]
          entryPoint = "https"
      [entryPoints.https]
       address = ":443"
       compress = true
         [entryPoints.https.tls]
           [[entryPoints.https.tls.certificates]]
           certFile = "/ssl/tls.crt"
           keyFile = "/ssl/tls.key

注: 其中tls.crt和tls.key就是证书文件,注意必须要改为这个文件名,否则会报错

[entryPoints.http.redirect]  entryPoint = "https" 的配置是把入口为http的访问强制转为https. 

可以用其他方式实现:将配置文件中的[entryPoints.http.redirect] entryPoint = "https" 去掉,在ingress的annotations中配置:ingress.kubernetes.io/ssl-redirect: "true" 。就可以将入口为http的访问强制转为https。

私签证书配置:

apiVersion: v1
kind: ConfigMap
metadata:
  name: traefik-conf
  namespace: kube-system
data:
  traefik.toml: |
    defaultEntryPoints = ["http","https"]
    [entryPoints]
      [entryPoints.http]
       address = ":80"
       compress = true
         [entryPoints.http.redirect]
          entryPoint = "https"
      [entryPoints.https]
       address = ":443"
       compress = true
         [entryPoints.https.tls]
           [[entryPoints.https.tls.certificates]]
           certFile = "/ssl/ca-cert.pem"
           keyFile = "/ssl/rsa_private_key.pem

创建configmap

kubectl create -f traefik-configmap.yaml

Traefik secret 配置

购买证书:

kubectl create secret tls traefik-cert --key tls.key --cert tls.crt -n kube-system

私签证书:

kubectl create secret generic traefik-cert --from-file=rsa_private_key.pem --from-file=ca-cert.pem -n kube-system

注:由于secret是不能跨命名空间的,如果应用是部署在default命名空间,那还需要在default命名空间创建一个该secret,去掉上面最后面的-n kube-system即可

k8s traefik部署配置

添加traefik-ingress.yaml文件

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: traefik-ingress-controller
rules:
  - apiGroups:
      - ""
    resources:
      - pods
      - services
      - endpoints
      - secrets
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - extensions
    resources:
      - ingresses
    verbs:
      - get
      - list
      - watch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: traefik-ingress-controller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: traefik-ingress-controller
subjects:
- kind: ServiceAccount
  name: traefik-ingress-controller
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
---
kind: Deployment
apiVersion: apps/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
      containers:
      - image: traefik:v1.6
        imagePullPolicy: IfNotPresent
        name: traefik-ingress-lb
        volumeMounts:
        - mountPath: "/ssl"
          name: "ssl"
        - mountPath: "/config"
          name: "config"
        ports:
        - containerPort: 80
        - containerPort: 443
        args:
        - --web
        - --kubernetes
        - --configfile=/config/traefik.toml
---
kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
  namespace: kube-system
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - protocol: TCP
      # 该端口为 traefik ingress-controller的服务端口
      port: 80
      # 集群hosts文件中设置的 NODE_PORT_RANGE 作为 NodePort的可用范围
      # 从默认20000~40000之间选一个可用端口,让ingress-controller暴露给外部的访问
      nodePort: 80
      name: web
    - protocol: TCP
      # 该端口为 traefik 的管理WEB界面
      port: 8080
      name: admin
    - protocol: TCP
      port: 443
      nodePort: 443
      name: https
  type: NodePort

注: https配置同样适用于deamon-set的形式

traefik-ssl-ds.yaml

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
---
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
  labels:
    k8s-app: traefik-ingress-lb
spec:
  template:
    metadata:
      labels:
        k8s-app: traefik-ingress-lb
        name: traefik-ingress-lb
    spec:
      serviceAccountName: traefik-ingress-controller
      terminationGracePeriodSeconds: 60
      containers:
      - image: traefik
        name: traefik-ingress-lb
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: admin
          containerPort: 8080
          hostPort: 8580
        - name: https
          containerPort: 443
          hostPort: 443
        securityContext:
          capabilities:
            drop:
            - ALL
            add:
            - NET_BIND_SERVICE
        args:
        - --api
        - --kubernetes
        - --logLevel=INFO
        - --configfile=/config/traefik.toml
        - --web
        volumeMounts:
        - mountPath: "/ssl"
          name: "ssl"
        - mountPath: "/config"
          name: "config"
      volumes:
      - name: ssl
        secret:
          secretName: traefik-cert
      - name: config
        configMap:
          name: traefik-conf
---
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
    - protocol: TCP
      port: 8443
      name: https

ingress 配置:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: k8s-example
  annotations:
    kubernetes.io/ingress.class: "traefik"
spec:
  tls:
  - secretName: traefik-cert
  rules:
  - host: k8s-example.luhaoyuan.com
    http:
      paths:
      - backend:
          serviceName: k8s-example
          servicePort: k8s-backend

spec下面多了tls的配置,secretName为之前新建的secret名。

 

最后

以上就是美满航空为你收集整理的traefik https配置的全部内容,希望文章能够帮你解决traefik https配置所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部