how-to-get-kubectl-bin.md
----------------------
```
@version    180902:1
@author     zhangxuhong <zhangxuhong@xitu.io>
```

# desc
------

如何获取 kubectl 二进制文件.


# main
------

- 下载

根据 kubernetes 文档 [https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl)  
我们得到 Centos 的 kubectl yum 源在: [https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64](https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64)  
我们打开 https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64/repodata/primary.xml 文件, 按照版本寻找我们要的 kubectl 下载地址.  

```
<package type="rpm">
<name>kubectl</name>
<arch>x86_64</arch>
<version epoch="0" ver="1.11.2" rel="0"/>
<checksum type="sha256" pkgid="YES">
a554c1728ecf79871b4d3e0fc797568e53149f4ed7ec7e437c949a02f197a1ab
</checksum>
<summary>
Command-line utility for interacting with a Kubernetes cluster.
</summary>
<description>
Command-line utility for interacting with a Kubernetes cluster.
</description>
<packager/>
<url>https://kubernetes.io</url>
<time file="0" build="1533926010"/>
<size package="7861358" installed="38728168" archive="38728420"/>
<location href="../../pool/a554c1728ecf79871b4d3e0fc797568e53149f4ed7ec7e437c949a02f197a1ab-kubectl-1.11.2-0.x86_64.rpm"/>
<format>
<rpm:license>ASL 2.0</rpm:license>
<rpm:vendor/>
<rpm:group>Unspecified</rpm:group>
<rpm:buildhost>1233039dde1f</rpm:buildhost>
<rpm:sourcerpm>kubelet-1.11.2-0.src.rpm</rpm:sourcerpm>
<rpm:header-range start="4424" end="8894"/>
<rpm:provides>
<rpm:entry name="kubectl" flags="EQ" epoch="0" ver="1.11.2" rel="0"/>
<rpm:entry name="kubectl(x86-64)" flags="EQ" epoch="0" ver="1.11.2" rel="0"/>
</rpm:provides>
<file>/usr/bin/kubectl</file>
</format>
</package>
```

可以看到 

```
<location href="../../pool/a554c1728ecf79871b4d3e0fc797568e53149f4ed7ec7e437c949a02f197a1ab-kubectl-1.11.2-0.x86_64.rpm"/>
```

即为下载地址.  我们拼接url得到下载地址为:  
https://packages.cloud.google.com/yum/pool/a554c1728ecf79871b4d3e0fc797568e53149f4ed7ec7e437c949a02f197a1ab-kubectl-1.11.2-0.x86_64.rpm  


- 解压 rpm 包.

下载 rpm 包后, 解压即可得到我们需要的 kubectl 二进制文件.  
解压需要 rpm2cpio 程序. 这个程序是 rpm 包自带的, 不需要安装.  

```
wget https://packages.cloud.google.com/yum/pool/a554c1728ecf79871b4d3e0fc797568e53149f4ed7ec7e437c949a02f197a1ab-kubectl-1.11.2-0.x86_64.rpm  
rpm2cpio a554c1728ecf79871b4d3e0fc797568e53149f4ed7ec7e437c949a02f197a1ab-kubectl-1.11.2-0.x86_64.rpm | cpio -idmv
```

解压后就是了.  