Linux学习-Kubernetes之Service

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-Kubernetes之Service相关的知识,希望对你有一定的参考价值。

Service的三种代理模式

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

Service类型

  • ExternalName
  • ClusterIP
  • NodePort
  • LoadBlance

资源记录

SVC_NAME.NS_NAME.DOMAIN.LTD.
默认:svc.cluster.local.

ClusterIP
#查看service定义
[root@master ~]# kubectl explain svc
#创建一个deployment类型的redis的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: logstor
  template:
    metadata:
      labels:
        app: redis
        role: logstor
    spec:
      containers:
        - name: redis
          image: redis:4.0-alpine
          ports:
          - name: redis
            containerPort: 6379
[root@master manifests]# kuberctl apply -f ds-demo.yaml

#为redis创建一个ClusterIP类型的service
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: default
spec:
  selector:
    app: redis
    role: logstor
  clusterIP: 10.97.97.97
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379
[root@master manifests]# kubectl apply -f redis-svc.yaml 
service/redis created
[root@master manifests]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        5d22h
nginx        NodePort    10.105.125.19   <none>        80:31526/TCP   4d23h
redis        ClusterIP   10.97.97.97     <none>        6379/TCP       14s
[root@master manifests]# kubectl describe service redis
Name:              redis
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=redis,role=logstor
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.97.97.97
IPs:               10.97.97.97
Port:              <unset>  6379/TCP
TargetPort:        6379/TCP
Endpoints:         10.244.1.35:6379
Session Affinity:  None
Events:            <none>

NodePort
#myapp的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      release: tye
  template:
    metadata:
      labels:
        app: myapp
        release: tye
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v4
        ports:
        - name: http
          containerPort: 80
#创建一个NodePort的Service
apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: tye
  clusterIP: 10.99.99.99
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080
[root@master manifests]# kubectl apply -f myapp-svc.yaml 
service/myapp created
#查看service
[root@master manifests]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        5d23h
myapp        NodePort    10.99.99.99     <none>        80:30080/TCP   41s
nginx        NodePort    10.105.125.19   <none>        80:31526/TCP   4d23h
redis        ClusterIP   10.97.97.97     <none>        6379/TCP       28m
[root@master manifests]# kubectl describe service myapp
Name:                     myapp
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=myapp,release=tye
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.99.99.99
IPs:                      10.99.99.99
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30080/TCP
Endpoints:                10.244.3.46:80,10.244.3.47:80,10.244.3.49:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
#找一台其它的虚拟机测试访问情况
[root@lotus ~]# while true; do curl http://192.168.88.101:30080/hostname.html;sleep 1; done
myapp-deploy-5cf7978485-jwvtl
myapp-deploy-5cf7978485-gsppv
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-gsppv
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-jwvtl

#修改sessionAffinity属性,添加客户端的session亲密性,同一客户端访问调度至同一个容器
[root@master manifests]# kubectl patch svc myapp  -p '{"spec":{"sessionAffinity":"ClientIP"}}'
service/myapp patched
[root@lotus ~]# while true; do curl http://192.168.88.101:30080/hostname.html; sleep 1;done
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
myapp-deploy-5cf7978485-b4sdw
Headless Service无头服务
apiVersion: v1
kind: Service
metadata:
  name: myapp-headless
  namespace: default
spec:
  selector:
    app: myapp
    release: tye
  clusterIP: None
  ports:
  - port: 80
    targetPort: 80

[root@master manifests]# kubectl apply -f myapp-svc-headless.yaml 
service/myapp-headless created
[root@master manifests]# kubectl get svc
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        5d23h
myapp            NodePort    10.99.99.99     <none>        80:30080/TCP   31m
myapp-headless   ClusterIP   None            <none>        80/TCP         4s
nginx            NodePort    10.105.125.19   <none>        80:31526/TCP   5d
redis            ClusterIP   10.97.97.97     <none>        6379/TCP       59m
#查看service解析情况
[root@master manifests]# dig -t A myapp-headless.default.svc.cluster.local. @10.96.0.10

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.7 <<>> -t A myapp-headless.default.svc.cluster.local. @10.96.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1280
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;myapp-headless.default.svc.cluster.local. IN A

;; ANSWER SECTION:
myapp-headless.default.svc.cluster.local. 30 IN	A 10.244.3.47
myapp-headless.default.svc.cluster.local. 30 IN	A 10.244.3.49
myapp-headless.default.svc.cluster.local. 30 IN	A 10.244.3.46

;; Query time: 0 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Fri Nov 12 09:58:24 EST 2021
;; MSG SIZE  rcvd: 237

[root@master manifests]# kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS      AGE     IP            NODE    NOMINATED NODE   READINESS GATES
myapp-deploy-5cf7978485-b4sdw   1/1     Running   1 (87m ago)   23h     10.244.3.47   node1   <none>           <none>
myapp-deploy-5cf7978485-gsppv   1/1     Running   1 (87m ago)   23h     10.244.3.46   node1   <none>           <none>
myapp-deploy-5cf7978485-jwvtl   1/1     Running   1 (87m ago)   23h     10.244.3.49   node1   <none>           <none>
myapp-ds-9lw8z                  1/1     Running   1 (87m ago)   23h     10.244.1.36   node2   <none>           <none>
myapp-ds-n54vw                  1/1     Running   1 (87m ago)   23h     10.244.3.48   node1   <none>           <none>
nginx                           1/1     Running   6 (87m ago)   5d14h   10.244.1.37   node2   <none>           <none>
redis-56fd57fd76-lcndb          1/1     Running   1 (87m ago)   23h     10.244.1.35   node2   <none>           <none>

以上是关于Linux学习-Kubernetes之Service的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习-Kubernetes之Secret和ConfigMap

Linux学习-Kubernetes之Secret和ConfigMap

Linux企业运维——Kubernetes存储之Configmap配置管理

Linux企业运维——Kubernetes存储之Secret配置管理

Linux企业运维——Kubernetes存储之Volumes配置管理

云原生技术之kubernetes学习笔记