Does we need to repeat port in yaml file in kubernetes?

6/26/2018

Currently i writing a yaml file for kubernetes and i've some question on my mind about the best practices. Like, do i've to declare in each {Service, Deployment Pod} the ports i want to open ?

i didn't found any advice for that :(

Do you see anything, than i can write to optimize my yaml file? thank in advance for your help and your advice.

apiVersion: v1
kind: Service
metadata:
  name: karaf
  namespace: poc
spec:
  type: NodePort
  selector:
    app: karaf
  ports:
  - name: port6443
    port: 6443
    targetPort: 6443
    nodePort: 30105
  - name: port5000
    port: 6100
    targetPort: 6100
    nodePort: 30100
  - name: port5001
    port: 6101
    targetPort: 6101
    nodePort: 30101  


---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: karaf
  namespace: poc
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: karaf
    spec:
      containers:
      - name: karaf
        image: "xxxxxx/karaf:ids-1.1.0"
        imagePullPolicy: Always
        ports:
        - containerPort: 6443
          protocol: TCP
        - containerPort: 6100
          protocol: TCP
        - containerPort: 6101
          protocol: TCP


---
apiVersion: v1
kind: Pod
metadata:
  name: karaf
  labels:
    app: karaf
spec:
  containers:
    - name: karaf 
      image: "xxxxxxxxxx/karaf:ids-1.1.0"
      ports:
      - containerPort: 6443
      ports:
      - containerPort: 6100
      ports:
      - containerPort: 6101
-- morla
kubectl
kubernetes
minikube

1 Answer

6/26/2018

do not repeat ports: key in deployment/pod multiple times your pod inherits from Deployment that started it, so I assume you placed it here just for reference, cause it makes no sense to create it manually), it's a list so just

port:
  - containerPort: 6443
  - containerPort: 6100
  - containerPort: 6101

also, you don't really have to define ports here, although it helps to make clear what is running where. If you don't, your software will still start on ports it's using, and services will successfully direct traffic to them.

-- Radek 'Goblin' Pieczonka
Source: StackOverflow