client-go示例

    下面,我们基于client-go,对Deployment升级镜像的步骤进行了定制,通过命令行传递一个Deployment的名字、应用容器名和新image名字的方式来升级。代码和使用方式见 。

    代码如下:

    我们使用文件认证连接kubernetes集群,该文件默认的位置是$HOME/.kube/config

    该代码编译后可以直接在kubernetes集群之外,任何一个可以连接到API server的机器上运行。

    编译运行

    1. $ go get github.com/rootsongjc/kubernetes-client-go-sample
    2. $ cd $GOPATH/src/github.com/rootsongjc/kubernetes-client-go-sample
    3. $ make
    4. $ ./update-deployment-image -h
    5. Usage of ./update-deployment-image:
    6. -alsologtostderr
    7. log to standard error as well as files
    8. -app string
    9. application name (default "app")
    10. -deployment string
    11. deployment name
    12. -image string
    13. new image name
    14. -kubeconfig string
    15. (optional) absolute path to the kubeconfig file (default "/Users/jimmy/.kube/config")
    16. -log_backtrace_at value
    17. when logging hits line file:N, emit a stack trace
    18. If non-empty, write log files in this directory
    19. -logtostderr
    20. log to standard error instead of files
    21. -stderrthreshold value
    22. logs at or above this threshold go to stderr
    23. -v value
    24. log level for V logs
    25. -vmodule value
    26. comma-separated list of pattern=N settings for file-filtered logging

    查看Deployment的event。

    1. $ kubectl describe deployment filebeat-test
    2. Name: filebeat-test
    3. Namespace: default
    4. CreationTimestamp: Fri, 19 May 2017 15:12:28 +0800
    5. Labels: k8s-app=filebeat-test
    6. Selector: k8s-app=filebeat-test
    7. Replicas: 2 updated | 3 total | 2 available | 2 unavailable
    8. StrategyType: RollingUpdate
    9. MinReadySeconds: 0
    10. Conditions:
    11. Type Status Reason
    12. ---- ------ ------
    13. Available True MinimumReplicasAvailable
    14. Progressing True ReplicaSetUpdated
    15. OldReplicaSets: filebeat-test-2365467882 (2/2 replicas created)
    16. NewReplicaSet: filebeat-test-2470325483 (2/2 replicas created)
    17. Events:
    18. FirstSeen LastSeen Count From SubObjectPath Type ReasoMessage
    19. --------- -------- ----- ---- ------------- -------- ------------
    20. 2h 1m 3 {deployment-controller } Normal ScalingReplicaSet Scaled down replica set filebeat-test-2365467882 to 2
    21. 1m 1m 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica set filebeat-test-2470325483 to 1
    22. 1m 1m 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica set filebeat-test-2470325483 to 2

    可以看到老的ReplicaSet从3个replica减少到了2个,有2个使用新配置的replica不可用,目前可用的replica是2个。

    这是因为我们指定的镜像不存在,查看Deployment的pod的状态。

    我们可以看到有两个pod正在拉取image。

    还原为原先的镜像

    1. $ ./update-deployment-image -deployment filebeat-test -image harbor-001.jimmysong.io/library/analytics-docker-test:Build_8
    2. Found deployment
    3. name -> filebeat-test
    4. Old image -> harbor-001.jimmysong.io/library/analytics-docker-test:Build_9
    5. New image -> harbor-001.jimmysong.io/library/analytics-docker-test:Build_8

    现在再查看Deployment的状态。

    可以看到available的replica个数恢复成3了。

    其实在使用该命令的过程中,通过kubernetes dashboard的页面上查看Deployment的状态更直观,更加方便故障排查。

    这也是dashboard最大的优势,简单、直接、高效。