6.Kubernetes核心资源:Service网络资源
[toc]
Service网络服务
资源分类
- NodePort:
- 宿主机节点的端口
- ClusterIP:
- 用来动态发现和负载均衡POD的IP,通过 Label(标签) 绑定POD
- PodIP:
- 提供POD使用的IP
ClusterIP资源
1 | # 编写ClusterIP资源清单 |
NodePort资源
1 | # 编写NodePort资源清单 |
示例:使用k8s资源启动h5小游戏(ns:h5)
分开创建资源清单
需求
指定名称空间:h5
名称空间资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22# 编辑创建h5名称空间的资源清单
vim h5-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
name: h5
# 应用资源清单
kubectl apply -f h5-ns.yaml
namespace/h5 created
# 查看名称空间
kubectl get ns
NAME STATUS AGE
default Active 6d8h
h5 Active 5s
kube-flannel Active 6d7h
kube-node-lease Active 6d8h
kube-public Active 6d8h
kube-system Active 6d8h
nginx-ingress Active 7h40m
test-mysql Active 5d8hPOD资源:deployment控制器
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# 编辑资源清单
vim h5-dp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: h5-dp
namespace: h5
spec:
selector:
matchLabels:
app: h5
replicas: 5
template:
metadata:
labels:
app: h5
name: h5-pod
namespace: h5
spec:
volumes:
- name: h5-code
hostPath:
path: /code/h5
containers:
- name: h5-container
image: nginx:alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: h5-code
mountPath: /usr/share/nginx/html/
# 应用资源清单
kubectl apply -f h5-dp.yaml
# 查看pod启动状态
kubectl get pod -n h5
NAME READY STATUS RESTARTS AGE
h5-dp-59f7b9c546-8kb8b 1/1 Running 0 25s
h5-dp-59f7b9c546-m5g7w 1/1 Running 0 25s
h5-dp-59f7b9c546-p4nkz 1/1 Running 0 25s
h5-dp-59f7b9c546-pcx79 1/1 Running 0 25s
h5-dp-59f7b9c546-s5d74 1/1 Running 0 25sHPA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23# 编辑资源清单
vim h5-hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: h5-hpa
namespace: h5
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: h5-dp
targetCPUUtilizationPercentage: 50
# 应用资源清单
kubectl apply -f h5-hpa.yaml
# 查看HPA资源状态
kubectl get hpa -n h5
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
h5-hpa Deployment/h5-dp <unknown>/50% 1 10 5 40sClusterIP/NodePort
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# 编辑资源清单
vim h5-node.yaml
apiVersion: v1
kind: Service
metadata:
name: h5-nodeport
namespace: h5
spec:
selector:
app: h5
ports:
- name: h5
port: 80
protocol: TCP
targetPort: 80
nodePort: 32767
type: NodePort
# 应用资源清单
kubectl apply -f h5-node.yaml
# 查看service资源状态
kubectl get svc -n h5
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
h5-nodeport NodePort 10.1.168.14 <none> 80:32767/TCP 25s
# 查看service资源详细状态
kubectl describe svc h5-nodeport -n h5
Name: h5-nodeport
Namespace: h5
Labels: <none>
Annotations: <none>
Selector: app=h5
Type: NodePort
IP: 10.1.168.14
Port: h5 80/TCP
TargetPort: 80/TCP
NodePort: h5 32767/TCP
Endpoints: 10.2.1.31:80,10.2.1.32:80,10.2.1.33:80 + 2 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
优化:合并资源清单
1 | # 编辑资源清单 |
nodeport 缺点
- 没有ingress之前,pod对外提供服务只能通过NodeIP:NodePort的形式,但是这种形式有缺点,一个节点上的port不能重复利用。比如某个服务占用了80,那么其他服务就不能在用这个端口了。
- NodePort是4层代理,不能解析7层的http,不能通过域名区分流量
- 为了解决这个问题,我们需要用到资源控制器叫Ingress,作用就是提供一个统一的访问入口。工作在7层
- 虽然我们可以使用nginx/haproxy来实现类似的效果,但是传统部署不能动态的发现我们新创建的资源,必须手动修改配置文件并重启。
- 适用于k8s的ingress控制器主流的有nginx-ingress和traefik
ingress网络资源
使用ingress解决nodeport缺陷
ingress部署
1 | # 官方ingress |
1 | # 找到第五版,第四章 |
使用ingress启动h5网站
创建名称空间
1
2
3
4
5
6# 编辑资源清单
vim h5-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
name: h5启动pod和deploment控制器
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# 编辑资源清单
vim h5-dp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: h5-dp
namespace: h5
spec:
selector:
matchLabels:
app: h5
replicas: 5
template:
metadata:
labels:
app: h5
name: h5-pod
namespace: h5
spec:
volumes:
- name: h5-code
hostPath:
path: /code/h5
containers:
- name: h5-container
image: nginx:alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: h5-code
mountPath: /usr/share/nginx/html/service资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 编辑资源清单
vim h5-cluster.yaml
apiVersion: v1
kind: Service
metadata:
name: h5-cluster
namespace: h5
spec:
selector:
app: h5
ports:
- name: h5
port: 80
protocol: TCP
targetPort: 80
type: ClusterIP合并优化资源清单
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
51
52
53# 编辑资源清单
vim ingress-h5.yaml
---
apiVersion: v1
kind: Namespace
metadata:
name: h5
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: h5-dp
namespace: h5
spec:
selector:
matchLabels:
app: h5
replicas: 5
template:
metadata:
labels:
app: h5
name: h5-pod
namespace: h5
spec:
volumes:
- name: h5-code
hostPath:
path: /code/h5
containers:
- name: h5-container
image: nginx:alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: h5-code
mountPath: /usr/share/nginx/html/
---
apiVersion: v1
kind: Service
metadata:
name: h5-cluster
namespace: h5
spec:
selector:
app: h5
ports:
- name: h5
port: 80
protocol: TCP
targetPort: 80
type: ClusterIP应用资源清单
1
kubectl apply -f ingress-h5.yaml
查看状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19# 查看POD状态
kubectl get pod -n h5
NAME READY STATUS RESTARTS AGE
h5-dp-59f7b9c546-8kb8b 1/1 Running 0 74m
h5-dp-59f7b9c546-m5g7w 1/1 Running 0 74m
h5-dp-59f7b9c546-p4nkz 1/1 Running 0 74m
h5-dp-59f7b9c546-pcx79 1/1 Running 0 74m
h5-dp-59f7b9c546-s5d74 1/1 Running 0 74m
# 查看ns状态
kubectl get ns
NAME STATUS AGE
default Active 6d10h
h5 Active 80m
# 查看ClusterIP
kubectl get svc -n h5
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
h5-cluster ClusterIP 10.1.173.4 <none> 80/TCP 3m4s使用ingress关联ClusterIP
1 | # 使用ingress关联ClusterIP |
服务发现
概念
我们也可以使用DNS域名的形式访问Service,如果在同一个命名空间里甚至可以直接使用service(clusterIP的名字)名来访问服务。
但即便处于不同的名称空间都可以进行通讯
结构图示
使用语法
在不同的名称空间,进行通信的语法
1 | # 语法格式 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 奥利奥の麦旋风!