Tuesday, February 12, 2019

学习舵轮(kubernetes)和埠(docker) - Kubernetes装载docker image

有了dockers image,用Kubernetes来自动装载管理很简单。

Creating docker image and deploying Kubernetes pod/service

Refer to kubernetes containers images/. Note, with snap, Docker stores keys for private registries in $HOME/snap/docker/.docker/config.json but not $HOME/.dockercfg or $HOME/.docker/config.json. But for non private repository, no need of the credentials and k8s uses docker under the hood. 我在Docker Hub 创建了ifun public repository. Then run:
 docker tag ifun:v0.2 quyq/ifun:v0.2
and
 docker push quyq/ifun:v0.2
‘push'命令把local build的image 上传到Docker Hub, which is 83M in size (my local shows the image is 206M?). To start the image as kubernetes pod: kubectl run ifun --image=quyq/ifun:v0.2 --port=3000 (If get: ErrImagePull=>trying and failing to pull image, ImagePullBackOff=>image can't be pulled). To create service run: kubectl expose deployment/ifun --type="NodePort" --port 3000 => get: service/ifun exposed. Then run kubectl get services will get like:
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE
ifun         NodePort    10.106.182.186  <none>      3000:32574/TCP    1m
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          16d

Use this url to access: $(minikube ip):32574, where minikube ip will return 192.168.99.100, which is the VM’s IP. Run minikube dashboard for detail.

To use a yaml file for the container with host volume, create the yaml file like this(try to avoid tab in yaml):

apiVersion: v1
kind: Pod
metadata:
 name: node-mongo
 labels:
   purpose: test-node_mongo
spec:
 containers:
 - name: ifun
   image: quyq/ifun:v0.2
   ports:
   - containerPort: 3000
   volumeMounts:
   - mountPath: /data
     name: db-volume
 volumes:
 - name: db-volume
   hostPath:
     path: c:/Temp # directory location on host(the node/VM, not host PC running the VM)
     type: Directory # this field is optional
Run: kubectl create -f my.yaml => run kubectl get pods will show: node-mongo   0/1     ContainerCreating   0  … 
Run kubectl logs node-mongo get:
Error from server (BadRequest): container "ifun" in pod "node-mongo" is waiting to start: ContainerCreating. kubectl describe pods gives detail of problem: SetUp failed for volume "db-volume" : hostPath type check failed: c:/Temp is not a directory. 我是在Windows PC上运行上述命令,如上述comment所述,mount的volume必须是那个运行docker的Linux VM而不是host Windows PC. Mount failure 导致pod failed to start. Change c:/Temp to /tmp and re-run, it can create the pod now. 

But run kubectl get deployments show nothing? Refer to this SO: set --restart=Always. When use yaml, have to create a deploy yaml like:
apiVersion: extensions/v1beta1 #why cannot be just v1?
kind: Deployment
metadata:
  name: node-mongo
spec:
  replicas: 2    #optional, default is one pod
  template:
     Pod’ metadata and spec here ...
This will create two pods and db would be stored in the VM’s /tmp folder. Since /tmp might get cleaned when restart VM, so should create a dedicated folder, such as /mongo_db on the VM, and change the volumes/hostPath/path to /mongo_db.现在可以运行Kubernetes supported NodeJS + MongoDB,database文件会存在虚拟机的/tmp目录下

Updating image

Kubernetes Docker VM contains docker, and it can access shared folder of host PC, such as c:\Users folder. So for running Kubernetes on Windows, after updating docker file, can push the update from the VM with exist tag as this: docker login; docker push quyq/ifun:v0.2; need a way to force image pull
To create the image with a new tag:

 docker image build -t ifun:v0.3 . (same as docker build --tag=ifun:v0.3 . ? image is optional);
 docker tag ifun:v0.3 quyq/ifun:v0.3; 
 docker push quyq/ifun:v0.3;
 then run kubectl edit deployment/node-mongo, and wait the deployment is done.


0 Comments:

Post a Comment