k8s 容器端口映射物理机 设置容器级别 hostPort 和 Pod级别 hostNetwork,或者通过映射Services的方式让外部访问 。即可以通过Nodeip+port的方式直接访问。
容器级别:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: '1'
labels:
app: redis1202
chart: redis-1.1.15
heritage: Tiller
release: redis-instance
name: redis1202
spec:
containers:
- name: redis1202
image: redis:1202
ports:
- containerPort: 6379
hostPort: 6381
name: redis
protocol: TCP
Pod级别(Pod级别的话,hostPort默认即为containerPort,即容器内端口直接映射到node端口,无需修改,即使在yaml里面添加了hostPort指定端口,指定的端口也必须跟containerPort一致!)
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: '1'
labels:
app: redis1202
chart: redis-1.1.15
heritage: Tiller
release: redis-instance
name: redis1202
spec:
hostNetwork: true
containers:
- name: redis1202
image: redis:1202
ports:
- containerPort: 6379
hostPort: 6381
name: redis
protocol: TCP
除此方法外,也可通过Service映射端口的方式,让外部访问,即NodePort的方式
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: redis1202
chart: redis-1.1.15
heritage: Tiller
release: redis1202-instance
name: redis1202
spec:
ports:
- port: 6379
protocol: TCP
targetPort: 6379
selector:
app: redis1202
type: NodePort
status:
loadBalancer: {}