欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kubernetes service

程序员文章站 2024-03-11 11:15:43
...

service工作模式

  • userspace 1.1-
  • iptables 1.10-
  • ipvs 1.11+

service类型

  • ClusterIP
  • NodePort
  • LoadBalancer
  • ExternalName

资源记录

SVC_NAME.NS_NAME.DOMAIN.LTD.
svc.cluster.local.
redis.default.svc.cluster.local.

service yaml

redis-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: default
spec:
  selector:
    app: redis
    role: logstor
  clusterIP: 10.254.0.97
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379

myapp-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: canary
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080
while true;do curl http://192.168.0.13:30080/hostname.html;sleep 1;done
while true;do curl http://192.168.0.13:30080;sleep 1;done

会话保持

kubectl patch service myapp -p '{"spec":{"sessionAffinity":"ClientIP"}}'
kubectl patch service myapp -p '{"spec":{"sessionAffinity":"None"}}'

无头service myapp-svc-headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp-svc
  namespace: default
spec:
  selector:
    app: myapp
    release: canary
  clusterIP: None
  ports:
  - port: 80
    targetPort: 80
dig -t A myapp.default.svc.cluster.local. @10.254.0.10