9.5、端口配置(ports)
5.2.5 端口设置
本小节来介绍容器的端口设置,也就是 containers 的 ports 选项。
首先看下ports支持的子选项:
1 2 3 4 5 6 7 8 9 10
| [root@k8s-master01 ~] KIND: Pod VERSION: v1 RESOURCE: ports <[]Object> FIELDS: name <string> containerPort<integer> hostPort <integer> hostIP <string> protocol <string>
|
接下来,编写一个测试案例,创建 pod-ports.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: Pod metadata: name: pod-ports namespace: dev spec: containers: - name: nginx image: registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:nginx-latest ports: - name: nginx-port containerPort: 80 protocol: TCP
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| kubectl create -f pod-ports.yaml
kubectl get pod pod-ports -n dev -o yaml ...... spec: containers: - image: registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:nginx-latest imagePullPolicy: IfNotPresent name: nginx ports: - containerPort: 80 name: nginx-port protocol: TCP ......
|
访问容器中的程序需要使用的是Podip:containerPort