Contents

k8s sevices

Understanding kubernetes networking: pods

Kubernetes Services simply visually explained

Kubernetes Ingress simply visually explained

Demystifying Kubernetes service discovery

浅谈Kubernetes Service负载均衡实现机制

Kubernetes| 找到容器不容易:Service、DNS 与服务发现

  1. create servie and servie types;
  2. service implement overivew
  3. discuss service and k8s
  4. ingress

what: 提供稳定的ip and dns name 以方便访问容器

types:

  1. cluster ip
  2. node port
  3. 外部:
    1. proxy
    2. load balancer
    3. ingress

types

cluster ip: internal ip + port

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: v1  
kind: Service  
metadata:  
	name: service-python  
spec:  
ports:  
	- port: 3000  
	  protocol: TCP  
	  targetPort: 443  
	selector:  
		run: pod-python  
	type: ClusterIP

https://cdn.jsdelivr.net/gh/atony2099/imgs@master/20210716/dEKWRO.jpg

nodePort: cluster ip + node ip, port

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: v1  
kind: Service  
metadata:  
  name: service-python  
spec:  
  ports:  
  - port: 3000  
    protocol: TCP  
    targetPort: 443  
    nodePort: 30080
  selector:  
    run: pod-python  
	type: NodePort

https://cdn.jsdelivr.net/gh/atony2099/imgs@master/20210716/6ZSnjC.jpg

load balancer: nodePort + external ip https://cdn.jsdelivr.net/gh/atony2099/imgs@master/20210716/0YbEMG.jpg ![[Pasted image 20230210215248.png]]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: v1  
kind: Service  
metadata:  
  name: service-python  
spec:  
  ports:  
  - port: 3000  
    protocol: TCP  
    targetPort: 443  
    nodePort: 30080  
  selector:  
    run: pod-python  
	type: LoadBalancer**

ingress controller

what:

a controller to implement gateway for k8s

types:

  1. nginx
  2. kong
  3. haproxy

depoy a nginx ingress controller example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-ingress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-ingress
  template:
    metadata:
      labels:
        app: nginx-ingress
    spec:
      serviceAccountName: nginx-ingress-serviceaccount
      containers:
        - name: nginx-ingress-controller
          image: mr.io/kubernetes-ingress-controller/nginx-ingress-controller:0.24.1
          args:
            - /nginx-ingress-controller
            - --configmap=$(POD_NAMESPACE)/nginx-configuration
            - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - name: http
              containerPort: 80
          livenessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            timeoutSeconds: 10
          readinessProbe:
            httpGet:
              path: /healthz
              port: 10254
              scheme: HTTP
            initialDelaySeconds: 10
            timeoutSeconds: 10

configure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: networking.k8s.io/v1beta1  
kind: Ingress  
metadata:  
  name: simple-fanout-example  
spec:  
  rules:  
  - host: api.myurl.com 
    http:  
      paths:  
      - path: /foo 
        backend:  
          serviceName: service1  
          servicePort: 4200  
      - path: /bar  
        backend:  
          serviceName: service2  
          servicePort: 8080  
  - host: website.myurl.com
    http:  
      paths:  
      - path: /
        backend:  
          serviceName: service3  
          servicePort: 3333

how

https://raw.githubusercontent.com/atony2099/imgs/master/uPic/OICxi4.jpg

https://raw.githubusercontent.com/atony2099/imgs/master/uPic/itoZdC.jpg

通过 kube-proxy实现路由; 通过 kube-dns 实现dns 解析: service name->cluster ip

https://cdn.jsdelivr.net/gh/atony2099/imgs@master/20211206/y8vLib.jpg https://cdn.jsdelivr.net/gh/atony2099/imgs@master/20211206/8boFTl.jpg

  1. kube-proxy 监听service,endpoint(pod);

  2. set iptables or ipvs 实现服务发现 clusterIP-> iptables(ipvs)-> podIP

  3. dns-> clusterip