Deploy Docker container to Kubernetes using yaml file

5/10/2019

I'm trying to deploy a docker container to Kubernetes using YAML file (local system).

Part 1: Docker container (Working fine)

  • I am able to run the container and push into docker hub
  • Able to access container application over a browser.

Part 2: Kubernetes (Not working)

  • Wrote deployment.yaml file that includes both deployment and service
  • Deployment: It is creating pod successfully
  • Service: Added service and trying to access the application over a browser but it is not working

    Error: Site can't be reached

Docker:

  • Container pushed into docker hub
    • name: $ID/tomcat-jenkins
    • version: 3

Kubernetes:

  • deployment.yaml
    apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
    kind: Deployment
    metadata:
      name: jenkins
    spec:
      selector:
        matchLabels:
          app: tomcat
      replicas: 2 # tells deployment to run 2 pods matching the template
      template:
        metadata:
          labels:
            app: tomcat
        spec:
          containers:
          - name: tomcat-jenkins
            image: $DOCKER-ID/tomcat-jenkins:3
            ports:
            - containerPort: 80
    # Service
    apiVersion: v1
    kind: Service
    metadata:
      name: jenkins
    spec:
      selector:
        app: tomcat
      ports:
      - protocol: TCP
        port: 80
        targetPort: 80
        nodePort: 31011
      type: NodePort

I am not able to access the application after deploying the container in Kubenetes pod.

I'm not sure whether my deployment.yaml file contains some syntax errors.

-- Vimal Soni
docker
kubernetes

1 Answer

5/10/2019

As you are exposing the service on NodePort , then you can access it with the http://NodeIP:NodePort` . the value of nodeport is 31011 for the above service.

Here is the a detail tutorail which explain exposing the service on k8s.

ClusterIP (default) - Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.

NodePort - Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using :. Superset of ClusterIP.

LoadBalancer - Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort

-- Suresh Vishnoi
Source: StackOverflow