青年IT男

个人从事金融行业,就职过易极付、思建科技等重庆一流技术团队,目前就职于某网约车平台负责整个支付系统建设。自身对金融行业有强烈的爱好。同时也实践大数据、数据存储、自动化集成和部署、分布式微服务、人工智能等领域。

CentOS7环境安装Kubernetes四部曲之四:安装kubectl工具

CentOS7环境安装Kubernetes四部曲之四:安装kubectl工具

本文是《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机器上执行以下命令下载:

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

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移动到可以全局访问的目录下:
mv ./kubectl /usr/local/bin/kubectl

3. 在linux机器的任意目录都能执行kubectl,直接输入kubectl然后回车,可以看到如下信息:

kubectl controls the Kubernetes cluster manager. 

Find more information at https://github.com/kubernetes/kubernetes.

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

Basic Commands (Intermediate):
  get            Display one or many resources
  explain        Documentation of resources
  edit           Edit a resource on the server
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

工具已经准备好,接下来我们把配置做好,使得kubectl可以连接到K8S上执行命令;

配置参数

  1. 在rancher的管理页面上,点击下图红框1中的”CLI”,在出现的页面中点击红框2中的”生成配置“: 
    1.png

  2. 如下图,点击红框中的”复制到剪切板“,将按钮上方的配置信息复制下来: 
    2.png

  3. 在linux机器上,在家目录创建.kube文件夹,命令如下:

mkdir ~/.kube

4. 在.kube文件夹下新建一个名为config的文件,把刚才复制的配置信息粘贴进来;

至此,kubectl工具的安装配置就完成了,输入命令get service -a -o wide –all-namespaces试试,可以看到当前的服务列表:

[root@v-test-test ~]# 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       <none>        443/TCP          20h       <none>
default       tomcat001              NodePort    10.43.243.24    <none>        8080:30018/TCP   4h        name=tomcat001
kube-system   heapster               ClusterIP   10.43.157.242   <none>        80/TCP           20h       k8s-app=heapster
kube-system   kube-dns               ClusterIP   10.43.0.10      <none>        53/UDP,53/TCP    20h       k8s-app=kube-dns
kube-system   kubernetes-dashboard   ClusterIP   10.43.199.86    <none>        80/TCP           20h       k8s-app=kubernetes-dashboard
kube-system   monitoring-grafana     ClusterIP   10.43.112.198   <none>        80/TCP           20h       k8s-app=grafana
kube-system   monitoring-influxdb    ClusterIP   10.43.200.65    <none>        8086/TCP         20h       k8s-app=influxdb
kube-system   tiller-deploy          ClusterIP   10.43.28.153    <none>        44134/TCP        20h       app=helm,name=tiller

至此,《CentOS7环境安装Kubernetes四部曲》就全部结束了,希望能够帮助您快速搭建环境,然后一起开始愉快的K8S之旅;

备注:kubectl如何管理切换多个cluster
“`
1.首先合并连接到的cluster配置到~/.kube/config文件
2.kubectl切换上下文kubectl config use-context attendance-k8s(attendance-k8s为集群上下文)
3.查看连接集群情况kubectl cluster-info

下面是我的~/.kube/config内容

apiVersion: v1
clusters:
– cluster:
insecure-skip-tls-verify: true
server: https://192.168.3.29:8080/r/projects/1a7/kubernetes:6443
name: attendance-k8s
– cluster:
server: http://192.168.3.25:8080
name: kubernetes
– cluster:
certificate-authority: /Users/liyong/.minikube/ca.crt
server: https://192.168.99.100:8443
name: minikube
– cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM3akNDQWRhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFvTVJJd0VBWURWUVFLRXdsMGFHVXQKY21GdVkyZ3hFakFRQmdOVkJBTVRDV05oZEhSc1pTMWpZVEFlRncweE9EQTJNamN3TnpFME1qQmFGdzB5T0RBMgpNalF3TnpFME1qQmFNQ2d4RWpBUUJnTlZCQW9UQ1hSb1pTMXlZVzVqYURFU01CQUdBMVVFQXhNSlkyRjBkR3hsCkxXTmhNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXRQOFpPQTVkYUZUTktwZVMKSVVyVVMrTVlReWR5TUpmdHRvY2JoakVjaGMvTk5QY292N3krUkxaMFRGVVFFKzBGZkN4TVpscHVaVGxHbGZ0bwp1ZTQ4anBINWhUUWw1ZmJ5bkFhZVJXa1BLOTAzRU9WTkJFVVBoMWlBMXBPVy95QmN0dk9WSFQyYnlXL2pIUEF1CitOVDh5Y29BeDlJZjlXWGtGVzh0b21peFREeG9YMGdVN3h4ZTlXM1QyY1U1RUVVamUyZzFkT1ljcU45dE83TlcKZ3k2K0RRMlBveGZaUk1IYmVKN3FmSXdnSE5VbncwaGF2VHI0alZyV0syTEg0bHBEa3lsTnF6dUtkblphQnVoQwpsbmk2VE9jZkxadjNwdWRMRWRtUU5FeTVvR3Q4dUlDZml4TzVadWszUlc2UlUwVzdVeGRQR0ZzUis3cm1mdlU4CmFhZTlWd0lEQVFBQm95TXdJVEFPQmdOVkhROEJBZjhFQkFNQ0FxUXdEd1lEVlIwVEFRSC9CQVV3QXdFQi96QU4KQmdrcWhraUc5dzBCQVFzRkFBT0NBUUVBS3NpYlpJMUtFQllaRnlXNjZab2lwZjYzNHpYWnY4K3ZUWmhuanh4NgpUUGVZSkJ0YURMZ0xtOE50Q056eCttS01HMy8xeFF5bEVUNE1iL3JIbTE3WUpsZ2N1TUR6eDg2Q1NsRENQOEM0CnFGWTROOTQrZjVmOWdjK05nLzZ5d0dxTVc4eFZ5eUQ2UHNqbm42WmtjcWp6aXdrWU1kQXIza3RocENDZkNJRUEKV044ei9XTUVScUJvamdheC9GS0pzdklGUWRPOThqdGkrYmFCZlRYMVdwQlE5QTQ3aHFUNFFkaDcrcHA3b3lHYQpPM1FjaksyUWFGbkhHL3gyZWV6Tm9HSDN2THBDelVIZGZPK3ZoR1JHTk1aK216WVNMYVVjdFJNYUZaWEJabDFRCm83dHZqMkhUTFYyY05QUGdmMU1KSkozZUg3TVdTbldSb3RkSlZzeUNiNkloMkE9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://rancher.huwo.io/k8s/clusters/c-qlrzb
name: thanos
contexts:
– context:
cluster: attendance-k8s
user: attendance-k8s
name: attendance-k8s
– context:
cluster: kubernetes
user: “”
name: kubernetes
– context:
cluster: minikube
user: minikube
name: minikube
– context:
cluster: thanos
user: user-7qtt9
name: thanos
current-context: attendance-k8s
kind: Config
preferences: {}
users:
– name: attendance-k8s
user:
token: QmFzaWMgTjBZNVFqSkVNRUpFTVRVNVJqaERNVVZFTnpFNmRXMVNXbUZ4UldWVFIxbG9USG8xZEROMU5VRkZjVGgzVkUxaU5FZDZVRkZPVUV0S2NWQlVjUT09
– name: minikube
user:
as-user-extra: {}
client-certificate: /Users/liyong/.minikube/client.crt
client-key: /Users/liyong/.minikube/client.key
– name: user-7qtt9
user:
token: kubeconfig-user-7qtt9:6zx46kskfnmdppsm8gz7zfldh4fl9frdk8dbzjqgkb6fqnjp8vn6hs

“`
转载地址:https://blog.csdn.net/boling_cavalry/article/details/79624655

0
1028826685@qq.com