Kubernetes--Service实践

Service作用和原理

Kubernetes--Service实践_第1张图片

作用:Service的作用主要是对外进行服务暴露,对内提供服务发现
原理:Service通过iptables或者ipvs提供对外反问,对内服务发现通过coredns实现对内服务发现

Service的类型

  • Cluster
  • NodePort
  • LoadBalancer
  • ExternalName

Cluster类型
$ cat myapp-deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:v1
        ports:
        - containerPort: 80
        name: http
$ cat myapp-svc.yaml 
kind: Service
apiVersion: v1
metadata:
  name: myapp-svc
spec:
  selector:
    app: myapp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
$ kubectl apply -f myapp-deploy.yaml
deployment.apps/myapp-deploy created

$ kubectl apply -f myapp-svc.yaml
service/myapp-svc created

$ kubectl get deploy,svc,endpoints
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp-deploy   3/3     3            3           28s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1                443/TCP   5d21h
service/myapp-svc    ClusterIP   10.105.225.171           80/TCP    22s

NAME                   ENDPOINTS                                   AGE
endpoints/kubernetes   172.16.56.133:8443                          5d21h
endpoints/myapp-svc    172.17.0.6:80,172.17.0.7:80,172.17.0.8:80   22s

$ kubectl run cirros-$RANDOM --rm -ti --image=cirros -- sh

/ # curl http://10.105.225.171

Hello World!

Hostname: myapp-deploy-ccc8b4bb5-f829g
Visits: cannot connect to Redis, counter disabled/

Kubernetes--Service实践_第2张图片


NodePort类型
$ cat myapp-svc.yaml 
kind: Service
apiVersion: v1
metadata:
  name: myapp-svc
spec:
  type: NodePort
  selector:
    app: myapp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 32223
$ kubectl apply -f myapp-svc.yaml
service/myapp-svc configured

$ kubectl get svc               
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1                443/TCP        5d21h
myapp-svc    NodePort    10.105.225.171           80:32223/TCP   9m46s

$ kubectl get pod -o wide        
NAME                           READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
myapp-deploy-ccc8b4bb5-dn4lb   1/1     Running   0          10m   172.17.0.6   minikube              
myapp-deploy-ccc8b4bb5-ds542   1/1     Running   0          10m   172.17.0.7   minikube              
myapp-deploy-ccc8b4bb5-f829g   1/1     Running   0          10m   172.17.0.8   minikube              

Kubernetes--Service实践_第3张图片

如果你尝试刷新页面,会发现得到不同的主机名,这是正确的

Kubernetes--Service实践_第4张图片


ExternalName类型
$ cat external-redis-svc.yaml 
kind: Service
apiVersion: v1
metadata:
  name: external-redis-svc
  namespace: default
spec:
  type: ExternalName
  externalName: www.baidu.com
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 0
  selector: {}
$ kubectl create -f external-redis-svc.yaml
service/external-redis-svc created

$ kubectl get svc
NAME                 TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)        AGE
external-redis-svc   ExternalName              www.baidu.com   80/TCP         5s
kubernetes           ClusterIP      10.96.0.1                  443/TCP        5d21h
myapp-svc            NodePort       10.105.225.171             80:32223/TCP   17m

$ kubectl run -ti --rm busybox --image=busybox -- sh
/ # nslookup external-redis-svc.default.svc.cluster.local.
Server:        10.96.0.10
Address:    10.96.0.10:53

external-redis-svc.default.svc.cluster.local    canonical name = www.baidu.com
www.baidu.com    canonical name = www.a.shifen.com
Name:    www.a.shifen.com
Address: 163.177.151.109
Name:    www.a.shifen.com
Address: 163.177.151.110

*** Can't find external-redis-svc.default.svc.cluster.local.: No answer

/ # exit

Kubernetes--Service实践_第5张图片

 
 
 

  • 如果外部服务没有域名,为了提高服务的兼容性可以采用service+手动endponit,自定义一个域名
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
apiVersion: v1
kind: Endpoints
metadata:
  name: my-service
subsets:
  - addresses:
      - ip: 192.0.2.42
    ports:
      - port: 9376

【完结】

你可能感兴趣的:(kubernetes)