WEBAPP开发之Kubernetes 实践
白羽 2019-01-02 来源 :网络 阅读 706 评论 0

摘要:本文将带你了解WEBAPP开发Kubernetes 实践之 service,希望本文对大家学WEBAPP有所帮助。

    本文将带你了解WEBAPP开发Kubernetes 实践之 service,希望本文对大家学WEBAPP有所帮助。


               

              


service是k8s最核心的概念。通过创建service,可以为一组具有相同功能的容器应用提供一个统一的入口地址,并且将请求负载分发到后端的各个容器应用上去

 

service定义详解

ymal格式的service定义文件的完整内容如下

apiVersion: v1            #必须kind: Service              #必须matadata:               #必须,元数据  name: string               #必须,service的名称  namespace: string          #非必须,指定的namespace名称,与rc在同一个namespace即可    labels:                #非必须,service的标签      - name: string    annotations:             #非必须,service的注解属性信息      - name: stringspec:                  #必须,详细描述    selector: []                #必须,lable   selector设置,选择具有执行lable标签的pod作为管理范文    type: string             #必须,service的类型,指定service的访问方式,默认为cluster   ip,用于k8s内部的pod访问, node上的kube-proxy通过设置iptables 转发                         nodePort,使用   宿主机的端口,是能够访问各node的外部客户通过node的ip地址和端口就能访问服务
                                     loadbalancer:   使用外接负载均衡完成服务到负载的分发,需要在spec.status.loadBalancer字段指定负载均衡器的ip,并同时定义nodePort和clusterIP
  clusterIP: string          #非必须,虚拟服务器ip地址    sessionAffinity: string       #非必须,是否支持session,可选值为cluster   ip    ports:                 #端口           - name: string            #端口名称        protocol: string          #协议类型    port:   int              #服务监听的端口号,service   clusterip的端口    targetPort:   int             #需要转发到后端pod的端口    nodePort:   int            #container port   映射到宿主机的端口  status:                 #当type   为loadBalancer时,说这负载均衡的地址      loadBalancer:        ingress:              #外部负载均衡          ip: string           #负载均衡器的ip地址          hostname: string        #外部负载均衡的主机名

 

一、service的基本用法

创建一个包含两个tomcat副本rc,并为其创建一个service

[root@k8s_master tomcat-service-rc]#   cat   webapp-rc.yaml apiVersion: v1kind:   ReplicationController metadata:       name: webapp    labels:        name: webappspec:     replicas: 2      selector:        name:   webapp    template:         metadata:             labels:                 name: webapp        spec:             containers:             - name: webapp                image: tomcat                imagePullPolicy: IfNotPresent              ports:              - containerPort:   8080[root@k8s_master   tomcat-service-rc]# kubectl create -f webapp-rc.yaml  #创建service并查看[root@k8s_master   tomcat-service-rc]# kubectl   expose rc webapp    #通过命令行快速创建,一般不采用service   "webapp" exposed[root@k8s_master tomcat-service-rc]#   kubectl   get svcNAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGEkubernetes   10.254.0.1         <none>          443/TCP      272dwebapp         10.254.185.154     <none>          8080/TCP     12s[root@k8s_master tomcat-service-rc]# kubectl describe svc webappName:            webappNamespace:        defaultLabels:            name=webappSelector:        name=webappType:            ClusterIPIP:            10.254.185.154Port:              <unset>      8080/TCPEndpoints:          10.1.20.2:8080,10.1.34.2:8080Session Affinity:      NoneNo events.此时在两个node   上通过clusterip:8080 均可以访问   tomcat服务,service将请求分发到两台容器上,默认采用了RoundRobin(轮训rr)模式,
  SessionAffinity :   基于客户端ip地址进行会话保持的模式,即第一次访问哪个pod,以后的请求都访问这个pod
#通过yaml文件的方式创建自定义的service[root@k8s_master   tomcat-service-rc]# cat webapp-service.yaml apiVersion: v1kind: Servicemetadata:     name: webapp1    labels:        name: webappspec:     ports:      - port: 8081          targetPort: 8081    selector:        name: webapp[root@k8s_master   tomcat-service-rc]# kubectl create -f webapp-service.yaml#查看两个service[root@k8s_master   tomcat-service-rc]# kubectl   describe svc webapp webapp1Name:            webappNamespace:        defaultLabels:            name=webappSelector:        name=webappType:            ClusterIPIP:            10.254.185.154Port:              <unset>      8080/TCPEndpoints:          10.1.20.2:8080,10.1.34.2:8080Session Affinity:      NoneNo events.Name:              webapp1Namespace:          defaultLabels:              name=webappSelector:          name=webappType:              ClusterIPIP:              10.254.179.138Port:              <unset>      8081/TCPEndpoints:          10.1.20.2:8080,10.1.34.2:8080Session Affinity:      NoneNo events.

 

创建一个不带标签选择器的service,即无法选择后端pod,系统不会自动创建endpoint,因此需要手动创建一个和该service同名的endpoint,用于指向实际的后端访问地址,如下:

 

二、集群外部访问pod或者service

  pod和service是集群内的虚拟概念,所有集群外的客户端系统无法通过pod的ip地址或者service的虚拟ip地址和端口访问到他们,为了让客户端能够访问到他们,需要将pod或者service的地址和端口映射到宿主机。

①、将容器应用的端口映射到物理机

容器级别映射

apiVersion: v1kind: Podmetadata:  name: webapp  labels:      name: webappspec:    containers:    - name:   webapp    image: tomcat    ports:      - containerPort: 8080       #容器端口      hostPort:   8081          #宿主机端口(物理机)

 

  pod级别的设置,hostNetwork=true,该pod中所有容器的端口号都被直接映射到物理机上,使用此设置的时候要注意,如果该容器的ports定义部分不指定hostPort(物理机端口),则默认hostPort等于containerPort,如果指定了hostPort,则hostPort必须等于containerPort的值

 

apiVersion: v1kind: Podmetadata:  name: webapp  labels:      name: webappspec:  hostNetwork:   true  containers:  - name: webapp      image: tomcat    ports:    - containerPort: 8080

此时会将tomcat容器运行的所有端口映射到创建pod的物理机上

 

②、将service的端口号映射到物理机

A、设置NodePort映射到物理机,同时设置service的类型为NodePort

[root@k8s_master tomcat-service-rc]#   cat   webapp-rc.yaml     #创建一个rcapiVersion: v1kind:   ReplicationController metadata:       name: webapp    labels:        name: webappspec:     replicas: 2      selector:        name:   webapp    template:         metadata:             labels:                 name: webapp        spec:             containers:             - name: webapp              image: tomcat              imagePullPolicy:   IfNotPresent              ports:              - containerPort:   8080[root@k8s_master tomcat-service-rc]#   cat webapp-service.yamlapiVersion: v1kind: Servicemetadata:     name: webapp    labels:        name: webappspec:     type:   NodePort    ports:      - port: 8080          #service虚拟端口          targetPort: 8080      #容器端口        nodePort:   30001        #各node节点开启的端口,端口范围   3000-32767    selector:        name:   webapp

 

查看

[root@k8s_master tomcat-service-rc]#   kubectl get svc   -o wideNAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE       SELECTORkubernetes   10.254.0.1        <none>          443/TCP          273d      <none>webapp       10.254.44.190   <nodes>       8080:30001/TCP     24s         name=webapp

 

B、设置LoadBalancer映射到公网云服务商(或nginx/lvs/harpoxy)提供的LoadBalancer地址,对该service的访问请求将会通过LoadBalancer转发到后端pod上,分发机制依赖于LoadBalancer的实现机制,仅供格式参考

apiVersion: v1kind: Servicemetadata:    name: my-webapp      labels:        name:   my-webappspec:    ports:      - protocol: TCP        port: 8080          targetPort: 8080        nodePort: 30001      clusterIP: 10.254.44.191    LoadBalancerIP:   10.20.203.100    type:   LoadBalancer    selector:        name: webappstatus:    LoadBalancer:        ingress:        - ip: 10.20.203.100

 

三、DNS服务搭建

  为了实现通过服务的名字在集群内部进行服务的相互访问,需要创建虚拟DNS服务来完成服务名到clusterip的解析

k8s提供的虚拟dns服务名为skydns,由四个组件构成

(1) etcd:   DNS存储

(2) kube2sky: 将k8s master中的service   注册到etcd

(3) skydns: 提供skydns的解析服务

(4)   healthz:提供对skydns服务的健康检查功能

 

工作原理解析

1、kube2sky容器应用通过调用k8s master的api获得集群中所有srvice的信息,并持续监控新service的生成,然后写入etcd中

2、根据kubelet启动参数设置(--cluster-dns),kubectl会在每个新创建的pod中设置dns域名解析配置文件/etc/resolv.conf,在其中增加一条nameserver和一条search

3、最后应用程序就能够像访问网站域名一样,通过服务的名字就能访问到服务器了

具体搭建信息看   kubernetes   DNS

 

四、Ingress: HTTP   7层路由

  service的表现形式为IP.Port,工作在tcp/ip层,对于基于http的服务来说,不同的url地址要对应到不同的后端服务或者虚拟服务器上去,这些要求通过service机制无法实现,k8s 1.1以后增加的ingress   可以将不同的url访问请求转发到后端不同的service,实现http的业务路由机制,在k8s中需要ingress的定义和ingress   controller的定义结合起来,才能形成完整的http负载分发能力



                     

                 

                                 

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之WebApp频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程