2. Services 与 Load Balancing

2.1 Services

2.1.1 Services常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 查看Services
kubectl get services

# 创建ClusterIP
kubectl expose deployment <NAME> --type=ClusterIP --target-port=<PORT> --port=<PORT>

# 创建NodePort
kubectl expose deployment <NAME> --type=NodePort --target-port=<PORT> --port=<PORT>

# 创建ExternalName
kubectl create service externalname <NAME> --external-name=www.baidu.com

# 创建Headless Service
kubectl expose deployment <NAME> --type=ClusterIP --cluster-ip=None

# 创建ExternalIP
kubectl expose deployment <NAME> --type=ClusterIP --external-ip=<IP_ADDRESS>

# 删除Service
kubectl delete services <NAME>

2.1.2 ClusterIP类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ClusterIP用户实现Kubernetes集群中内部服务的访问

# 编写Yaml
apiVersion: v1
kind: Service
metadata:
name: clusterip-service
spec:
type: ClusterIP
selector:
app: pod-nginx
ports:
- port: 80
targetPort: 80

2.1.3 NodePort类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
NodePort用户实现Kubernetes集群内部服务的对外暴露

# 编写Yaml
apiVersion: v1
kind: Service
metadata:
name: clusterip-service
spec:
type: NodePort
selector:
app: pod-nginx
ports:
- port: 80
targetPort: 80
# nodePort: 30696

2.1.4 ExternalName类型

1
2
3
4
5
6
7
8
9
10
ExternalName用户实现Kubernetes集群中的应用可以通过Service访问集群外地的域名服务

# 编写yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ExternalName
externalName: www.baidu.com

2.1.5 ExternalIP方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ExternalIP用户于实现Kubernetes集群内部服务的对外暴露

# 编写Yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ClusterIP
selector:
app: pod-nginx
ports:
- name: http
protocol: TCP
port: 18080
targetPort: 80
externalIPs:
- 172.17.210.201
- 172.17.210.202

2.1.6 Headless Services

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
Headless Service用于满足不需要负载均衡或者Service IP的服务

# 编写yaml
apiVersion: v1
kind: Service
metadata:
name: headless-service
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: statefulset-nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: statefulset-nginx
serviceName: "headless-service"
replicas: 3
template:
metadata:
labels:
app: statefulset-nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent

2.1.7 External_IP_Port方式

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
External_IP_Port用于实现Kubernetes集群中的应用可以通过Service访问集群外部的IP:Port服务

# 编写Service Yaml文件
apiVersion: v1
kind: Service
metadata:
name: external-ip-port
spec:
clusterIP: None
type: ClusterIP
ports:
- port: 3306
targetPort: 3306

# 编写Endpoints Yaml文件
apiVersion: v1
kind: Endpoints
metadata:
name: external-ip-port
subsets:
- addresses:
- ip: 192.168.1.1
ports:
- port: 3306
protocol: TCP

2.1.8 会话保持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 编写Yaml文件
apiVersion: v1
kind: Service
metadata:
name: clusterip-service
spec:
sessionAffinity: ClientIP
clientIP:
timeoutSeconds: 10800
type: ClusterIP
selector:
app: pod-nginx
ports:
- port: 80
targetPort: 80

2.2 Ingress

2.2.1 部署Ingress Nginx Controllers

1
2
3
4
5
访问https://github.com/kubernetes/ingress-nginx/tree/master/deploy/static/provider/baremetal

# 下载deploy.yaml,并做如下修改:
注释创建services(273 - 300行)
在Deployment中添加hostNetwork: true(在329行下面添加)

2.2.2 创建Ingress

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
49
50
# 练习一
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: test-ingress
spec:
backend:
serviceName: test01
servicePort: 80

# 练习二
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: name-virtual-host-ingress
spec:
rules:
- host: foo.bar.com
http:
paths:
- backend:
serviceName: test01
servicePort: 80
- host: bar.foo.com
http:
paths:
- backend:
serviceName: test02
servicePort: 80

# 练习三
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: simple-fanout-example
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: foo.bar.com
http:
paths:
- path: /foo
backend:
serviceName: test01
servicePort: 80
- path: /bar
backend:
serviceName: test02
servicePort: 80