How to deploy docker image on container through kubernetes YAML file?

11/25/2019

I am new to Kubernetes and trying my first deployment. Main Content of Docker file:

..........
..........
COPY . $PROJECT_HOME
WORKDIR $PROJECT_HOME
EXPOSE 9000
CMD ["sbt"]

I can successfully build the container image using the following command docker build -t fitmeold:0.3 .

To deploy the image on a container using docker I used the following command:

docker run  -it -p 80:9000 fitmeold:0.3 --name fitmeold sbt run

were as

  1. -p for port forwarding because sbt will run the project default on 9000 port.

  2. fitmeold:0.3 is my image name

  3. sbt run to pass Command argument

Instead of the building through docker build, I want to use the Kubernetes to deploy the fitmeold:0.3 container with the replication factor. My YAML file looks like this:

---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: "fitmeold"
  namespace: "default"
  labels:
    app: "fitmeold"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: "fitmeold"
  template:
    metadata:
      labels:
        app: "fitmeold"
    spec:
      containers:
        - name: "fitmeold"
          image: "fitmeold:0.3"
          command:
            - "-p 80:8000 -it sbt run"
---
apiVersion: "autoscaling/v2beta1"
kind: "HorizontalPodAutoscaler"
metadata:
  name: "fitmeold-hpa"
  namespace: "default"
  labels:
    app: "fitmeold"
spec:
  scaleTargetRef:
    kind: "Deployment"
    name: "fitmeold"
    apiVersion: "apps/v1"
  minReplicas: 1
  maxReplicas: 5
  metrics:
    - type: "Resource"
      resource:
        name: "cpu"
        targetAverageUtilization: 80

Is this the right way to pass the command arguments through YAML file or is it equivalent to docker build command?

If that's correct why I am getting ImagePullBackOff as in status for each pods. I am not able to understand what I am doing wrong and how to pass the equivalent docker build command argument in YAML file.

-- Amit Pal
containers
docker
google-kubernetes-engine
kubernetes
kubernetes-pod

2 Answers

11/25/2019

The "ImagePullBackOff" status indicate that the image used by a container cannot be loaded from the image registry. Basically it means you didn't pull your image to Docker hub.

To troubleshoot this, please run on your computer:

$ kubectl describe pod <NAME_OF_YOUR_POD>

From the events list you get, it should appear at least one with "Failed" reason. Please check out the explanation for the event.

In case you see a message like "Error response from daemon: repository not found: does not exist or no pull access" please pull your image by:

$ docker login (provide user and password)
$ docker pull [IMAGE_NAME]
-- xavirubio
Source: StackOverflow

11/25/2019

use the below yamls

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: fitmeold
  name: fitmeold
spec:
  replicas: 1
  selector:
    matchLabels:
      run: fitmeold
  strategy: {}
  template:
    metadata:
      labels:
        run: fitmeold
    spec:
      containers:
      - image: fitmeold:0.3
        command: ["sbt run"]
        name: fitmeold
        ports:
        - containerPort: 8000

service

apiVersion: v1
kind: Service
metadata:
  labels:
    run: fitmeold
  name: fitmeold
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8000
  selector:
    run: fitmeold
-- P Ekambaram
Source: StackOverflow