1. 程式人生 > >轉載CentOS7環境安裝kubectl工具

轉載CentOS7環境安裝kubectl工具

CentOS7環境安裝Kubernetes四部曲之四:安裝kubectl工具

 

轉自--https://blog.csdn.net/boling_cavalry/article/details/79624655

2018年03月20日 16:40:57

閱讀數:2117

本文是《CentOS7環境安裝Kubernetes四部曲》系列的終篇,經歷了前三篇文章的實戰,我們用rancher搭建了具備master和node的完整K8S環境,但是目前還不能通過kubectl工具在K8S環境做更多的操作,本章我們來實戰安裝和配置kubectl工具;

原文地址:http://blog.csdn.net/boling_cavalry/article/details/79624655

前三篇文章連結

  1. 《CentOS7環境安裝Kubernetes四部曲之一:標準化機器準備》;
  2. 《CentOS7環境安裝Kubernetes四部曲之二:配置模板和安裝master》;
  3. 《CentOS7環境安裝Kubernetes四部曲之三:新增節點》;

kubectl工具安裝在哪裡?

kubectl是個客戶端工具,可以安裝在任意一臺Linux、windows或者Mac電腦上,只要這個電腦能夠連線master節點,本次實戰我們將kubectl安裝在一臺全新的CentOS7機器上;

下載kubectl工具

有三種下載方式,您可以選擇其中任意一種: 
1. 在linux機器上執行以下命令下載:

<span style="color:#000000"><code class="language-shell">curl -LO https://storage<span style="color:#009900">.googleapis</span><span style="color:#009900">.com</span>/kubernetes-release/release/$(curl -s https://storage<span style="color:#009900">.googleapis</span><span style="color:#009900">.com</span>/kubernetes-release/release/stable<span style="color:#009900">.txt</span>)/bin/linux/amd64/kubectl</code></span>
  • 1

2.在我的GitHub下載,地址是:https://github.com/zq2599/blog_demos/blob/master/k8s_tools/kubectl/linux/kubectl.zip,在這個頁面點選”download”按鈕即可下載,下載後記得解壓; 
3. 在csdn下載,地址:https://download.csdn.net/download/boling_cavalry/10297309

設定工具

  1. kubectl檔案下載到linux機器後,執行命令chmod +x ./kubectl,給檔案可執行許可權;
  2. 執行以下命令,將kubectl移動到可以全域性訪問的目錄下:
<span style="color:#000000"><code class="language-shell">mv ./kubectl /usr/<span style="color:#000088">local</span>/bin/kubectl</code></span>
  • 1


3. 在linux機器的任意目錄都能執行kubectl,直接輸入kubectl然後回車,可以看到如下資訊:

<span style="color:#000000"><code class="language-shell">kubectl controls the Kubernetes cluster manager. 

Find more information <span style="color:#000088">at</span> <span style="color:#000088">https</span>://github.com/kubernetes/kubernetes.

Basic Commands (Beginner):
  <span style="color:#4f4f4f">create</span>         Create a resource <span style="color:#4f4f4f">from</span> a <span style="color:#4f4f4f">file</span> or <span style="color:#4f4f4f">from</span> <span style="color:#000088">stdin</span>.
  expose         Take a replication controller, service, deployment or pod and expose <span style="color:#000088">it</span> <span style="color:#000088">as</span> a <span style="color:#4f4f4f">new</span> Kubernetes Service
  run            Run a particular image <span style="color:#000088">on</span> <span style="color:#009900">the</span> <span style="color:#009900">cluster</span>
  <span style="color:#4f4f4f">set</span>            Set specific features <span style="color:#000088">on</span> <span style="color:#009900">objects</span>
  run-container  Run a particular image <span style="color:#000088">on</span> <span style="color:#009900">the</span> <span style="color:#009900">cluster</span>. <span style="color:#009900">This</span> <span style="color:#009900">command</span> <span style="color:#009900">is</span> <span style="color:#009900">deprecated</span>, <span style="color:#009900">use</span> <span style="color:#009900">"run"</span> <span style="color:#009900">instead</span>

Basic Commands (Intermediate):
  <span style="color:#4f4f4f">get</span>            Display <span style="color:#009900">one</span> or many resources
  explain        Documentation of resources
  edit           Edit a resource <span style="color:#000088">on</span> <span style="color:#009900">the</span> <span style="color:#009900">server</span>
  <span style="color:#4f4f4f">delete</span>         Delete resources <span style="color:#000088">by</span> filenames, <span style="color:#000088">stdin</span>, resources and names, or <span style="color:#000088">by</span> resources and label selector</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

工具已經準備好,接下來我們把配置做好,使得kubectl可以連線到K8S上執行命令;

配置引數

  1. 在rancher的管理頁面上,點選下圖紅框1中的”CLI”,在出現的頁面中點選紅框2中的”生成配置“: 
    這裡寫圖片描述
  2. 如下圖,點選紅框中的”複製到剪下板“,將按鈕上方的配置資訊複製下來: 
    這裡寫圖片描述
  3. 在linux機器上,在家目錄建立.kube資料夾,命令如下:
<span style="color:#000000"><code class="language-shell"><span style="color:#000088">mkdir</span> ~<span style="color:#008800">/.kube</span></code></span>
  • 1


4. 在.kube資料夾下新建一個名為config的檔案,把剛才複製的配置資訊貼上進來;

至此,kubectl工具的安裝配置就完成了,輸入命令get service -a -o wide –all-namespaces試試,可以看到當前的服務列表:

<span style="color:#000000"><code class="language-shell">[[email protected] ~]# kubectl get service -a -o wide --all-namespaces
NAMESPACE     NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE       SELECTOR
default       kubernetes             ClusterIP   10.43.0.1       <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        443/TCP          20h       <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>
default       tomcat001              NodePort    10.43.243.24    <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        8080:30018/TCP   4h        name=tomcat001
kube-system   heapster               ClusterIP   10.43.157.242   <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        80/TCP           20h       k8s-app=heapster
kube-system   kube-dns               ClusterIP   10.43.0.10      <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        53/UDP,53/TCP    20h       k8s-app=kube-dns
kube-system   kubernetes-dashboard   ClusterIP   10.43.199.86    <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        80/TCP           20h       k8s-app=kubernetes-dashboard
kube-system   monitoring-grafana     ClusterIP   10.43.112.198   <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        80/TCP           20h       k8s-app=grafana
kube-system   monitoring-influxdb    ClusterIP   10.43.200.65    <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        8086/TCP         20h       k8s-app=influxdb
kube-system   tiller-deploy          ClusterIP   10.43.28.153    <span style="color:#006666"><<span style="color:#4f4f4f">none</span>></span>        44134/TCP        20h       app=helm,name=tiller</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

至此,《CentOS7環境安裝Kubernetes四部曲》就全部結束了,希望能夠幫助您快速搭建環境,然後一起開始愉快的K8S之旅;