創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,內(nèi)丘企業(yè)網(wǎng)站建設,內(nèi)丘品牌網(wǎng)站建設,網(wǎng)站定制,內(nèi)丘網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,
內(nèi)丘網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。>kubernetes部署Ingress訪問代理與負載均衡器
Kubernetes中的pod都有獨立的內(nèi)部IP(外部不可訪問),通過Service可以對多個pod進行負載均衡和故障轉移,Service可以具有ClusterIP、NodeIP或LoadBanlancer模式。目前,ClusterIP只能內(nèi)部訪問,需通過kubectl proxy代理出來,NodeIP是跟Node綁定的、遷移性差,LoadBanlancer的每個服務都有獨立的IP地址,管理、使用不便。有沒有一個固定的獨立IP、自動節(jié)點漂移的解決方案呢?以前這樣的功能基本上都用Nginx來實現(xiàn),現(xiàn)在Kubernetes有一個做好了的服務,也是基于Nginx的,就是Ingress。
如何訪問K8S中的服務:
1、Ingress介紹
Kubernetes 暴露服務的方式目前只有三種:LoadBlancer Service、NodePort Service、Ingress;前兩種估計都應該很熟悉,下面詳細的了解下這個 Ingress
Ingress由兩部分組成:Ingress Controller 和 Ingress 服務。
Ingress Contronler 通過與 Kubernetes API 交互,動態(tài)的去感知集群中 Ingress 規(guī)則變化,然后讀取它,按照自定義的規(guī)則,規(guī)則就是寫明了哪個域名對應哪個service,生成一段 Nginx 配置,再寫到 Nginx-ingress-control的 Pod 里,這個Ingress Contronler 的pod里面運行著一個nginx服務,控制器會把生成的nginx配置寫入/etc/nginx.conf文件中,然后 reload 一下使用配置生效。以此來達到域名分配置及動態(tài)更新的問題。
看個簡單的圖方便理解:
image.png
ingress控制器有兩種:nginx和haproxy 這里是以nginx為講解。
2、部署一個Nginx Ingress
ingress的部署文件在github Ingress 倉庫找到. 針對官方配置我們單獨添加了 nodeselector 指定,綁定LB地址 以方便DNS 做解析。
$ls
default-backend.yamljenkins-ingress.ymlnginx-ingress-controller-rbac.ymlnginx-ingress-controller.yaml
---
default-backend.yaml:這是官方要求必須要給的默認后端,提供404頁面的。它還提供了一個http檢測功能,檢測nginx-ingress-controll健康狀態(tài)的,通過每隔一定時間訪問nginx-ingress-controll的/healthz頁面,如是沒有響應就
返回404之類的錯誤碼。
nginx-ingress-controller-rbac.yml:這ingress的RBAC授權文件
nginx-ingress-controller.yaml:這是控制器的部署文件。
jenkins-ingress.yml:這是Ingress服務文件,這個可以是任意web程序,里面配置域名與service的對應關系,Ingress稱之為規(guī)則。
catnginx-ingress-controller-rbac.yml
#apiVersion:v1
#kind:Namespace
#metadata:#這里是創(chuàng)建一個namespace,因為此namespace早有了就不用再創(chuàng)建了
#name:kube-system
---
apiVersion:v1
kind:ServiceAccount
metadata:
name:nginx-ingress-serviceaccount#創(chuàng)建一個serveerAcount
namespace:kube-system
---
apiVersion:rbac.authorization.k8s.io/v1beta1
kind:ClusterRole
metadata:
name:nginx-ingress-clusterrole#這個ServiceAcount所綁定的集群角色
rules:
-apiGroups:
-""
resources:#此集群角色的權限,它能操作的API資源
-configmaps
-endpoints
-nodes
-pods
-secrets
verbs:
-list
-watch
-apiGroups:
-""
resources:
-nodes
verbs:
-get
-apiGroups:
-""
resources:
-services
verbs:
-get
-list
-watch
-apiGroups:
-"extensions"
resources:
-ingresses
verbs:
-get
-list
-watch
-apiGroups:
-""
resources:
-events
verbs:
-create
-patch
-apiGroups:
-"extensions"
resources:
-ingresses/status
verbs:
-update
---
apiVersion:rbac.authorization.k8s.io/v1beta1
kind:Role
metadata:
name:nginx-ingress-role#這是一個角色,而非集群角色
namespace:kube-system
rules:#角色的權限
-apiGroups:
-""
resources:
-configmaps
-pods
-secrets
-namespaces
verbs:
-get
-apiGroups:
-""
resources:
-configmaps
resourceNames:
#Defaultsto"-"
#Here:"-"
#Thishastobeadaptedifyouchangeeitherparameter
#whenlaunchingthenginx-ingress-controller.
-"ingress-controller-leader-nginx"
verbs:
-get
-update
-apiGroups:
-""
resources:
-configmaps
verbs:
-create
-apiGroups:
-""
resources:
-endpoints
verbs:
-get
-create
-update
---
apiVersion:rbac.authorization.k8s.io/v1beta1
kind:RoleBinding#角色綁定
metadata:
name:nginx-ingress-role-nisa-binding
namespace:kube-system
roleRef:
apiGroup:rbac.authorization.k8s.io
kind:Role
name:nginx-ingress-role
subjects:
-kind:ServiceAccount
name:nginx-ingress-serviceaccount#綁定在這個用戶
namespace:kube-system
---
apiVersion:rbac.authorization.k8s.io/v1beta1
kind:ClusterRoleBinding#集群綁定
metadata:
name:nginx-ingress-clusterrole-nisa-binding
roleRef:
apiGroup:rbac.authorization.k8s.io
kind:ClusterRole
name:nginx-ingress-clusterrole
subjects:
-kind:ServiceAccount
name:nginx-ingress-serviceaccount#集群綁定到這個serviceacount
namespace:kube-system#集群角色是可以跨namespace,但是這里只指明給這個namespce來使用
$kubectlcreate-fnginx-ingress-controller-rbac.yml
serviceaccount"nginx-ingress-serviceaccount"created
clusterrole"nginx-ingress-clusterrole"created
role"nginx-ingress-role"created
rolebinding"nginx-ingress-role-nisa-binding"created
clusterrolebinding"nginx-ingress-clusterrole-nisa-binding"created
RBAC創(chuàng)建完后,就創(chuàng)建default backend服務:
$catdefault-backend.yaml
apiVersion:extensions/v1beta1
kind:Deployment
metadata:
name:default-http-backend
labels:
k8s-app:default-http-backend
namespace:kube-system
spec:
replicas:1
template:
metadata:
labels:
k8s-app:default-http-backend
spec:
terminationGracePeriodSeconds:60
containers:
-name:default-http-backend
#Anyimageispermissableaslongas:
#1.Itservesa404pageat/
#2.Itserves200ona/healthzendpoint
image:gcr.io/google_containers/defaultbackend:1.0
livenessProbe:
httpGet:
path:/healthz#這個URI是nginx-ingress-controller中nginx里配置好的localtion
port:8080
scheme:HTTP
initialDelaySeconds:30#30s檢測一次/healthz
timeoutSeconds:5
ports:
-containerPort:8080
resources:
limits:
cpu:10m
memory:20Mi
requests:
cpu:10m
memory:20Mi
nodeSelector:#指定調(diào)度到些Node,以便后面DNS解析
kubernetes.io/hostname:10.3.1.17
---
apiVersion:v1
kind:Service#為defaultbackend創(chuàng)建一個service
metadata:
name:default-http-backend
namespace:kube-system
labels:
k8s-app:default-http-backend
spec:
ports:
-port:80
targetPort:8080
selector:
k8s-app:default-http-backend
創(chuàng)建:
1
2
3
$kubectlcreate-fdefault-backend.yaml
deployment"default-http-backend"created
service"default-http-backend"created
root@ubuntu15:/data/ingress#kubectlgetrs,pod,svc-nkube-system
NAMEDESIREDCURRENTREADYAGE
rs/default-http-backend-857b544d941111m
NAMEREADYSTATUSRESTARTSAGE
po/default-http-backend-857b544d94-bwgjd1/1Running01m
NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE
svc/default-http-backendClusterIP10.254.208.14480/TCP1m
創(chuàng)建好default backend后就要創(chuàng)建nginx-ingress-controller了:
$catnginx-ingress-controller.yaml apiVersion:extensions/v1beta1 kind:Deployment metadata: name:nginx-ingress-controller labels: k8s-app:nginx-ingress-controller namespace:kube-system spec: replicas:1 template: metadata: labels: k8s-app:nginx-ingress-controller spec: #hostNetworkmakesitpossibletouseipv6andtopreservethesourceIPcorrectlyregardlessofdockerconfiguration #however,itisnotaharddependencyofthenginx-ingress-controlleritselfanditmaycauseissuesifport10254alreadyistakenonthehost #thatsaid,sincehostPortisbrokenonCNI(https://github.com/kubernetes/kubernetes/issues/31307)wehavetousehostNetworkwhereCNIisused #likewithkubeadm #hostNetwork:true#注釋表示不使用宿主機的80口, terminationGracePeriodSeconds:60 hostNetwork:true#表示容器使用和宿主機一樣的網(wǎng)絡 serviceAccountName:nginx-ingress-serviceaccount#引用前面創(chuàng)建的serviceacount containers: -image:gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.1#容器使用的鏡像 name:nginx-ingress-controller#容器名 readinessProbe:#啟動這個服務時要驗證/healthz端口10254會在運行的node上監(jiān)聽。 httpGet: path:/healthz port:10254 scheme:HTTP livenessProbe: httpGet: path:/healthz port:10254 scheme:HTTP initialDelaySeconds:10#每隔10做健康檢查 timeoutSeconds:1 ports: -containerPort:80 hostPort:80#80映射到80 -containerPort:443 hostPort:443 env: -name:POD_NAME valueFrom: fieldRef: fieldPath:metadata.name -name:POD_NAMESPACE valueFrom: fieldRef: fieldPath:metadata.namespace args: -/nginx-ingress-controller ---default-backend-service=$(POD_NAMESPACE)/default-http-backend #---default-ssl-certificate=$(POD_NAMESPACE)/ingress-secret#這是啟用Https時用的 nodeSelector:#指明運行在哪,此IP要和defaultbackend是同一個IP kubernetes.io/hostname:10.3.1.17#上面映射到了hostport80,確保此IP80,443沒有占用.
這個控制器就是一個deployment ,里面運行一個容器gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.1 ,有點像nginx容器,現(xiàn)在創(chuàng)建:
1
2
$kubectlcreate-fnginx-ingress-controller.yaml
deployment"nginx-ingress-controller"created
root@ubuntu15:/data/ingress#kubectlgetrs,pod,svc-nkube-system
NAMEDESIREDCURRENTREADYAGE
rs/default-http-backend-857b544d9411112m
rs/nginx-ingress-controller-8576d4545d11027s
NAMEREADYSTATUSRESTARTSAGE
po/default-http-backend-857b544d94-bwgjd1/1Running012m
po/nginx-ingress-controller-8576d4545d-9tjnv0/1ContainerCreating027s
NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE
svc/default-http-backendClusterIP10.254.208.14480/TCP12m
現(xiàn)在ingress controller 控制器已部署好了,那么如何使用了,那就要寫一個ingress規(guī)則了,此處就以已存在的jenkins服務為例,配置如何使用域名訪問這個service:
$kubectlgetsvc,ep
NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE
svc/jenkinsserviceNodePort10.254.70.478080:30002/TCP3h
NAMEENDPOINTSAGE
ep/jenkinsservice172.30.10.15:8080,172.30.11.7:80803h
現(xiàn)在寫個jenkins service的Ingress 規(guī)則:
$catjenkins-ingress.yml
apiVersion:extensions/v1beta1
kind:Ingress
metadata:
name:jenkins-ingress
namespace:default#服務在哪個空間內(nèi)就寫哪個空間
annotations:
kubernetes.io/ingress.class:"nginx"
spec:
rules:
-host:ingress.jenkins.com#此service的訪問域名
http:
paths:
-backend:
serviceName:jenkinsservice
servicePort:8080
創(chuàng)建它:
$kubectlcreate-fjenkins-ingress.yml ingress"jenkins-ingress"created $kubectlgetingress NAMEHOSTSADDRESSPORTSAGE jenkins-ingressingress.jenkins.com8010s
到這里就已經(jīng)部署完成了,配置好域名后,就可以用此域名來訪問了:
image.png
部署完成了,現(xiàn)在看下nginx-ingress-controller 里nginx配置文件發(fā)生了哪些變化:
upstreamdefault-jenkinsservice-8080{ least_conn; server172.30.10.15:8080max_fails=0fail_timeout=0; server172.30.11.7:8080max_fails=0fail_timeout=0; } upstreamupstream-default-backend{ least_conn; server172.30.11.6:8080max_fails=0fail_timeout=0; } server{ server_nameingress.jenkins.com; listen[::]:80; location/{ ... proxy_passhttp://default-jenkinsservice-8080; ... } }
這些配置都是ingress-controller 自已寫入的,動態(tài)更新就是它能通過K8S API感知到service的endpoint 發(fā)生了變化,然后修改nginx配置并執(zhí)行reload.
至此,部署完成。
Ingress還有很多部署方式,比如配置https訪問的, 以后再寫。
本文名稱:kubernetes部署Ingress訪問代理與負載均衡器
標題來源:
http://weahome.cn/article/cjjchg.html