千家信息网

K8s之traefik(ingess)发布服务-实战

发表于:2025-01-27 作者:千家信息网编辑
千家信息网最后更新 2025年01月27日,K8s之traefik(ingess)发布服务实战上篇描述了基于k8s集群安装部署traefik作为ingress服务,简单演示了一下发布服务,本篇将细节讲述如果结合traefik发布业务服务。安装部
千家信息网最后更新 2025年01月27日K8s之traefik(ingess)发布服务-实战

K8s之traefik(ingess)发布服务实战

上篇描述了基于k8s集群安装部署traefik作为ingress服务,简单演示了一下发布服务,本篇将细节讲述如果结合traefik发布业务服务。

安装部署参考:

https://blog.51cto.com/michaelkang/2429929

版本介绍

traefik:v1.7k8s:v1.15.1

快速部署traefik

如果你的k8s集群已经部署完成,想快速部署traefik,执行命令如下:

kubectl create -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yamlkubectl create -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-ds.yaml执行成功,在浏览器数据一个node节点的IP地址:8080 端口即可看到traefik的管理页面。

实战开始

编写一个业务yaml文件

如果对yaml不太熟悉,看这边:https://blog.51cto.com/michaelkang/2429745

---#配置deploymentkind: DeploymentapiVersion: extensions/v1beta1metadata:#设置dm名称  name: dm-pttest#添加标签 pttest  labels:    app: pttestspec:# 通过replicas声明pod个数是2   replicas: 2# 通过标签选择被控制的pod    selector:    matchLabels:      app: pttest# 在template中定义pod   template:    metadata:# 给pod打上标签app=pttest      labels:        app: pttest    spec:      containers:# 声明容器名称,注意不是pod名称,pod名称应该定义在metadata中      - name: myapp        image: ikubernetes/myapp:v1        ports:        - containerPort: 80---#服务配置apiVersion: v1# 声明一个Service资源对象kind: Servicemetadata:#pod 名称  name: svcpttest  labels:    app: pttest  annotations:#检查后端服务返回错误率,大于%50,踢出集群    traefik.backend.circuitbreaker: "NetworkErrorRatio() > 0.5"spec:  ports:  - name: http    port: 80  selector:    app: pttest---#配置ingressapiVersion: extensions/v1beta1kind: Ingressmetadata:  name: myapp-traefik-ingress  annotations:    kubernetes.io/ingress.class: traefikspec:  rules:  - host: pttest.pt.com    http:      paths:      - path: /#指定后端服务        backend:          serviceName: svcpttest          servicePort: 80

验证服务

#查看服务[root@kubm-02 traefik]# kubectl get svc -o wideNAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE     SELECTORsvcpttest    ClusterIP   10.245.148.114           80/TCP    2m12s   app=pttest  <==lable#查看container[root@kubm-02 traefik]# kubectl get pods  -o wideNAME                         READY   STATUS    RESTARTS   AGE     IP             NODE         NOMINATED NODE   READINESS GATESdm-pttest-6f6cd797f5-cdjk5   1/1     Running   0          63s     10.244.3.115   kubnode-01              dm-pttest-6f6cd797f5-tqh64   1/1     Running   0          63s     10.244.4.135   kubnode-02              #ingress 信息[root@kubm-02 traefik]# kubectl get ingress                                NAME                    HOSTS           ADDRESS   PORTS   AGEmyapp-traefik-ingress   pttest.pt.com             80      23s#deployment[root@kubm-02 traefik]# kubectl get deployNAME        READY   UP-TO-DATE   AVAILABLE   AGEdm-pttest   2/2     2            2           12m

验证访问:

请求pttest.pt.com, 服务svcpttest会把请求负载给后端的pod。

#请求两次分别返回两个[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-cdjk5[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-tqh64

扩容节点

kubectl edit deployment dm-pttest

apiVersion: extensions/v1beta1kind: Deploymentmetadata:  annotations:    deployment.kubernetes.io/revision: "1"  creationTimestamp: "2019-08-16T10:13:49Z"  generation: 1  labels:    app: pttest  name: dm-pttest  namespace: default  resourceVersion: "3871589"  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/dm-pttest  uid: 153a60fb-66df-4884-aa9a-49ac47cebd2cspec:  progressDeadlineSeconds: 2147483647  replicas: 5                             《=====pod数量,有默认2个,调整到5个保存退出,k8s集群会自动执行;

验证

kubectl get pods

[root@kubm-02 traefik]# kubectl get podsNAME                         READY   STATUS    RESTARTS   AGEdm-pttest-6f6cd797f5-cdjk5   1/1     Running   0          17mdm-pttest-6f6cd797f5-qd4cl   1/1     Running   0          99sdm-pttest-6f6cd797f5-r25dc   1/1     Running   0          99sdm-pttest-6f6cd797f5-tbfmh   1/1     Running   0          99sdm-pttest-6f6cd797f5-tqh64   1/1     Running   0          17m#请求[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-r25dc[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-tbfmh[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-tqh64[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-cdjk5[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-qd4cl[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-r25dc[root@kubm-02 traefik]# curl -H "HOST:pttest.pt.com" http://172.20.101.166/hostname.htmldm-pttest-6f6cd797f5-tbfmh

使用命令调整pod数量

调整pod数量为1;kubectl scale deployment dm-pttest --replicas=1调整pod数量为2;kubectl scale deployment dm-pttest --replicas=2

参考文档

https://kubernetes.io/zh/docs/concepts/services-networking/connect-applications-service/
https://blog.csdn.net/u010606397/article/details/90752262

0