How to use Local docker image in kubernetes via kubectl

7/23/2019

I created customize Docker Image and stored in my local system Now I want use that Docker Image via kubectl .

Docker image:- 1:- docker build -t backend:v1 .

Then Kubernetes file:-

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: backend
  namespace: web-console
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: backend
    spec:
      containers:
      - env:
        - name: mail_auth_pass
        - name: mail_auth_user
        - name: mail_from
        - name: mail_greeting
        - name: mail_service
        - name: mail_sign
        - name: mongodb_url
          value: mongodb://mongodb.mongodb.svc.cluster.local/console
        - name: server_host
          value: "0.0.0.0"
        - name: server_port
          value: "3000"
        - name: server_sessionSecret
          value: "1234"
          image: backend
          imagePullPolicy: Never
        name: backend
        resources: {}
      restartPolicy: Always
status: {}```

Command to run kubectl:- kubectl create -f  backend-deployment.yaml

**getting Error:-** 
error: error validating "backend-deployment.yaml": error validating data: [ValidationError(Deployment.spec.template.spec.containers[0].env[9]): unknown field "image" in io.k8s.api.core.v1.EnvVar, ValidationError(Deployment.spec.template.spec.containers[0].env[9]): unknown field "imagePullPolicy" in io.k8s.api.core.v1.EnvVar]; if you choose to ignore these errors, turn validation off with --validate=false
-- gaurav agnihotri
docker
kubernetes

1 Answer

7/23/2019

As the error specifies unknown field "image" and unknown field "imagePullPolicy"

There is syntax error in your kubernetes deployment file.

Make these changes in your yaml file.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: backend
  namespace: web-console
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: backend
    spec:
      containers:
      - name: backend
        image: backend
        imagePullPolicy: Never
        env:
        - name: mail_auth_pass
        - name: mail_auth_user
        - name: mail_from
        - name: mail_greeting
        - name: mail_service
        - name: mail_sign
        - name: mongodb_url
          value: mongodb://mongodb.mongodb.svc.cluster.local/console
        - name: server_host
          value: "0.0.0.0"
        - name: server_port
          value: "3000"
        - name: server_sessionSecret
          value: "1234"
        resources: {}
      restartPolicy: Always
status: {}

Validate your kubernetes yaml file online using https://kubeyaml.com/

Or with kubectl apply --validate=true --dry-run=true -f deployment.yaml

Hope this helps.

-- mchawre
Source: StackOverflow