How can I convert ClusterIp to NodePort in Kubernetes?

12/14/2021

I am trying to convert from ClusterIp to NodePort. But When I call via curl http://176.55.116.xxx:30100 . It creates the below error. How can I convert below from clusterIp to Nodeport version?

Unable o connect to remote server when using curl to call http://176.55.116.116:30100 below configuration is not working...

(1) ClusterIp version (deployment_service.yml) WORKING

<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-js -->
apiVersion: apps/v1
kind: DaemonSet
metadata:
  namespace: coturn
  name: coturn
  labels:
    app.kubernetes.io/name: coturn
    app.kubernetes.io/instance: coturn
    app.kubernetes.io/version: 0.0.1
spec:
  # replicas: 1
  selector:
    matchLabels:
          app.kubernetes.io/name: coturn
          app.kubernetes.io/instance: coturn
          app.kubernetes.io/version: 0.0.1
  template:
    metadata:
      labels:
            app.kubernetes.io/name: coturn
            app.kubernetes.io/instance: coturn
            app.kubernetes.io/version: 0.0.1
    spec:
      hostNetwork: true
      # securityContext:
      #   runAsUser: 1000
      #   runAsGroup: 1000
      containers:
        - name: coturn
          image: coturn/coturn
          imagePullPolicy: Always
          ports:
            # - name: turn-udp
            #   containerPort: 3000
            #   protocol: UDP
            - name: turn-port1
              containerPort: 3478
              hostPort: 3478
              protocol: UDP
            - name: turn-port2
              containerPort: 3478
              hostPort: 3478
              protocol: TCP
          # securityContext:
          #   capabilities:
          #     drop:
          #       - ALL
            # readOnlyRootFilesystem: true
            # allowPrivilegeEscalation: false
          args:
          #   - --stun-only
            - -v

--- 

apiVersion: v1
kind: Service
metadata:
  name: coturn
  namespace: coturn
  labels:
       app.kubernetes.io/name: coturn
       app.kubernetes.io/instance: coturn
       app.kubernetes.io/version: 0.0.1
spec:
  type: ClusterIP
  ports:
    # - port: 3000
    #   targetPort: 3000
    #   protocol: UDP
    #   name: turn-udp
    - port: 3478
      targetPort: 3478
      protocol: UDP
      name: turn-port1
    - port: 3478
      targetPort: 3478
      protocol: TCP
      name: turn-port2
    # - port: 3478
    #   targetPort: 3478
    #   protocol: TCP
    #   name: turn-port3
    # - port: 3479
    #   targetPort: 3479
    #   protocol: TCP
    #   name: turn-port4
    
  selector:
       app.kubernetes.io/name: coturn
       app.kubernetes.io/instance: coturn
       app.kubernetes.io/version: 0.0.1
<!-- end snippet -->

(2) NodePort version (deployment_service.yml) : (NOT WORKInG)

<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-js -->
apiVersion: apps/v1
kind: DaemonSet
metadata:
  namespace: coturn
  name: coturn
  labels:
    app.kubernetes.io/name: coturn
    app.kubernetes.io/instance: coturn
    app.kubernetes.io/version: 0.0.1
spec:
  # replicas: 1
  selector:
    matchLabels:
          app.kubernetes.io/name: coturn
          app.kubernetes.io/instance: coturn
          app.kubernetes.io/version: 0.0.1
  template:
    metadata:
      labels:
            app.kubernetes.io/name: coturn
            app.kubernetes.io/instance: coturn
            app.kubernetes.io/version: 0.0.1
    spec:
      hostNetwork: true
      # securityContext:
      #   runAsUser: 1000
      #   runAsGroup: 1000
      containers:
        - name: coturn
          image: coturn/coturn
          imagePullPolicy: Always
          ports:
            # - name: turn-udp
            #   containerPort: 3000
            #   protocol: UDP
            - name: turn-port1
              containerPort: 3478
              hostPort: 3478
              protocol: UDP
            - name: turn-port2
              containerPort: 3478
              hostPort: 3478
              protocol: TCP
          # securityContext:
          #   capabilities:
          #     drop:
          #       - ALL
            # readOnlyRootFilesystem: true
            # allowPrivilegeEscalation: false
          args:
          #   - --stun-only
            - -v

--- 

apiVersion: v1
kind: Service
metadata:
  name: coturn
  namespace: coturn
  labels:
       app.kubernetes.io/name: coturn
       app.kubernetes.io/instance: coturn
       app.kubernetes.io/version: 0.0.1
spec:
  type: NodePort
  ports:
    # - port: 3000
    #   targetPort: 3000
    #   protocol: UDP
    #   name: turn-udp
    - port: 3478
      targetPort: 3478
      nodePort: 30100
      protocol: UDP
      name: turn-port1
    - port: 3478
      targetPort: 3478
      nodePort: 30100
      protocol: TCP
      name: turn-port2
    selector:
      app: coturn
    # - port: 3478
    #   targetPort: 3478
    #   protocol: TCP
    #   name: turn-port3
    # - port: 3479
    #   targetPort: 3479
    #   protocol: TCP
    #   name: turn-port4
status:
  loadBalancer: {}
    
<!-- end snippet -->

Error:

yusuf@DESKTOP-QK5VI8R:~/turnserver$ kubectl apply -f deploy.yml
daemonset.apps/coturn created
error: error parsing deploy.yml: error converting YAML to JSON: yaml: line 27: did not find expected '-' indicator
-- ALEXALEXIYEV
kubernetes

1 Answer

12/14/2021

You have an error in the yaml syntax, the selector is under spec.ports, which cannot work.

This is the corrected file (to check problems, use YAML validators, there are plenty online which you can toy with):

apiVersion: apps/v1
kind: DaemonSet
metadata:
  namespace: coturn
  name: coturn
  labels:
    app.kubernetes.io/name: coturn
    app.kubernetes.io/instance: coturn
    app.kubernetes.io/version: 0.0.1
spec:
  # replicas: 1
  selector:
    matchLabels:
          app.kubernetes.io/name: coturn
          app.kubernetes.io/instance: coturn
          app.kubernetes.io/version: 0.0.1
  template:
    metadata:
      labels:
            app.kubernetes.io/name: coturn
            app.kubernetes.io/instance: coturn
            app.kubernetes.io/version: 0.0.1
    spec:
      hostNetwork: true
      # securityContext:
      #   runAsUser: 1000
      #   runAsGroup: 1000
      containers:
        - name: coturn
          image: coturn/coturn
          imagePullPolicy: Always
          ports:
            # - name: turn-udp
            #   containerPort: 3000
            #   protocol: UDP
            - name: turn-port1
              containerPort: 3478
              hostPort: 3478
              protocol: UDP
            - name: turn-port2
              containerPort: 3478
              hostPort: 3478
              protocol: TCP
          # securityContext:
          #   capabilities:
          #     drop:
          #       - ALL
            # readOnlyRootFilesystem: true
            # allowPrivilegeEscalation: false
          args:
          #   - --stun-only
            - -v
--- 
apiVersion: v1
kind: Service
metadata:
  name: coturn
  namespace: coturn
  labels:
    app.kubernetes.io/name: coturn
    app.kubernetes.io/instance: coturn
    app.kubernetes.io/version: 0.0.1
spec:
  type: NodePort
  ports:
    # - port: 3000
    #   targetPort: 3000
    #   protocol: UDP
    #   name: turn-udp
    - port: 3478
      targetPort: 3478
      nodePort: 30100
      protocol: UDP
      name: turn-port1
    - port: 3478
      targetPort: 3478
      nodePort: 30100
      protocol: TCP
      name: turn-port2
    # - port: 3478
    #   targetPort: 3478
    #   protocol: TCP
    #   name: turn-port3
    # - port: 3479
    #   targetPort: 3479
    #   protocol: TCP
    #   name: turn-port4
  selector:
    app: coturn
-- AndD
Source: StackOverflow