service

三种工作模式:userspace、iptables、ipvs

删除手动创建的service

[root@master ~]# kubectl delete svc redis
service "redis" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17d
myapp NodePort 10.103.191.244 <none> 80:31339/TCP 17d
nginx ClusterIP 10.108.177.175 <none> 80/TCP 17d

定义service 的顶级资源清单帮助

[root@master ~]# kubectl explain svc
KIND: Service
VERSION: v1 DESCRIPTION:
Service is a named abstraction of software service (for example, mysql)
consisting of local port (for example 3306) that the proxy listens on, and
the selector that determines which pods will answer requests sent through
the proxy. FIELDS:
apiVersion <string> 版本
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources kind <string> 类型
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds metadata <Object> 元数据
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata spec <Object> 期望状态
Spec defines the behavior of a service.
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status status <Object>
Most recently observed status of the service. Populated by the system.
Read-only. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

  spec 的重要字段介绍

[root@master ~]# kubectl explain svc.spec
KIND: Service
VERSION: v1 RESOURCE: spec <Object> DESCRIPTION:
Spec defines the behavior of a service.
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status ServiceSpec describes the attributes that a user creates on a service. FIELDS:
clusterIP <string> 集群的podIP,动态分配的,如果想要固定地址可以使用这个字段定义
clusterIP is the IP address of the service and is usually assigned randomly
by the master. If an address is specified manually and is not in use by
others, it will be allocated to the service; otherwise, creation of the
service will fail. This field can not be changed through updates. Valid
values are "None", empty string (""), or a valid IP address. "None" can be
specified for headless services when proxying is not required. Only applies
to types ClusterIP, NodePort, and LoadBalancer. Ignored if type is
ExternalName. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies externalIPs <[]string>
externalIPs is a list of IP addresses for which nodes in the cluster will
also accept traffic for this service. These IPs are not managed by
Kubernetes. The user is responsible for ensuring that traffic arrives at a
node with this IP. A common example is external load-balancers that are not
part of the Kubernetes system. externalName <string>
externalName is the external reference that kubedns or equivalent will
return as a CNAME record for this service. No proxying will be involved.
Must be a valid RFC-1123 hostname (https://tools.ietf.org/html/rfc1123) and
requires Type to be ExternalName. externalTrafficPolicy <string>
externalTrafficPolicy denotes if this Service desires to route external
traffic to node-local or cluster-wide endpoints. "Local" preserves the
client source IP and avoids a second hop for LoadBalancer and Nodeport type
services, but risks potentially imbalanced traffic spreading. "Cluster"
obscures the client source IP and may cause a second hop to another node,
but should have good overall load-spreading. healthCheckNodePort <integer>
healthCheckNodePort specifies the healthcheck nodePort for the service. If
not specified, HealthCheckNodePort is created by the service api backend
with the allocated nodePort. Will use user-specified nodePort value if
specified by the client. Only effects when Type is set to LoadBalancer and
ExternalTrafficPolicy is set to Local. loadBalancerIP <string>
Only applies to Service Type: LoadBalancer LoadBalancer will get created
with the IP specified in this field. This feature depends on whether the
underlying cloud-provider supports specifying the loadBalancerIP when a
load balancer is created. This field will be ignored if the cloud-provider
does not support the feature. loadBalancerSourceRanges <[]string>
If specified and supported by the platform, this will restrict traffic
through the cloud-provider load-balancer will be restricted to the
specified client IPs. This field will be ignored if the cloud-provider does
not support the feature." More info:
https://kubernetes.io/docs/tasks/access-application-cluster/configure-cloud-provider-firewall/ ports <[]Object> 端口与容器端口建立关系的定义
The list of ports that are exposed by this service. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies publishNotReadyAddresses <boolean>
publishNotReadyAddresses, when set to true, indicates that DNS
implementations must publish the notReadyAddresses of subsets for the
Endpoints associated with the Service. The default value is false. The
primary use case for setting this field is to use a StatefulSet's Headless
Service to propagate SRV records for its Pods without respect to their
readiness for purpose of peer discovery. selector <map[string]string> 关联到那些pod资源上
Route service traffic to pods with label keys and values matching this
selector. If empty or not present, the service is assumed to have an
external process managing its endpoints, which Kubernetes will not modify.
Only applies to types ClusterIP, NodePort, and LoadBalancer. Ignored if
type is ExternalName. More info:
https://kubernetes.io/docs/concepts/services-networking/service/ sessionAffinity <string> 会话粘性
Supports "ClientIP" and "None". Used to maintain session affinity. Enable
client IP based session affinity. Must be ClientIP or None. Defaults to
None. More info:
https://kubernetes.io/docs/concepts/services -networking/service/#virtual-ips-and-service-proxies sessionAffinityConfig <Object>
sessionAffinityConfig contains the configurations of session affinity. type <string> 指定type ,ClusterIP集群内部的通信地址NodePort 节点的地址供外部访问 LoadBalancer:公有云的负载均衡器 ExternalName:集群外部服务引入到集群内部使用
type determines how the Service is exposed. Defaults to ClusterIP. Valid
options are ExternalName, ClusterIP, ,NodePort and LoadBalancer.
"ExternalName" maps to the specified externalName. "ClusterIP" allocates a
cluster-internal IP address for load-balancing to endpoints. Endpoints are
determined by the selector or if that is not specified, by manual
construction of an Endpoints object. If clusterIP is "None", no virtual IP
is allocated and the endpoints are published as a set of endpoints rather
than a stable IP. "NodePort" builds on ClusterIP and allocates a port on
every node which routes to the clusterIP. "LoadBalancer" builds on NodePort
and creates an external load-balancer (if supported in the current cloud)
which routes to the clusterIP. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types

  ports 字段的定义

[root@master ~]# kubectl explain svc.spec.ports
KIND: Service
VERSION: v1 RESOURCE: ports <[]Object> DESCRIPTION:
The list of ports that are exposed by this service. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies ServicePort contains information on service's port. FIELDS:
name <string> 名字
The name of this port within the service. This must be a DNS_LABEL. All
ports within a ServiceSpec must have unique names. This maps to the 'Name'
field in EndpointPort objects. Optional if only one ServicePort is defined
on this service. nodePort <integer> 节点的端口
The port on each node on which this service is exposed when type=NodePort
or LoadBalancer. Usually assigned by the system. If specified, it will be
allocated to the service if unused or else creation of the service will
fail. Default is to auto-allocate a port if the ServiceType of this Service
requires one. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport port <integer> -required- 对外部提供访问服务的端口
The port that will be exposed by this service. protocol <string> 协议
The IP protocol for this port. Supports "TCP", "UDP", and "SCTP". Default
is TCP. targetPort <string> 容器的端口
Number or name of the port to access on the pods targeted by the service.
Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If
this is a string, it will be looked up as a named port in the target Pod's
container ports. If this is not specified, the value of the 'port' field is
used (an identity map). This field is ignored for services with
clusterIP=None, and should be omitted or set equal to the 'port' field.
More info:
https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service

  service 的yml 文件定义

[root@master manifests]# cat redis-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: redis-svc
namespace: default
spec:
selector:
app: redis 绑定的pod资源根据标签绑定的
clusterIP: 10.97.97.97 提供集群内访问地址
type: ClusterIP 类型
ports:
- port: 6379 service 对外提供的服务端口
targetPort: 6379 容器里暴露的端口

  创建这个service服务

[root@master manifests]# kubectl apply -f redis-svc.yaml
service/redis-svc created

  查看这个service的运行状态

[root@master manifests]# kubectl describe svc redis-svc
Name: redis-svc
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"redis-svc","namespace":"default"},"spec":{"clusterIP":"10.97.97.9...
Selector: app=redis
Type: ClusterIP
IP: 10.97.97.97
Port: <unset> 6379/TCP
TargetPort: 6379/TCP
Endpoints: 10.244.1.67:6379
Session Affinity: None
Events: <none>

  DNS资源记录格式

SVC_NAME.NS_NAME.DOMAIN.LTD
服务名.名称空间.资源域名后缀
默认资源后缀:svc.cluster.local

  定一个节点级别的服务

[root@master manifests]# cat myapp-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
selector:
cx: yl 绑定的pod
clusterIP: 10.99.99.99 集群内部的地址
type: NodePort
ports:
- port: 80 集群内部访问端口
targetPort: 80 容器里暴露的端口
nodePort: 30080 外部访问端口

  查看各个节点的端口

[root@master ~]# ss -lntp | grep 30080  主节点
LISTEN 0 128 :::30080 :::* users:(("kube-proxy",pid=15320,fd=11))
[root@node01 ~]# ss -lntp | grep 30080 node01节点
LISTEN 0 128 :::30080 :::* users:(("kube-proxy",pid=10352,fd=11))
[root@node02 ~]# ss -lntp | grep 30080 node02 节点
LISTEN 0 128 :::30080 :::* users:(("kube-proxy",pid=10422,fd=11))

  通过打补丁方式修改回话粘性

[root@master manifests]# kubectl patch svc myapp -p '{"spec":{"sessionAffinity":"ClientIP"}}'
service/myapp patched
[root@master manifests]# kubectl describe svc myapp
Name: myapp
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"myapp","namespace":"default"},"spec":{"clusterIP":"10.99.99.99","...
Selector: cx=yl
Type: NodePort
IP: 10.99.99.99
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 30080/TCP
Endpoints: 10.244.1.60:80,10.244.1.61:80,10.244.1.64:80 + 2 more...
Session Affinity: ClientIP
External Traffic Policy: Cluster
Events: <none>

  

Kubernetes的service资源介绍的更多相关文章

  1. k8s 之service资源介绍(三)

    kubernetes service资源 apiVersion: v1 kind: Service metadata: name: kubia spec: ports: - port: 80 targ ...

  2. kubernetes的Ingress资源介绍

    Ingress 的资源帮助信息介绍 [root@master ~]# kubectl explain ingress KIND: Ingress VERSION: extensions/v1beta1 ...

  3. kubernetes系列08—service资源详解

    本文收录在容器技术学习系列文章总目录 1.认识service 1.1 为什么要使用service Kubernetes Pod 是有生命周期的,它们可以被创建,也可以被销毁,然而一旦被销毁生命就永远结 ...

  4. (七)Kubernetes Service资源

    Service概述 为什么要使用Service Kubernetes Pod是平凡的,由Deployment等控制器管理的Pod对象都是有生命周期的,它们会被创建,也会意外挂掉.虽然它们可以由控制器自 ...

  5. Kubernetes 学习10 Service资源

    一.Service对应组件关系 1.在kubernetes平台之上,pod是有生命周期的,所以为了能够给对应的客户端提供一个固定的访问端点,因此我们在客户端和服务Pod之间添加一个固定的中间层,这个中 ...

  6. kubernetes进阶(04)kubernetes的service

    一.service概念 Service是对一组提供相同功能的Pods的抽象,并为它们提供一个统一的入口.借助Service,应用可以方便的实现服务发现与负载均衡,并实现应用的零宕机升级.Service ...

  7. 【k8s】kubernetes(k8s)介绍

    转自 http://blog.csdn.net/Real_Myth/article/details/78719244 一.Kubernetes系列之介绍篇   •Kubernetes介绍 1.背景介绍 ...

  8. Puppet自动化运维-资源介绍篇(4)

    1.什么是资源? 资源是Puppet最基础的元素,每个资源的定义都具有标题,类型,以及一系列的属性. 资源定义有如下的特性:   (1) Puppet使用title在编译时区分每个资源,使用命名变量在 ...

  9. puppet(2)-资源介绍

    puppet- 资源介绍: 类型.属性与状态同实现方式分离.仅指定目标状态 type {'title': attribute => value, ... } 查看支持的资源类型: puppet ...

随机推荐

  1. optm.adam

    optm.adam 待办 https://www.cnblogs.com/dylancao/p/9878978.html 这个优化包 理解求导过程,来理解神经网络.

  2. bzoj4199: [Noi2015]品酒大会 (并查集 && 后缀数组)

    据说用后缀自动机 + dp也能做 然而并不会 后缀数组的做法呢 就是先建个后缀数组,求出height值,此时如果直接找,复杂度是n ^ 2的,肯定会超时. 但是height大的值是不会对小的产生影响的 ...

  3. BZOJ1034[ZJOI2008]泡泡堂

    一开始是不会的,不知道如何处理相等的情况,瞎贪心一直WA. 于是就递归处理是让相等的平局还是输掉,如下,拿到了50分. int solve(int *a,int *b,int i,int l,int ...

  4. MySQL学习(六)change-buffer

    文章部分总结描述来自参考文章,属于半原创. 概述     文章将会介绍 change buffer 相关的知识点 查看 MySQL InnoDB 状态的命令 SHOW ENGINE INNODB ST ...

  5. AWD - IDE For Web dev汉化版

    一款安卓上的HTML网页编辑软件,不错哦 下载链接 http://t.cn/AiRIvtoL

  6. Linux gd库安装步骤说明

    gd 库是 PHP 处理图形的扩展库,它提供了一系列用来处理图片的 API(应用程序编程接口),使用 gd 库可以处理图片或者生成图片.在网站上,gd 库通常用来生成缩略图,或者对图片加水印,或者生成 ...

  7. java基础(七)之子类实例化

    知识点;1.生成子类的过程2.使用super调用父类构造函数的方法 首先编写3个文件. Person.java class Person{ String name; int age; Person() ...

  8. 剑指Offer:面试题20:表示数值的字符串

    记录一下书上的写法.很整洁,每个函数的功能都显而易见.自己开始写的一堆if else语句像是一坨屎.另外注释的地方短路效应也要注意一下.总之这题还挺考察代码素质的(我这种就不存在什么素质..乱糟糟一团 ...

  9. Blocked Billboard II

    前言 今天比赛真的状态不好(腐了一小会),导致差点爆0. 这个题解真的是在非常非常专注下写出来的,要不然真的心态崩. 刚换了域名,发现了美化脚本的bug,有点担心(汗-_-||). 题目 题目描述 奶 ...

  10. GitHub网页版基本操作

    创建存储库 登录GitHub进入主页,点击头像左边的加号,创建存储库 填写存储库名称.描述,根据需求设置其他选项.点击“Create repository”按钮 创建分支 打开之前创建好的存储库,点击 ...