理解Ingress前端
簡單的說,ingress就是從kubernetes集羣外訪問集羣的入口,將用戶的URL請求轉發到不一樣的service上。Ingress至關於nginx、apache等負載均衡方向代理服務器,其中還包括規則定義,即URL的路由信息,路由信息得的刷新由Ingress controller來提供。node
理解Ingress Controllernginx
Ingress Controller 實質上能夠理解爲是個監視器,Ingress Controller 經過不斷地跟 kubernetes API 打交道,實時的感知後端 service、pod 等變化,好比新增和減小 pod,service 增長與減小等;當獲得這些變化信息後,Ingress Controller 再結合下文的 Ingress 生成配置,而後更新反向代理負載均衡器,並刷新其配置,達到服務發現的做用。git
部署Traefik
Traefik是一款開源的反向代理與負載均衡工具。它最大的優勢是可以與常見的微服務系統直接整合,能夠實現自動化動態配置。目前支持Docker, Swarm, Mesos/Marathon, Mesos, Kubernetes, Consul, Etcd, Zookeeper, BoltDB, Rest API等等後端模型。github
配置文件web
ingress-rbac.yaml算法
apiVersion: v1 kind: ServiceAccount metadata: name: ingress namespace: kube-system --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: ingress subjects: - kind: ServiceAccount name: ingress namespace: kube-system roleRef: kind: ClusterRole name: cluster-admin apiGroup: rbac.authorization.k8s.io
建立名爲traefik-ingress
的ingress,文件名ingress.yamlapache
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: traefik-ingress namespace: default spec: rules: - host: traefik.locust.io http: paths: - path: / backend: serviceName: locust-master #確保此service-name在 上面指定的namespace中 servicePort: 8089
這其中的backend
中要配置default namespace中啓動的service名字,若是你沒有配置namespace名字,默認使用default namespace,若是你在其餘namespace中建立服務想要暴露到kubernetes集羣外部,能夠建立新的ingress.yaml文件,同時在文件中指定該namespace
,其餘配置與上面的文件格式相同。。path
就是URL地址後的路徑,如traefik.frontend.io/path,service將會接受path這個路徑,host最好使用service-name.filed1.filed2.domain-name這種相似主機名稱的命名方式,方便區分服務。後端
根據你本身環境中部署的service的名字和端口自行修改,有新service增長時,修改該文件後能夠使用kubectl replace -f ingress.yaml
來更新。api
使用DaemonSet類型來部署Traefik,文件名 treafik.yml
apiVersion: extensions/v1beta1 kind: DaemonSet metadata: name: traefik-ingress-lb namespace: kube-system labels: k8s-app: traefik-ingress-lb spec: template: metadata: labels: k8s-app: traefik-ingress-lb name: traefik-ingress-lb spec: terminationGracePeriodSeconds: 60 hostNetwork: true restartPolicy: Always serviceAccountName: ingress containers: - image: traefik name: traefik-ingress-lb resources: limits: cpu: 200m memory: 30Mi requests: cpu: 100m memory: 20Mi ports: #暴露端口 - name: http containerPort: 80 hostPort: 80 - name: admin containerPort: 8580 hostPort: 8580 args: - --web - --web.address=:8580 - --kubernetes #nodeSelector: #edgenode: "true"
查看pod信息並訪問ui(須要配置)
http://10.10.10.12:8580/dashboard/
以下所示
默認狀況下是沒任何東西的
使用下面的yaml配置來建立Traefik的ingress,文件名 ui.yml
apiVersion: v1 kind: Service metadata: name: traefik-web-ui namespace: kube-system spec: selector: k8s-app: traefik-ingress-lb ports: - name: web port: 80 targetPort: 8580 --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: traefik-web-ui namespace: kube-system spec: rules: - host: test.traefik-ingress.io http: paths: - path: / backend: serviceName: traefik-web-ui servicePort: web
建立
kubectl create -f .
部署Locust的Controller和Service
locust是啥。。自行百度
$ git clone https://github.com/rootsongjc/distributed-load-testing-using-kubernetes.git $ cd distributed-load-testing-using-kubernetes/kubernetes-config
修改locust-master-cotnroller中spec.template.spec.containers.env
字段中的value爲你sample-webapp
service的名字(要進行壓力測試的服務的地址)。例:
- name: TARGET_HOST value: http://jenkins.kube-system.svc.cluster.local:8080
部署locust-master
$ kubectl create -f locust-master-controller.yaml $ kubectl create -f locust-master-service.yaml
部署locust-worker
$ kubectl create -f locust-worker-controller.yaml
#worker擴容
$ kubectl scale --replicas=20 replicationcontrollers locust-worker
配置Traefik
ingress.yaml
中加入以下配置(上面已經配置):
- host: traefik.locust.io http: paths: - path: / backend: serviceName: locust-master #namespace要寫對 servicePort: 8089
而後執行kubectl replace -f ingress.yaml
便可更新traefik。
本地配置host,而後訪問traefik.locust.io
start swarming就會開始測試
設置邊緣節點
首先解釋下什麼叫邊緣節點(Edge Node),所謂的邊緣節點即集羣內部用來向集羣外暴露服務能力的節點,集羣外部的服務經過該節點來調用集羣內部的服務,邊緣節點是集羣內外交流的一個Endpoint。
邊緣節點要考慮兩個問題
- 邊緣節點的高可用,不能有單點故障,不然整個kubernetes集羣將不可用
- 對外的一致暴露端口,即只能有一個外網訪問IP和端口
在Kubernetes中添加了service的同時,在DNS中增長一個記錄,這條記錄須要跟ingress中的host
字段相同,IP地址即VIP的地址,本示例中是10.10.10.166
,這樣集羣外部就能夠經過service的DNS名稱來訪問服務了。
選擇Kubernetes的兩個個node做爲邊緣節點,並安裝keepalived,下圖展現了邊緣節點的配置,同時展現了向Kubernetes中添加服務的過程。
配置說明
須要對原先的traefik ingress進行改造,從以Deployment方式啓動改爲DeamonSet。還須要指定一個與node在同一網段的IP地址做爲VIP,這裏指定成10.10.10.166,配置keepalived前須要先保證這個IP沒有被分配。。
- Traefik以DaemonSet的方式啓動
- 經過hostPort暴露端口
- 當前VIP漂移到10.10.10.166上
- Traefik根據訪問的host和path配置,將流量轉發到相應的service上
-
安裝keepalived並配置
yum install keepalived ipvsadm
配置
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from kaadmin@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.10.10.166 } } virtual_server 10.10.10.166 80{ delay_loop 6 lb_algo loadbalance lb_kind DR nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 10.10.10.12 80{ weight 1 TCP_CHECK { connect_timeout 3 } } real_server 10.10.10.162 80{ weight 1 TCP_CHECK { connect_timeout 3 } } }
Realserver
的IP和端口即traefik供外網訪問的IP和端口。將以上配置分別拷貝到另一臺node的/etc/keepalived
目錄下。
使用轉發效率最高的lb_kind DR
直接路由方式轉發,使用TCP_CHECK來檢測real_server的health。
設置keepalived爲開機自啓動:
chkconfig keepalived on
啓動keepalived
systemctl start keepalived
node都啓動了keepalived後,觀察eth0的IP,會在兩臺node的某一臺上發現一個VIP是10.10.10.166。
-
重新配置traefik ingress
因爲上面配置了keepalived,此時,本地hosts 文件的解析地址能夠寫成10.10.10.166,使用基於域名的traefik ingress
以上面的locust爲例,配置traefik ingress以下
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: traefik-ingress namespace: default annotations: #必須指定 kubernetes.io/ingress.class: traefik traefik.frontend.rule.type: PathPrefixStrip spec: rules: #- host: traefik.locust.io - host: test.traefik-ingress.io http: paths: - path: /locust #此處配置了url,上面必須指定annotations指定的配置 backend: serviceName: locust-master servicePort: 8089
建立好之後,訪問 test.traefik-ingress.io/locust
其餘服務同理,host可寫test.traefik-ingress.io,只配置path用於區分不一樣的服務,訪問的時候訪問host/path便可
Traefik在Kubernetes中Annotate
的配置
通用配置 kubernetes.io/ingress.class: traefik #Ingress聲明,這裏聲明瞭ingress後端採用traefik實現,而不是nginx的controller ingress.kubernetes.io/whitelist-source-range: "1.2.3.0/24, fe80::/16" #配置訪問白名單,支持ipv4和ipv6 ingress.kubernetes.io/auth-type: basic #http認證模式,此處爲basic模式 ingress.kubernetes.io/auth-secret: mysecret #basic認證的對應的username和password,這裏對應的traefik所在kubernetes命名空間裏的secrets
前端配置 traefik.frontend.rule.type: PathPrefixStrip traefik.frontend.priority: "3" #配置前端的權重,值越高則優先匹配 traefik.frontend.passHostHeader: "false" #關閉傳入Hearder traefik.protocol=https #使用https協議 traefik.frontend.entryPoints=http,https #同時支持http和https 後端配置 traefik.backend.loadbalancer.method=drr #後端Service的負載均衡策略,目前traefik支持的策略包括:wrr(加權輪訓調度算法)和drr(動態加權循環調度算法) traefik.backend.loadbalancer.stickiness=true #是否開啓負載均衡器的session親和性 traefik.backend.loadbalancer.stickiness.cookieName=NAME #手動配置後端session親和性的cookie名稱 traefik.backend.loadbalancer.sticky=true 棄用 健康檢查 traefik.backend.healthcheck.path=/health traefik的監控檢查路徑 traefik.backend.healthcheck.interval=5s 健康檢查的時間間隔 traefik.backend.circuitbreaker: "NetworkErrorRatio() > 0.5" 監測某臺節點上的服務錯誤率達到50%時,自動下線該節點。 traefik.backend.circuitbreaker: "LatencyAtQuantileMS(50.0) > 50" 監測某臺節點上服務的延時大於50ms時,自動下線該節點。 traefik.backend.circuitbreaker: "ResponseCodeRatio(500, 600, 0, 600) > 0.5" 監測某臺節點上服務返回狀態碼爲[500-600]在[0-600]區間佔比超過50%時,自動下線該節點。