Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Local docker image in kubernetes via kubectl

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
like image 273
gaurav agnihotri Avatar asked Dec 22 '22 22:12

gaurav agnihotri


2 Answers

Local Registry

Set the local registry first using this command

docker run -d -p 5000:5000 --restart=always --name registry registry:2

Image Tag

Given a Dockerfile, the image could be built and tagged this easy way:

docker build -t localhost:5000/my-image

Image Pull Policy

the field imagePullPolicy should then be changed to Never get the right image from the right repo.

given this sample pod template

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
spec:
  containers:
    - name: app
      image: localhost:5000/my-image
      imagePullPolicy: Never

Deploy Pod

The pod can be deployed using:

kubectl create -f pod.yml

Hope this comes in handy :)

like image 128
Amado Saladino Avatar answered Jan 04 '23 01:01

Amado Saladino


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.

like image 36
mchawre Avatar answered Jan 04 '23 00:01

mchawre