系統 - K3s - Import Docker Image


參考資訊:
https://cwienczek.com/2020/06/import-images-to-k3s-without-docker-registry/

Dockerfile

FROM nginx

RUN echo "123" > /MyDockerImage

建立Docker Image

$ sudo docker build -t mydocker .
$ docker image ls
    REPOSITORY                         TAG                    IMAGE ID       CREATED          SIZE
    mydocker                           latest                 d9803798f068   43 seconds ago   188MB

$ sudo docker save mydocker -o mydocker.tar
$ ls -al mydocker.tar 
    -rw------- 1 root root 191824896 May 15 08:31 mydocker.tar

匯入Docker Image到K3s

$ sudo k3s ctr image import mydocker.tar
$ sudo k3s ctr image ls
    docker.io/library/mydocker:latest

main.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: mydocker
        imagePullPolicy: Never
        ports:
        - containerPort: 80

建立Pod

$ kubectl create -f main.yaml
$ kubectl get pod
    NAME                                READY   STATUS    RESTARTS   AGE
    nginx-deployment-574c6948bf-6cxcd   1/1     Running   0          13s

運行Pod

$ kubectl exec -it nginx-deployment-574c6948bf-6cxcd -- /bin/bash

root@nginx-deployment-574c6948bf-6cxcd:/# cat /MyDockerImage 
    123

刪除Pod

$ kubectl get deployment
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   1/1     1            1           111s

$ kubectl delete deployment nginx-deployment
    deployment.apps "nginx-deployment" deleted

$ kubectl get pod
    No resources found in default namespace.