Kubenete study notes (Service)

Service:

Ways to create service:
Kubectl expose created a Service resource with the same pod selector as the one
used by the ReplicationController
Kubectl create with service specs

 apiVersion: v1
 kind: Service
 metadata:
   name: kubia
 spec:
   ports:
     - port: 80
       targetPort: 8080
   selector:
     app: kubia 

kubectl exec [pod name] -- [command name] to execute command within pod
Kubenete service only supports 2 types of session affinity (none/client ip), since it deals with TCP/UDP packet, it does not know cookie

Discover service:

  • Use kubectl exec [pod name] env to find out service host/port: KUBERNETES_SERVICE_HOST KUBERNETES_SERVICE_PORT
  • Use kubenete’s own DNS server in pod kube-dns in kube-system namespace
  • Use FQDN: [pod name].[namespace].[configurable cluster domain suffix] or [pod name].[namespace] or [pod name] to access service in pod
  • Ping does not work within pod, since cluster ip is a virtual one

Service endpoint:

  • Service link to pod via endpoints.
  • kubectl get endpoints [pod name] to get endpoints for pod
  • the pod selector is used to build a list of IPs and ports, which is then stored in the Endpoints resource
  • Endpoint will not be auto-created for services without selector
  • Endpoint can be created for external servers by manual creation: exposing either ip address or host name
 apiVersion: v1
 kind: Endpoints
 metadata:
   name: external-service
 subsets:
   - addresses:
       - ip: 11.11.11.11
       - ip: 22.22.22.22
     ports:
       - port: 80
 apiVersion: v1
 kind: Service
 metadata:
    name: external-service
 spec:
    type: ExternalName
    externalName: someapi.somecompany.com
    ports:
       - port: 80

Exposing service to external client:
1.Create a nodePort service External-IP = nodes indicates that service is accessible through the IP address of any cluster node [node ip]:[node port] or [cluster ip]:[port]

 spec:
   type: NodePort
   ports:
     - port: 80
       targetPort: 8080
       nodePort: 30123 

Need to open firewall to access node
Client’s IP is not visible to the pod
Find out node’s external ip: kubectl get nodes -o jsonpath=‘{.items[].status.addresses[?(@.type=="ExternalIP")].address}‘

Use spec: externalTrafficPolicy: local to instruct kubenete use to use pod on node receives the request, preventing extra hop but may not load balance evenly.

2.Create a loadBalancer service External-IP = fixed. Node port can be assigned automatically

spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080 

No need to open firewall

3.Create an ingress service A single ingress service can be used for multiple pods. Host path of the request determines which service the request is forwarded to. The Ingress controller didn’t forward the request to the service. It only used it to select a pod.

 apiVersion: extensions/v1beta1
 kind: Ingress
 metadata:
   name: kubia
 spec:
   rules:
     - host: kubia.example.com
   http:
     paths:
       - path: /
     backend:
       serviceName: kubia-nodeport
       servicePort: 80 

Readiness probe:

  • Invoked periodically to check whether pod is ready.
  • Unlike liveness probe, pod failing readiness probe check is not killed/restarted, instead it is removed from the service. After ready, it is added back
  • Readiness can be viewed via “get pods” ‘s ready column
  • Always define a readiness probe to avoid pod becoming ready too soon when starting up
  • No need to include shutdown handling logic in readiness probe

Headless service:

  • Setting the cluster ip of service to none creates a headless service
  • DNS Lookup does not return cluster ip of service but each pod’s ip
    Performing DNS lookup in kubenetes: kubectl run dnsutils --image=tutum/dnsutils --generator=run-pod/v1 --command -- sleep infinity
    kubectl exec dnsutils nslookup service name
  • A headless services still provides load balancing across pods, but through the DNS round-robin mechanism instead of through the service proxy.
  • Use service specs: publishNotReadyAddresses field to return pod’s IP even if it is not ready.

Troubleshot service:

  • Make sure you’re connecting to the service’s cluster IP from within the cluster, not from the outside.
  • Don’t bother pinging the service IP to figure out if the service is accessible (remember, the service’s cluster IP is a virtual IP and pinging it will never work).
  • If you’ve defined a readiness probe, make sure it’s succeeding; otherwise the pod won’t be part of the service.
  • To confirm that a pod is part of the service, examine the corresponding endpoints object with kubectl get endpoints.
  • If you’re trying to access the service through its FQDN or a part of it (for example, myservice.mynamespace.svc.cluster.local or myservice.mynamespace) and it doesn’t work, see if you can access it using its cluster IP instead of the FQDN.
  • Check whether you’re connecting to the port exposed by the service and not the target port.
  • Try connecting to the pod IP directly to confirm your pod is accepting connections on the correct port.
  • If you can’t even access your app through the pod’s IP, make sure your app isn’t only binding to localhost.

原文地址:https://blog.51cto.com/shadowisper/2476302

时间: 2024-10-17 05:04:21

Kubenete study notes (Service)的相关文章

Kubenete study notes (POD)

Pod Definition: Create pod by definition: kubectl create -f [filename]Display pod definition: kubectl get po [pod name] -o yaml/jsonGetting log: kubectl logs [pod name] -c [container name] Port forwarding: kubectl port-forward [pod name] [localport]:

Kubenete study notes (Replication Controller)

Replication controller: ReplicationController schedules pod to one work node, kubelet application in node pulls image and create containers.Pod started by "kubectl run" is not created directly. ReplicationController is created by command and rep

My study notes —— 初始24种设计模式

摘要 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于己于他人于系统都是多赢的:设计模式使代码编制真正工程化:设计模式是软件工程的基石脉络,如同大厦的结构一样. 目录 设计概念 设计原则 四要素 基本模式 创建型 结构型 行为型 正文 设计概念 Change:让代码富有弹性,能够应对需求的变化: DRY:消除冗余,让代码更精炼: KISS:让代码

[2016-03-16]How can I take better study notes?

A:Dad,how can I take better study notes? B:Okay,look.Divide your pate into two columns. A:Like this? B:No.the one on the right should be larger. A:Let me do it agian.Is this better? B:Yes.Write down all the ideas on the right side of the page. A:What

Machine Learning Algorithms Study Notes(3)--Learning Theory

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 229 的学习笔记. Machine Learning Algorithms Study Notes 系列文章介绍 3 Learning Theory 3.1 Regularization and model selection 模型选择问题:对于一个学习问题,可以有多种模型选择.比如要拟合一组样本点,

Machine Learning Algorithms Study Notes(2)--Supervised Learning

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 2    Supervised Learning    3 2.1    Perceptron Learning Algorithm (PLA)    3 2.1.1    PLA -- "知错能改"演算法    4 2.2    Linear Regression    6 2.2.1    线性回归模型    6 2.2.2    最小二乘法( le

Machine Learning Algorithms Study Notes(1)--Introduction

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 目 录 1    Introduction    1 1.1    What is Machine Learning    1 1.2    学习心得和笔记的框架    1 2    Supervised Learning    3 2.1    Perceptron Learning Algorithm (PLA)    3 2.1.1    PLA -- "知

ORACLE STUDY NOTES 04

? [JSU]LJDragon's Oracle course notes In the first semester, junior year ? PL/SQL游标(declare .. begin .. end) --游标:指向查询结果集的指针,指针指向哪一行,提取的就是哪一行的数据 --PLSQL的游标默认指向结果集的第1行 --显示游标的四大步骤: 1.定义游标 cursor 游标变量名 is 查询语句; 2.打开游标 open 游标变量名;(可以重复打开) 3.提取游标 fetch 游

study notes: high performance linux server programming

1:linux网络API分为:socker地址API,socker基础API,网络信息API 1,socker地址API:包含IP地址和端口(ip, port).表示TCP通信的一端. 2,socker基础API:创建/命名/监听socker,接收/发起链接,读写数据,获取地址信息,检测带外标记和读取/设置socker选项.sys/socket.h 3,网络信息API:主机名和IP地址的转换,服务名和端口号的转换.netdb.h 2:socket和API的函数 和 相关知识. 1,函数. 1 I