# 1. 命令模式
# 1.1 使用deployment部署
kubectl create deployment web --image=nginx --replicas=3
kubectl get deploy,pods
1
2
2
# 1.2 使用Service将Pod暴露出去
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort
kubectl get service
1
2
2
# 1.3 浏览器访问
http://NodeIP:Port # 端口随机生成,通过get svc获取
1
# 2. yaml编排模式
web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web
type: 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
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
名称 | 说明 |
---|---|
apiVersion | api版本 |
kind | 资源类型,Deployment,Service等 |
metadata | 资源元数据 |
spec | 资源规格 |
replicas | 副本(实例)数量 |
selector | 标签选择器,与下面metadata.labels保持一致 |
template | pod模版 |
metadata | pod元数据 |
spec | pod规格 |
containers | 容器配置 |
port | service端口,通过ClusterIP访问用 |
targetPort | 镜像内服务端口,例如nginx镜像是80 |
等同于 kubectl create deployment web --image=nginx --replicas=3
和 kubectl expose deployment web --port=80 --target-port=80 --type=NodePort
组合。
kubectl apply -f web.yaml
1
通过 nginx-ingress
kubectl create ingress demo-localhost --class=nginx \
--rule=demo.localdev.me/*=web:80
kubectl port-forward service/web 8080:80
1
2
3
4
2
3
4
这样通过访问 http://demo.localdev.me:8080/ 就可以看到页面了。