kubernetes-handbook/develop/client-go-sample.html

1747 lines
80 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html lang="zh-cn" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>6.3 client-go示例 · Kubernetes Handbook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.2">
<meta name="author" content="Jimmy Song">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-page-toc-button/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-image-captions/image-captions.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-page-footer-ex/style/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="contribute.html" />
<link rel="prev" href="testing.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="輸入並搜尋" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
1. 前言
</a>
</li>
<li class="chapter " data-level="1.2" data-path="../concepts/">
<a href="../concepts/">
2. 概念原理
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="../concepts/concepts.html">
<a href="../concepts/concepts.html">
2.1 设计理念
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="../concepts/objects.html">
<a href="../concepts/objects.html">
2.2 主要概念
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.2.1" data-path="../concepts/pod-overview.html">
<a href="../concepts/pod-overview.html">
2.2.1 Pod
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.2.1.1" data-path="../concepts/pod.html">
<a href="../concepts/pod.html">
2.2.1.1 Pod解析
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2.2.2" data-path="../concepts/node.html">
<a href="../concepts/node.html">
2.2.2 Node
</a>
</li>
<li class="chapter " data-level="1.2.2.3" data-path="../concepts/namespace.html">
<a href="../concepts/namespace.html">
2.2.3 Namespace
</a>
</li>
<li class="chapter " data-level="1.2.2.4" data-path="../concepts/service.html">
<a href="../concepts/service.html">
2.2.4 Service
</a>
</li>
<li class="chapter " data-level="1.2.2.5" data-path="../concepts/volume.html">
<a href="../concepts/volume.html">
2.2.5 Volume和Persistent Volume
</a>
</li>
<li class="chapter " data-level="1.2.2.6" data-path="../concepts/deployment.html">
<a href="../concepts/deployment.html">
2.2.6 Deployment
</a>
</li>
<li class="chapter " data-level="1.2.2.7" data-path="../concepts/secret.html">
<a href="../concepts/secret.html">
2.2.7 Secret
</a>
</li>
<li class="chapter " data-level="1.2.2.8" data-path="../concepts/statefulset.html">
<a href="../concepts/statefulset.html">
2.2.8 StatefulSet
</a>
</li>
<li class="chapter " data-level="1.2.2.9" data-path="../concepts/daemonset.html">
<a href="../concepts/daemonset.html">
2.2.9 DaemonSet
</a>
</li>
<li class="chapter " data-level="1.2.2.10" data-path="../concepts/serviceaccount.html">
<a href="../concepts/serviceaccount.html">
2.2.10 ServiceAccount
</a>
</li>
<li class="chapter " data-level="1.2.2.11" data-path="../concepts/replicaset.html">
<a href="../concepts/replicaset.html">
2.2.11 ReplicationController和ReplicaSet
</a>
</li>
<li class="chapter " data-level="1.2.2.12" data-path="../concepts/job.html">
<a href="../concepts/job.html">
2.2.12 Job
</a>
</li>
<li class="chapter " data-level="1.2.2.13" data-path="../concepts/cronjob.html">
<a href="../concepts/cronjob.html">
2.2.13 CronJob
</a>
</li>
<li class="chapter " data-level="1.2.2.14" data-path="../concepts/ingress.html">
<a href="../concepts/ingress.html">
2.2.14 Ingress
</a>
</li>
<li class="chapter " data-level="1.2.2.15" data-path="../concepts/configmap.html">
<a href="../concepts/configmap.html">
2.2.15 ConfigMap
</a>
</li>
<li class="chapter " data-level="1.2.2.16" data-path="../concepts/horizontal-pod-autoscaling.html">
<a href="../concepts/horizontal-pod-autoscaling.html">
2.2.16 Horizontal Pod Autoscaling
</a>
</li>
<li class="chapter " data-level="1.2.2.17" data-path="../concepts/label.html">
<a href="../concepts/label.html">
2.2.17 Label
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../guide/">
<a href="../guide/">
3. 用户指南
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../guide/resource-configuration.html">
<a href="../guide/resource-configuration.html">
3.1 资源配置
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1.1" data-path="../guide/configure-liveness-readiness-probes.html">
<a href="../guide/configure-liveness-readiness-probes.html">
3.1.1 配置Pod的liveness和readiness探针
</a>
</li>
<li class="chapter " data-level="1.3.1.2" data-path="../guide/configure-pod-service-account.html">
<a href="../guide/configure-pod-service-account.html">
3.1.2 配置Pod的Service Account
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3.2" data-path="../guide/command-usage.html">
<a href="../guide/command-usage.html">
3.2 命令使用
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.2.1" data-path="../guide/using-kubectl.html">
<a href="../guide/using-kubectl.html">
3.2.1 使用kubectl
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3.3" data-path="../guide/cluster-management.html">
<a href="../guide/cluster-management.html">
3.3 集群管理
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.3.1" data-path="../guide/managing-tls-in-a-cluster.html">
<a href="../guide/managing-tls-in-a-cluster.html">
3.3.1 管理集群中的TLS
</a>
</li>
<li class="chapter " data-level="1.3.3.2" data-path="../guide/kubelet-authentication-authorization.html">
<a href="../guide/kubelet-authentication-authorization.html">
3.3.2 kubelet的认证授权
</a>
</li>
<li class="chapter " data-level="1.3.3.3" data-path="../guide/tls-bootstrapping.html">
<a href="../guide/tls-bootstrapping.html">
3.3.3 TLS bootstrap
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3.4" data-path="../guide/access-kubernetes-cluster.html">
<a href="../guide/access-kubernetes-cluster.html">
3.4 访问 Kubernetes 集群
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.4.1" data-path="../guide/access-cluster.html">
<a href="../guide/access-cluster.html">
3.4.1 访问集群
</a>
</li>
<li class="chapter " data-level="1.3.4.2" data-path="../guide/authenticate-across-clusters-kubeconfig.html">
<a href="../guide/authenticate-across-clusters-kubeconfig.html">
3.4.2 使用 kubeconfig 文件配置跨集群认证
</a>
</li>
<li class="chapter " data-level="1.3.4.3" data-path="../guide/connecting-to-applications-port-forward.html">
<a href="../guide/connecting-to-applications-port-forward.html">
3.4.3 通过端口转发访问集群中的应用程序
</a>
</li>
<li class="chapter " data-level="1.3.4.4" data-path="../guide/service-access-application-cluster.html">
<a href="../guide/service-access-application-cluster.html">
3.4.4 使用 service 访问群集中的应用程序
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3.5" data-path="../guide/application-development-deployment-flow.html">
<a href="../guide/application-development-deployment-flow.html">
3.5 在kubernetes中开发部署应用
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.5.1" data-path="../guide/deploy-applications-in-kubernetes.html">
<a href="../guide/deploy-applications-in-kubernetes.html">
3.5.1 适用于kubernetes的应用开发部署流程
</a>
</li>
<li class="chapter " data-level="1.3.5.2" data-path="../guide/migrating-hadoop-yarn-to-kubernetes.html">
<a href="../guide/migrating-hadoop-yarn-to-kubernetes.html">
3.5.2 迁移传统应用到kubernetes中——以Hadoop YARN为例
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../practice/">
<a href="../practice/">
4. 最佳实践
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../practice/install-kbernetes1.6-on-centos.html">
<a href="../practice/install-kbernetes1.6-on-centos.html">
4.1 在CentOS上部署kubernetes1.6集群
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1.1" data-path="../practice/create-tls-and-secret-key.html">
<a href="../practice/create-tls-and-secret-key.html">
4.1.1 创建TLS证书和秘钥
</a>
</li>
<li class="chapter " data-level="1.4.1.2" data-path="../practice/create-kubeconfig.html">
<a href="../practice/create-kubeconfig.html">
4.1.2 创建kubeconfig文件
</a>
</li>
<li class="chapter " data-level="1.4.1.3" data-path="../practice/etcd-cluster-installation.html">
<a href="../practice/etcd-cluster-installation.html">
4.1.3 创建高可用etcd集群
</a>
</li>
<li class="chapter " data-level="1.4.1.4" data-path="../practice/kubectl-installation.html">
<a href="../practice/kubectl-installation.html">
4.1.4 安装kubectl命令行工具
</a>
</li>
<li class="chapter " data-level="1.4.1.5" data-path="../practice/master-installation.html">
<a href="../practice/master-installation.html">
4.1.5 部署master节点
</a>
</li>
<li class="chapter " data-level="1.4.1.6" data-path="../practice/node-installation.html">
<a href="../practice/node-installation.html">
4.1.6 部署node节点
</a>
</li>
<li class="chapter " data-level="1.4.1.7" data-path="../practice/kubedns-addon-installation.html">
<a href="../practice/kubedns-addon-installation.html">
4.1.7 安装kubedns插件
</a>
</li>
<li class="chapter " data-level="1.4.1.8" data-path="../practice/dashboard-addon-installation.html">
<a href="../practice/dashboard-addon-installation.html">
4.1.8 安装dashboard插件
</a>
</li>
<li class="chapter " data-level="1.4.1.9" data-path="../practice/heapster-addon-installation.html">
<a href="../practice/heapster-addon-installation.html">
4.1.9 安装heapster插件
</a>
</li>
<li class="chapter " data-level="1.4.1.10" data-path="../practice/efk-addon-installation.html">
<a href="../practice/efk-addon-installation.html">
4.1.10 安装EFK插件
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.2" data-path="../practice/service-discovery-and-loadbalancing.html">
<a href="../practice/service-discovery-and-loadbalancing.html">
4.2 服务发现与负载均衡
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.2.1" data-path="../practice/traefik-ingress-installation.html">
<a href="../practice/traefik-ingress-installation.html">
4.2.1 安装Traefik ingress
</a>
</li>
<li class="chapter " data-level="1.4.2.2" data-path="../practice/distributed-load-test.html">
<a href="../practice/distributed-load-test.html">
4.2.2 分布式负载测试
</a>
</li>
<li class="chapter " data-level="1.4.2.3" data-path="../practice/network-and-cluster-perfermance-test.html">
<a href="../practice/network-and-cluster-perfermance-test.html">
4.2.3 网络和集群性能测试
</a>
</li>
<li class="chapter " data-level="1.4.2.4" data-path="../practice/edge-node-configuration.html">
<a href="../practice/edge-node-configuration.html">
4.2.4 边缘节点配置
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.3" data-path="../practice/operation.html">
<a href="../practice/operation.html">
4.3 运维管理
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.3.1" data-path="../practice/service-rolling-update.html">
<a href="../practice/service-rolling-update.html">
4.3.1 服务滚动升级
</a>
</li>
<li class="chapter " data-level="1.4.3.2" data-path="../practice/app-log-collection.html">
<a href="../practice/app-log-collection.html">
4.3.2 应用日志收集
</a>
</li>
<li class="chapter " data-level="1.4.3.3" data-path="../practice/configuration-best-practice.html">
<a href="../practice/configuration-best-practice.html">
4.3.3 配置最佳实践
</a>
</li>
<li class="chapter " data-level="1.4.3.4" data-path="../practice/monitor.html">
<a href="../practice/monitor.html">
4.3.4 集群及应用监控
</a>
</li>
<li class="chapter " data-level="1.4.3.5" data-path="../practice/jenkins-ci-cd.html">
<a href="../practice/jenkins-ci-cd.html">
4.3.5 使用Jenkins进行持续构建与发布
</a>
</li>
<li class="chapter " data-level="1.4.3.6" data-path="../practice/data-persistence-problem.html">
<a href="../practice/data-persistence-problem.html">
4.3.6 数据持久化问题
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.4" data-path="../practice/storage.html">
<a href="../practice/storage.html">
4.4 存储管理
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.4.1" data-path="../practice/glusterfs.html">
<a href="../practice/glusterfs.html">
4.4.1 GlusterFS
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.4.1.1" data-path="../practice/using-glusterfs-for-persistent-storage.html">
<a href="../practice/using-glusterfs-for-persistent-storage.html">
4.4.1.1 使用GlusterFS做持久化存储
</a>
</li>
<li class="chapter " data-level="1.4.4.1.2" data-path="../practice/storage-for-containers-using-glusterfs-with-openshift.html">
<a href="../practice/storage-for-containers-using-glusterfs-with-openshift.html">
4.4.1.2 在OpenShift中使用GlusterFS做持久化存储
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../usecases/">
<a href="../usecases/">
5. 领域应用
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../usecases/microservices.html">
<a href="../usecases/microservices.html">
5.1 微服务架构
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1.1" data-path="../usecases/istio.html">
<a href="../usecases/istio.html">
5.1.1 Istio
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1.1.1" data-path="../usecases/istio-installation.html">
<a href="../usecases/istio-installation.html">
5.1.1.1 安装istio
</a>
</li>
<li class="chapter " data-level="1.5.1.1.2" data-path="../usecases/configuring-request-routing.html">
<a href="../usecases/configuring-request-routing.html">
5.1.1.2 配置请求的路由规则
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.1.2" data-path="../usecases/linkerd.html">
<a href="../usecases/linkerd.html">
5.1.2 Linkerd
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1.2.1" data-path="../usecases/linkerd-user-guide.html">
<a href="../usecases/linkerd-user-guide.html">
5.1.2.1 Linkerd 使用指南
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.1.3" data-path="../usecases/service-discovery-in-microservices.html">
<a href="../usecases/service-discovery-in-microservices.html">
5.1.3 微服务中的服务发现
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5.2" data-path="../usecases/big-data.html">
<a href="../usecases/big-data.html">
5.2 大数据
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.2.1" data-path="../usecases/spark-on-kubernetes.html">
<a href="../usecases/spark-on-kubernetes.html">
5.2.1 Spark on Kubernetes
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="./">
<a href="./">
6. 开发指南
</a>
<ul class="articles">
<li class="chapter " data-level="1.6.1" data-path="developing-environment.html">
<a href="developing-environment.html">
6.1 开发环境搭建
</a>
</li>
<li class="chapter " data-level="1.6.2" data-path="testing.html">
<a href="testing.html">
6.2 单元测试和集成测试
</a>
</li>
<li class="chapter active" data-level="1.6.3" data-path="client-go-sample.html">
<a href="client-go-sample.html">
6.3 client-go示例
</a>
</li>
<li class="chapter " data-level="1.6.4" data-path="contribute.html">
<a href="contribute.html">
6.4 社区贡献
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.7" data-path="../appendix/">
<a href="../appendix/">
7. 附录
</a>
<ul class="articles">
<li class="chapter " data-level="1.7.1" data-path="../appendix/docker-best-practice.html">
<a href="../appendix/docker-best-practice.html">
7.1 Docker最佳实践
</a>
</li>
<li class="chapter " data-level="1.7.2" data-path="../appendix/issues.html">
<a href="../appendix/issues.html">
7.2 问题记录
</a>
</li>
<li class="chapter " data-level="1.7.3" data-path="../appendix/tricks.html">
<a href="../appendix/tricks.html">
7.3 使用技巧
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本書使用 GitBook 釋出
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >6.3 client-go示例</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div class="search-plus" id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="client-go&#x793A;&#x4F8B;">client-go&#x793A;&#x4F8B;</h1>
<p>&#x8BBF;&#x95EE;kubernetes&#x96C6;&#x7FA4;&#x6709;&#x51E0;&#x4E0B;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x65B9;&#x5F0F;</th>
<th>&#x7279;&#x70B9;</th>
<th>&#x652F;&#x6301;&#x8005;</th>
</tr>
</thead>
<tbody>
<tr>
<td>Kubernetes dashboard</td>
<td>&#x76F4;&#x63A5;&#x901A;&#x8FC7;Web UI&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x7B80;&#x5355;&#x76F4;&#x63A5;&#xFF0C;&#x53EF;&#x5B9A;&#x5236;&#x5316;&#x7A0B;&#x5EA6;&#x4F4E;</td>
<td>&#x5B98;&#x65B9;&#x652F;&#x6301;</td>
</tr>
<tr>
<td>kubectl</td>
<td>&#x547D;&#x4EE4;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x529F;&#x80FD;&#x6700;&#x5168;&#xFF0C;&#x4F46;&#x662F;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;&#x9002;&#x5408;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x8FDB;&#x4E00;&#x6B65;&#x7684;&#x5206;&#x88C5;&#xFF0C;&#x5B9A;&#x5236;&#x529F;&#x80FD;&#xFF0C;&#x7248;&#x672C;&#x9002;&#x914D;&#x6700;&#x597D;</td>
<td>&#x5B98;&#x65B9;&#x652F;&#x6301;</td>
</tr>
<tr>
<td><a href="https://github.com/kubernetes/client-go" target="_blank">client-go</a></td>
<td>&#x4ECE;kubernetes&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#x62BD;&#x79BB;&#x51FA;&#x6765;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x5305;&#xFF0C;&#x7B80;&#x5355;&#x6613;&#x7528;&#xFF0C;&#x4F46;&#x9700;&#x8981;&#x5C0F;&#x5FC3;&#x533A;&#x5206;kubernetes&#x7684;API&#x7248;&#x672C;</td>
<td>&#x5B98;&#x65B9;&#x652F;&#x6301;</td>
</tr>
<tr>
<td><a href="https://github.com/kubernetes-incubator/client-python" target="_blank">client-python</a></td>
<td>python&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;kubernetes-incubator</td>
<td>&#x5B98;&#x65B9;&#x652F;&#x6301;</td>
</tr>
<tr>
<td><a href="https://github.com/fabric8io/kubernetes-client" target="_blank">Java client</a></td>
<td>fabric8&#x4E2D;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;kubernetes&#x7684;java&#x5BA2;&#x6237;&#x7AEF;</td>
<td>redhat</td>
</tr>
</tbody>
</table>
<p>&#x4E0B;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x57FA;&#x4E8E;<a href="https://github.com/kubernetes/client-go" target="_blank">client-go</a>&#xFF0C;&#x5BF9;Deployment&#x5347;&#x7EA7;&#x955C;&#x50CF;&#x7684;&#x6B65;&#x9AA4;&#x8FDB;&#x884C;&#x4E86;&#x5B9A;&#x5236;&#xFF0C;&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x4F20;&#x9012;&#x4E00;&#x4E2A;Deployment&#x7684;&#x540D;&#x5B57;&#x3001;&#x5E94;&#x7528;&#x5BB9;&#x5668;&#x540D;&#x548C;&#x65B0;image&#x540D;&#x5B57;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5347;&#x7EA7;&#x3002;&#x4EE3;&#x7801;&#x548C;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x89C1; <a href="https://github.com/rootsongjc/kubernetes-client-go-sample" target="_blank">https://github.com/rootsongjc/kubernetes-client-go-sample</a> &#x3002;</p>
<h2 id="kubernetes-client-go-sample">kubernetes-client-go-sample</h2>
<p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">package</span> main
<span class="hljs-keyword">import</span> (
<span class="hljs-string">&quot;flag&quot;</span>
<span class="hljs-string">&quot;fmt&quot;</span>
<span class="hljs-string">&quot;os&quot;</span>
<span class="hljs-string">&quot;path/filepath&quot;</span>
<span class="hljs-string">&quot;k8s.io/apimachinery/pkg/api/errors&quot;</span>
metav1 <span class="hljs-string">&quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;</span>
<span class="hljs-string">&quot;k8s.io/client-go/kubernetes&quot;</span>
<span class="hljs-string">&quot;k8s.io/client-go/tools/clientcmd&quot;</span>
)
<span class="hljs-keyword">func</span> main() {
<span class="hljs-keyword">var</span> kubeconfig *<span class="hljs-keyword">string</span>
<span class="hljs-keyword">if</span> home := homeDir(); home != <span class="hljs-string">&quot;&quot;</span> {
kubeconfig = flag.String(<span class="hljs-string">&quot;kubeconfig&quot;</span>, filepath.Join(home, <span class="hljs-string">&quot;.kube&quot;</span>, <span class="hljs-string">&quot;config&quot;</span>), <span class="hljs-string">&quot;(optional) absolute path to the kubeconfig file&quot;</span>)
} <span class="hljs-keyword">else</span> {
kubeconfig = flag.String(<span class="hljs-string">&quot;kubeconfig&quot;</span>, <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-string">&quot;absolute path to the kubeconfig file&quot;</span>)
}
deploymentName := flag.String(<span class="hljs-string">&quot;deployment&quot;</span>, <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-string">&quot;deployment name&quot;</span>)
imageName := flag.String(<span class="hljs-string">&quot;image&quot;</span>, <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-string">&quot;new image name&quot;</span>)
appName := flag.String(<span class="hljs-string">&quot;app&quot;</span>, <span class="hljs-string">&quot;app&quot;</span>, <span class="hljs-string">&quot;application name&quot;</span>)
flag.Parse()
<span class="hljs-keyword">if</span> *deploymentName == <span class="hljs-string">&quot;&quot;</span> {
fmt.Println(<span class="hljs-string">&quot;You must specify the deployment name.&quot;</span>)
os.Exit(<span class="hljs-number">0</span>)
}
<span class="hljs-keyword">if</span> *imageName == <span class="hljs-string">&quot;&quot;</span> {
fmt.Println(<span class="hljs-string">&quot;You must specify the new image name.&quot;</span>)
os.Exit(<span class="hljs-number">0</span>)
}
<span class="hljs-comment">// use the current context in kubeconfig</span>
config, err := clientcmd.BuildConfigFromFlags(<span class="hljs-string">&quot;&quot;</span>, *kubeconfig)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
<span class="hljs-built_in">panic</span>(err.Error())
}
<span class="hljs-comment">// create the clientset</span>
clientset, err := kubernetes.NewForConfig(config)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
<span class="hljs-built_in">panic</span>(err.Error())
}
deployment, err := clientset.AppsV1beta1().Deployments(<span class="hljs-string">&quot;default&quot;</span>).Get(*deploymentName, metav1.GetOptions{})
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
<span class="hljs-built_in">panic</span>(err.Error())
}
<span class="hljs-keyword">if</span> errors.IsNotFound(err) {
fmt.Printf(<span class="hljs-string">&quot;Deployment not found\n&quot;</span>)
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf(<span class="hljs-string">&quot;Error getting deployment%v\n&quot;</span>, statusError.ErrStatus.Message)
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
<span class="hljs-built_in">panic</span>(err.Error())
} <span class="hljs-keyword">else</span> {
fmt.Printf(<span class="hljs-string">&quot;Found deployment\n&quot;</span>)
name := deployment.GetName()
fmt.Println(<span class="hljs-string">&quot;name -&gt;&quot;</span>, name)
containers := &amp;deployment.Spec.Template.Spec.Containers
found := <span class="hljs-literal">false</span>
<span class="hljs-keyword">for</span> i := <span class="hljs-keyword">range</span> *containers {
c := *containers
<span class="hljs-keyword">if</span> c[i].Name == *appName {
found = <span class="hljs-literal">true</span>
fmt.Println(<span class="hljs-string">&quot;Old image -&gt;&quot;</span>, c[i].Image)
fmt.Println(<span class="hljs-string">&quot;New image -&gt;&quot;</span>, *imageName)
c[i].Image = *imageName
}
}
<span class="hljs-keyword">if</span> found == <span class="hljs-literal">false</span> {
fmt.Println(<span class="hljs-string">&quot;The application container not exist in the deployment pods.&quot;</span>)
os.Exit(<span class="hljs-number">0</span>)
}
_, err := clientset.AppsV1beta1().Deployments(<span class="hljs-string">&quot;default&quot;</span>).Update(deployment)
<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> {
<span class="hljs-built_in">panic</span>(err.Error())
}
}
}
<span class="hljs-keyword">func</span> homeDir() <span class="hljs-keyword">string</span> {
<span class="hljs-keyword">if</span> h := os.Getenv(<span class="hljs-string">&quot;HOME&quot;</span>); h != <span class="hljs-string">&quot;&quot;</span> {
<span class="hljs-keyword">return</span> h
}
<span class="hljs-keyword">return</span> os.Getenv(<span class="hljs-string">&quot;USERPROFILE&quot;</span>) <span class="hljs-comment">// windows</span>
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528;<code>kubeconfig</code>&#x6587;&#x4EF6;&#x8BA4;&#x8BC1;&#x8FDE;&#x63A5;kubernetes&#x96C6;&#x7FA4;&#xFF0C;&#x8BE5;&#x6587;&#x4EF6;&#x9ED8;&#x8BA4;&#x7684;&#x4F4D;&#x7F6E;&#x662F;<code>$HOME/.kube/config</code>&#x3002;</p>
<p>&#x8BE5;&#x4EE3;&#x7801;&#x7F16;&#x8BD1;&#x540E;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;kubernetes&#x96C6;&#x7FA4;&#x4E4B;&#x5916;&#xFF0C;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x8FDE;&#x63A5;&#x5230;API server&#x7684;&#x673A;&#x5668;&#x4E0A;&#x8FD0;&#x884C;&#x3002;</p>
<p><strong>&#x7F16;&#x8BD1;&#x8FD0;&#x884C;</strong></p>
<pre><code class="lang-bash">$ go get github.com/rootsongjc/kubernetes-client-go-sample
$ <span class="hljs-built_in">cd</span> <span class="hljs-variable">$GOPATH</span>/src/github.com/rootsongjc/kubernetes-client-go-sample
$ make
$ ./update-deployment-image -h
Usage of ./update-deployment-image:
-alsologtostderr
<span class="hljs-built_in">log</span> to standard error as well as files
-app string
application name (default <span class="hljs-string">&quot;app&quot;</span>)
-deployment string
deployment name
-image string
new image name
-kubeconfig string
(optional) absolute path to the kubeconfig file (default <span class="hljs-string">&quot;/Users/jimmy/.kube/config&quot;</span>)
-log_backtrace_at value
when logging hits line file:N, emit a stack trace
-log_dir string
If non-empty, write <span class="hljs-built_in">log</span> files <span class="hljs-keyword">in</span> this directory
-logtostderr
<span class="hljs-built_in">log</span> to standard error instead of files
-stderrthreshold value
logs at or above this threshold go to stderr
-v value
<span class="hljs-built_in">log</span> level <span class="hljs-keyword">for</span> V logs
-vmodule value
comma-separated list of pattern=N settings <span class="hljs-keyword">for</span> file-filtered logging
</code></pre>
<p><strong>&#x4F7F;&#x7528;&#x4E0D;&#x5B58;&#x5728;&#x7684;image&#x66F4;&#x65B0;</strong></p>
<pre><code class="lang-bash"> $ ./update-deployment-image -deployment filebeat-test -image sz-pg-oam-docker-hub-001.tendcloud.com/library/analytics-docker-test:Build_9
Found deployment
name -&gt; filebeat-test
Old image -&gt; sz-pg-oam-docker-hub-001.tendcloud.com/library/analytics-docker-test:Build_8
New image -&gt; sz-pg-oam-docker-hub-001.tendcloud.com/library/analytics-docker-test:Build_9
</code></pre>
<p>&#x67E5;&#x770B;Deployment&#x7684;event&#x3002;</p>
<pre><code class="lang-Bash">$ kubectl describe deployment filebeat-test
Name: filebeat-test
Namespace: default
CreationTimestamp: Fri, 19 May 2017 15:12:28 +0800
Labels: k8s-app=filebeat-test
Selector: k8s-app=filebeat-test
Replicas: 2 updated | 3 total | 2 available | 2 unavailable
StrategyType: RollingUpdate
M<span class="hljs-keyword">in</span>ReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True ReplicaSetUpdated
OldReplicaSets: filebeat-test-2365467882 (2/2 replicas created)
NewReplicaSet: filebeat-test-2470325483 (2/2 replicas created)
Events:
FirstSeen LastSeen Count From SubObjectPath Type ReasoMessage
--------- -------- ----- ---- ------------- -------- ------------
2h 1m 3 {deployment-controller } Normal ScalingReplicaSet Scaled down replica <span class="hljs-built_in">set</span> filebeat-test-2365467882 to 2
1m 1m 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica <span class="hljs-built_in">set</span> filebeat-test-2470325483 to 1
1m 1m 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica <span class="hljs-built_in">set</span> filebeat-test-2470325483 to 2
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x8001;&#x7684;ReplicaSet&#x4ECE;3&#x4E2A;replica&#x51CF;&#x5C11;&#x5230;&#x4E86;2&#x4E2A;&#xFF0C;&#x6709;2&#x4E2A;&#x4F7F;&#x7528;&#x65B0;&#x914D;&#x7F6E;&#x7684;replica&#x4E0D;&#x53EF;&#x7528;&#xFF0C;&#x76EE;&#x524D;&#x53EF;&#x7528;&#x7684;replica&#x662F;2&#x4E2A;&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x6307;&#x5B9A;&#x7684;&#x955C;&#x50CF;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x67E5;&#x770B;Deployment&#x7684;pod&#x7684;&#x72B6;&#x6001;&#x3002;</p>
<pre><code class="lang-bash">$ kubectl get pods <span class="hljs-_">-l</span> k8s-app=filebeat-test
NAME READY STATUS RESTARTS AGE
filebeat-test-2365467882-4zwx8 2/2 Running 0 33d
filebeat-test-2365467882-rqskl 2/2 Running 0 33d
filebeat-test-2470325483-6vjbw 1/2 ImagePullBackOff 0 4m
filebeat-test-2470325483-gc14k 1/2 ImagePullBackOff 0 4m
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6709;&#x4E24;&#x4E2A;pod&#x6B63;&#x5728;&#x62C9;&#x53D6;image&#x3002;</p>
<p><strong>&#x8FD8;&#x539F;&#x4E3A;&#x539F;&#x5148;&#x7684;&#x955C;&#x50CF;</strong></p>
<p>&#x5C06;image&#x8BBE;&#x7F6E;&#x4E3A;&#x539F;&#x6765;&#x7684;&#x955C;&#x50CF;&#x3002;</p>
<pre><code class="lang-bash">$ ./update-deployment-image -deployment filebeat-test -image sz-pg-oam-docker-hub-001.tendcloud.com/library/analytics-docker-test:Build_8
Found deployment
name -&gt; filebeat-test
Old image -&gt; sz-pg-oam-docker-hub-001.tendcloud.com/library/analytics-docker-test:Build_9
New image -&gt; sz-pg-oam-docker-hub-001.tendcloud.com/library/analytics-docker-test:Build_8
</code></pre>
<p>&#x73B0;&#x5728;&#x518D;&#x67E5;&#x770B;Deployment&#x7684;&#x72B6;&#x6001;&#x3002;</p>
<pre><code class="lang-bash">$ kubectl describe deployment filebeat-test
Name: filebeat-test
Namespace: default
CreationTimestamp: Fri, 19 May 2017 15:12:28 +0800
Labels: k8s-app=filebeat-test
Selector: k8s-app=filebeat-test
Replicas: 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
M<span class="hljs-keyword">in</span>ReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: &lt;none&gt;
NewReplicaSet: filebeat-test-2365467882 (3/3 replicas created)
Events:
FirstSeen LastSeen Count From SubObjectPath Type ReasoMessage
--------- -------- ----- ---- ------------- -------- ------------
2h 8m 3 {deployment-controller } Normal ScalingReplicaSet Scaled down replica <span class="hljs-built_in">set</span> filebeat-test-2365467882 to 2
8m 8m 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica <span class="hljs-built_in">set</span> filebeat-test-2470325483 to 1
8m 8m 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica <span class="hljs-built_in">set</span> filebeat-test-2470325483 to 2
2h 1m 3 {deployment-controller } Normal ScalingReplicaSet Scaled up replica <span class="hljs-built_in">set</span> filebeat-test-2365467882 to 3
1m 1m 1 {deployment-controller } Normal ScalingReplicaSet Scaled down replica <span class="hljs-built_in">set</span> filebeat-test-2470325483 to 0
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;available&#x7684;replica&#x4E2A;&#x6570;&#x6062;&#x590D;&#x6210;3&#x4E86;&#x3002;</p>
<p>&#x5176;&#x5B9E;&#x5728;&#x4F7F;&#x7528;&#x8BE5;&#x547D;&#x4EE4;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x901A;&#x8FC7;kubernetes dashboard&#x7684;&#x9875;&#x9762;&#x4E0A;&#x67E5;&#x770B;Deployment&#x7684;&#x72B6;&#x6001;&#x66F4;&#x76F4;&#x89C2;&#xFF0C;&#x66F4;&#x52A0;&#x65B9;&#x4FBF;&#x6545;&#x969C;&#x6392;&#x67E5;&#x3002;</p>
<figure id="fig1.6.3.1"><img src="../images/kubernetes-client-go-sample-update.jpg" alt="&#x4F7F;&#x7528;kubernetes dashboard&#x8FDB;&#x884C;&#x6545;&#x969C;&#x6392;&#x67E5;"><figcaption>Figure: &#x4F7F;&#x7528;kubernetes dashboard&#x8FDB;&#x884C;&#x6545;&#x969C;&#x6392;&#x67E5;</figcaption></figure>
<p>&#x8FD9;&#x4E5F;&#x662F;dashboard&#x6700;&#x5927;&#x7684;&#x4F18;&#x52BF;&#xFF0C;&#x7B80;&#x5355;&#x3001;&#x76F4;&#x63A5;&#x3001;&#x9AD8;&#x6548;&#x3002; </p>
<footer class="page-footer-ex"> <span class="page-footer-ex-copyright">for GitBook</span>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="page-footer-ex-footer-update">update
2017-08-21 18:23:34
</span></footer>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="testing.html" class="navigation navigation-prev " aria-label="Previous page: 6.2 单元测试和集成测试">
<i class="fa fa-angle-left"></i>
</a>
<a href="contribute.html" class="navigation navigation-next " aria-label="Next page: 6.4 社区贡献">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
2017-08-29 12:23:54 +08:00
gitbook.page.hasChanged({"page":{"title":"6.3 client-go示例","level":"1.6.3","depth":2,"next":{"title":"6.4 社区贡献","level":"1.6.4","depth":2,"path":"develop/contribute.md","ref":"develop/contribute.md","articles":[]},"previous":{"title":"6.2 单元测试和集成测试","level":"1.6.2","depth":2,"path":"develop/testing.md","ref":"develop/testing.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github","codesnippet","splitter","page-toc-button","image-captions","page-footer-ex","editlink","-lunr","-search","search-plus"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/rootsongjc/kubernetes-handbook"},"editlink":{"label":"编辑本页","multilingual":false,"base":"https://github.com/rootsongjc/kubernetes-handbook/blob/master/"},"page-footer-ex":{"copyright":"for GitBook","update_format":"YYYY-MM-DD HH:mm:ss","update_label":"update"},"splitter":{},"codesnippet":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"page-toc-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{},"image-captions":{"variable_name":"_pictures"}},"page-footer-ex":{"copyright":"Jimmy Song","update_label":"最后更新:","update_format":"YYYY-MM-DD HH:mm:ss"},"theme":"default","author":"Jimmy Song","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"_pictures":[{"backlink":"index.html#fig1.1.1","level":"1.1","list_caption":"Figure: wercker status","alt":"wercker status","nro":1,"url":"https://app.wercker.com/status/b8b69e593784e17ddcfd1286adfd8f3c/s/master","index":1,"caption_template":"Figure: _CAPTION_","label":"wercker status","attributes":{},"title":"wercker status","skip":false,"key":"1.1.1"},{"backlink":"concepts/index.html#fig1.2.1","level":"1.2","list_caption":"Figure: Borg架构","alt":"Borg架构","nro":2,"url":"../images/borg.png","index":1,"caption_template":"Figure: _CAPTION_","label":"Borg架构","attributes":{},"skip":false,"key":"1.2.1"},{"backlink":"concepts/index.html#fig1.2.2","level":"1.2","list_caption":"Figure: Kubernetes架构","alt":"Kubernetes架构","nro":3,"url":"../images/architecture.png","index":2,"caption_template":"Figure: _CAPTION_","label":"Kubernetes架构","attributes":{},"skip":false,"key":"1.2.2"},{"backlink":"concepts/index.html#fig1.2.3","level":"1.2","list_caption":"Figure: kubernetes整体架构示意图","alt":"kubernetes整体架构示意图","nro":4,"url":"../images/kubernetes-whole-arch.png","index":3,"caption_template":"Figure: _CAPTION_","label":"kubernetes整体架构示意图","attributes":{},"skip":false,"key":"1.2.3"},{"backlink":"concepts/index.html#fig1.2.4","level":"1.2","list_caption":"Figure: Kubernetes master架构示意图","alt":"Kubernetes master架构示意图","nro":5,"url":"../images/kubernetes-master-arch.png","index":4,"caption_template":"Figure: _CAPTION_","label":"Kubernetes master架构示意图","attributes":{},"skip":false,"key":"1.2.4"},{"backlink":"concepts/index.html#fig1.2.5","level":"1.2","list_caption":"Figure: kubernetes node架构示意图","alt":"kubernetes node架构示意图","nro":6,"url":"../images/kubernetes-node-arch.png","index":5,"caption_template":"Figure: _CAPTION_","label":"kubernetes node架构示意图","attributes":{},"skip":false,"key":"1.2.5"},{"backlink":"concepts/index.html#fig1.2.6","level":"1.2","list_caption":"Figure: Kubernetes分层架构示意图","alt
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
<script src="../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-editlink/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
<script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>