本文不对Kubernetes作过多介绍,直接讲Kubernetes的各类YAML基本撰写规范。 基本概念请见: http://www.infoq.com/cn/articles/Kubernetes-system-architecture-introduction
全部的Resource的定义文档在这里都有 http://kubernetes.io/v1.0/docs/api-reference/definitions.html 例如:你要查询Pod声明里面有哪些字段,那么颇有用. 很少废话。html
Kubernetes的编排基本单元是Pod,故而哪怕你只有一个Container,也要建立一个Pod来容纳这个Container.nginx
apiVersion: v1 kind: Pod metadata: name: hello-world # pod资源名称,集群unique的 spec: # specification of the pod’s contents restartPolicy: Never # 运行这个容器一次,而后就结束这个pod containers: - name: hello # 只是这个container的nickname image: "ubuntu:14.04" # image 名, 默认使用Docker Hub command: ["/bin/echo","hello”,”world"]
部分解释见上,其中command覆盖了Docker容器的Entrypoint
, Command参数(对应于Docker的Cmd)可使用args
来声明:golang
command: ["/bin/echo"] args: ["hello","world"]
建立pod则为:shell
$ kubectl create -f ./hello-world.yaml --validate # 将会进行验证而且给出WARN,可是出问题的话,依然会建立Pod,会输出WARN信息 pods/hello-world
apiVersion: v1 kind: Pod metadata: name: hello-world spec: # specification of the pod’s contents restartPolicy: Never containers: - name: hello image: "ubuntu:14.04" env: # 这是一个环境变量 - name: MESSAGE value: "hello world" command: ["/bin/sh","-c"] # Kubernetes并不会自动运行shell,须要手动指定 args: ["/bin/echo \"${MESSAGE}\""] # 这是用到的环境变量展开
若是没有shell咱们的环境变量依然能够用$(ENVVAR)
来展开。例如:ubuntu
command: ["/bin/echo"] args: ["$(MESSAGE)"]
##2. 查看Pod的状态##api
$ kubectl get pods NAME READY STATUS RESTARTS AGE hello-world 0/1 Pending 0 0s
状态以下:bash
unscheduled - 一开始建立POD,还没选择到节点来运行Pod,而后当即schedule
scheduled - Pod已经被Scheduled,准备在目标节点pull镜像
running - 从镜像启动容器成功, READY列表示多少个容器正常
ExitCode:0 - 正常退出,容器再也不运行app
##3. 删除Pod##spa
$ kubectl delete pod hello-world pods/hello-world
或者 resource/name 格式来删除rest
$ kubectl delete pods/hello-world pods/hello-world
会把容器和其输出的日志都删除。
##4. 建立Replication Controller## Replication Controller能够保证『副本数量正确』的Pod在运行,下面简称RC/rc。下面是2副本的Nginx:
apiVersion: v1 kind: ReplicationController # 再也不是Pod metadata: name: my-nginx spec: replicas: 2 template: # 这是一个PodTemplateSpec,下面声明了Pod的规范 metadata: # 无需声明Pod名称,由于他们由RC自动生成 labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 # 对外开放的端口
##5. 删除Replication Controller##
$ kubectl delete rc my-nginx replicationcontrollers/my-nginx
Kubernetes使用Lable来分类、识别不一样的资源集合,例如Pods和Replication Controllers。 咱们能够用app
这个key和nginx
这个value来读取pods和rc:
$ kubectl get pods -L app NAME READY STATUS RESTARTS AGE APP my-nginx-afv12 0/1 Running 0 3s nginx my-nginx-lg99z 0/1 Running 0 3s nginx $ kubectl get rc my-nginx -L app CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS APP my-nginx nginx nginx app=nginx 2 nginx
也可使用golang的模板来获取信息
$ kubectl get rc my-nginx -o template --template="{{.spec.selector}}" map[app:nginx]
下面是一个服务的yaml文件。服务是与Pod松耦合的。能够自由组合。一旦建立完毕就分配一个clusterIP,而对这个clusterIP会有个简单的load balancer.
apiVersion: v1 kind: Service metadata: name: nginxsvc labels: app: nginx spec: ports: - port: 80 # 访问的地址为 http://clusterIP:80/ protocol: TCP selector: # 意思是由app=nginx的pod来处理 app: nginx
一个Service背后能够有多个endpoints,来进行处理。
$ kubectl describe svc nginxsvc Name: nginxsvc Namespace: default Labels: app=nginx Selector: app=nginx Type: ClusterIP IP: 10.0.116.146 Port: <unnamed> 80/TCP Endpoints: 10.245.0.14:80,10.245.0.15:80 Session Affinity: None No events. $ kubectl get ep NAME ENDPOINTS nginxsvc 10.245.0.14:80,10.245.0.15:80