K8s of traefik (ingess) publishing service - combat

K8s of traefik (ingess) combat Publishing Service

Part I describes the installation based on k8s cluster deployment traefik as ingress service, a bit simple demonstration of publishing services, business services This article deals about the details if combined traefik release.

Setup Deployment Reference:

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

Version Introduction

traefik:v1.7
k8s:v1.15.1

Rapid deployment traefik

If you k8s cluster has been deployed, I want to quickly deploy traefik, execute the following command:

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的管理页面。

Actual start

Writing a business document yaml

If you are not familiar with yaml, look here: 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

Authentication Service

#查看服务
[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   <none>        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   <none>           <none>
dm-pttest-6f6cd797f5-tqh54   1/1     Running   0          63s     10.244.4.135   kubnode-02   <none>           <none>

#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

Verification visit:

Request pttest.pt.com, service svcpttest will request load to the back-end of the 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-tqh54

Node expansion

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集群会自动执行;

verification

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-tqh54   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-tqh54
[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

Use command to adjust the number of pod

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

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

Reference Documents

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

Guess you like

Origin blog.51cto.com/michaelkang/2430244