MiniKube和Kubectl

Minikube is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node.

The Minikube CLI provides basic bootstrapping operations for working with your cluster, including start, stop, status, and delete.

start a minikube

1
2
$ minikube version
$ minikube start

cluster info

1
2
$ kubectl cluster-info #Details of the cluster and it's health status
$ kubectl get nodes # view the nodes in the cluster.

常用命令

  • kubectl get - list resources
  • kubectl describe - show detailed information about a resource
  • kubectl logs - print the logs from a container in a pod
  • kubectl exec - execute a command on a container in a pod

create a deployment

一个Pod中有一个或者多个容器,一个k8s deployment 可以检查Pod的健康情况而且在Pod重启后可以重新启动Pod。deployment 是一种推荐的用来创建和伸缩Pod的方式。

deployment可以指导k8s如何创建和更新你的应用实例。

使用kubectl run命令来创建一个管理Pod的的deployment。我们需要提供deployment的名字和image的所在地。

1
kubectl run nginx-deployment --image=nginx --port=80
查看所有deployment
1
kubectl get deployments
查看pod
1
kubectl get pods -o wide #(-o wide 会显示IP等信息)

当创建一个deployment的时候,k8s会自动的创建一个Pod。

一个Pod 是K8s的抽象,代表了一个或者多个容器,还有一些共享的资源。

一个Pod中的容器共享一个IP地址和端口空间

Pod是K8S平台上自动的单元,当我们在K8S平台上创建了Deployment,Deployment会自动创建Pod.Each Pod is tied to the Node where it is scheduled, and remains there until termination (according to restart policy) or deletion. In case of a Node failure, identical Pods are scheduled on other available Nodes in the cluster.

设置代理

1
kubectl proxy

在主机和k8s集群之间设置代理,然后可以直接通过代理的方法访问API。

Create a service

一个Pod终将死亡,Pod是有生命周期的,当一个工作节点死亡后,上面运行的Pod也会消失。

Service是一种抽象,定义了一系列Pod和策略。

默认情况下,一个Pod只能通过K8S集群中的内部IP地址访问到。如果想让容器可以在外部访问到的话,可以把Pod暴露成一个k8s的Service。

可以使用kubectl expose 命令

1
kubectl expose deployment hello --type=LoadBalancer
访问服务
1
minikube service hello

Service 使用labels 和selector 来匹配Pod.labels 是附着在Object上的键值对。lables 可以在创建的时候也可以在创建后附着在Object上。而且他们可以在任意时刻被修改。

deployment会自动的创建标签给Pod,然后可以用Label来查询Pod的列表。(使用-l 参数)

1
kubectl get pods -l run=nginx

可以采用相同的方式获取service。

1
kubectl get service -l run=nginx

应用一个new label 可以使用 (注意是new label ,并不是修改已有的Label)。

1
kubectl label pod $POD_NAME app=v1

enable 插件

1
minikube addons list

clean up

Now you can clean up the resources you created in your cluster:

1
2
kubectl delete service hello-node
kubectl delete deployment hello-node

删除的时候,label 也可以派上用场。

1
kubectl delete service -l run=kubernetes-bootcamp

Optionally, force removal of the Docker images created:

1
docker rmi hello-node:v1 hello-node:v2 -f

Optionally, stop the Minikube VM:

1
2
minikube stop
eval $(minikube docker-env -u)

Optionally, delete the Minikube VM:

1
minikube delete# optional

运行多个应用

前面,我们创建了一个deployment,然后把它暴露成一个服务,deploymnet只为我们的App创建了一个Pod.当流量增加的时候,我们应该扩容我们的应用。

使用kubectl scale 命令

1
kubectl scale deployments/kubernetes-bootcamp --replicas=4

缩容的话只需要把 replicas 的数值设为1即可。