189 8069 5689

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

云计算

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

创新互联专注于新城网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供新城营销型网站建设,新城网站制作、新城网页设计、新城网站官网定制、小程序开发服务,打造新城网络公司原创品牌,更为您提供新城网站排名全网营销落地服务。

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

安装部署参考:

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

版本介绍
traefik:v1.7
k8s:v1.15.1
快速部署traefik

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

kubectl create -f https://raw.githubusercontent.com/containous/traefik/v1.7/examples/k8s/traefik-rbac.yaml
kubectl 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

---
#配置deployment
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
#设置dm名称
  name: dm-pttest
#添加标签 pttest
  labels:
    app: pttest
spec:
# 通过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: Service
metadata:
#pod 名称
  name: svcpttest
  labels:
    app: pttest
  annotations:
#检查后端服务返回错误率,大于%50,踢出集群
    traefik.backend.circuitbreaker: NetworkErrorRatio() > 0.5
spec:
  ports:
  - name: http
    port: 80
  selector:
    app: pttest

---
#配置ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myapp-traefik-ingress
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: pttest.pt.com
    http:
      paths:
      - path: /
#指定后端服务
        backend:
          serviceName: svcpttest
          servicePort: 80
验证服务
#查看服务
[root@kubm-02 traefik]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE     SELECTOR

svcpttest    ClusterIP   10.245.148.114           80/TCP    2m12s   app=pttest  <==lable

#查看container
[root@kubm-02 traefik]# kubectl get pods  -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP             NODE         NOMINATED NODE   READINESS GATES
dm-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   AGE
myapp-traefik-ingress   pttest.pt.com             80      23s

#deployment
[root@kubm-02 traefik]# kubectl get deploy
NAME        READY   UP-TO-DATE   AVAILABLE   AGE

dm-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.html
dm-pttest-6f6cd797f5-cdjk5

[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-tqh64
扩容节点

kubectl edit deployment dm-pttest

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  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-49ac47cebd2c
spec:
  progressDeadlineSeconds: 2147483647
  replicas: 5                             《=====pod数量,有默认2个,调整到5个

保存退出,k8s集群会自动执行;
验证

kubectl get pods

[root@kubm-02 traefik]# kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
dm-pttest-6f6cd797f5-cdjk5   1/1     Running   0          17m
dm-pttest-6f6cd797f5-qd4cl   1/1     Running   0          99s
dm-pttest-6f6cd797f5-r25dc   1/1     Running   0          99s
dm-pttest-6f6cd797f5-tbfmh   1/1     Running   0          99s
dm-pttest-6f6cd797f5-tqh64   1/1     Running   0          17m

#请求
[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-r25dc
[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-tbfmh
[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-tqh64
[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-cdjk5
[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-qd4cl
[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-pttest-6f6cd797f5-r25dc
[root@kubm-02 traefik]# curl -H HOST:pttest.pt.com http://172.20.101.166/hostname.html
dm-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


当前名称:K8s之traefik(ingess)发布服务-实战
网站地址:http://cdxtjz.cn/article/cjpcie.html

其他资讯