通过 kubectl 连接 公有云Kubernetes 集群

发布时间:2020-07-09 00:03:41 作者:GoDevops
来源:网络 阅读:1198

    kubectl是Kubernetes集群的命令行工具,通过kubectl能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署,不仅可以部署master、node节点,还可以部署到其他机器上管理Kubernetes。

官方文档:https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-macos

一、软件安装

1、使用二机制包在macOS安装

1、下载软件(最新版本)
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s  

2、赋予执行权限
chmod +x ./kubectl
3、将其拷贝到已经添加到环境变量的某个目录中 方便后期执行
sudo mv ./kubectl /usr/local/bin/kubectl
4、执行命令测试
kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.0", GitCommit:"2bd9643cee5b3b3a5ecbd3af49d09018f0773c77", GitTreeState:"clean", BuildDate:"2019-09-18T14:36:53Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"14+", GitVersion:"v1.14.6-aliyun.1", GitCommit:"a4182a8", GitTreeState:"", BuildDate:"2019-08-27T06:03:13Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}

2、使用二进制包在CentOS 7.x上面安装

1、下载软件

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s  
指定版本
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.13.5/bin/linux/amd64/kubectl 
如果您要下载最新版本的安装包,使用如下命令即可: 仅需将v1.13.5替换为$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)即可

2、赋予执行权限
chmod +x ./kubectl
3、将其拷贝到已经添加到环境变量的某个目录中 方便后期执行
sudo mv ./kubectl /usr/local/bin/kubectl
4、测试
[root@izuf6beunt2a2ki2nqvphuz ~]# kubectl  version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.0", GitCommit:"2bd9643cee5b3b3a5ecbd3af49d09018f0773c77", GitTreeState:"clean", BuildDate:"2019-09-18T14:36:53Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"14+", GitVersion:"v1.14.6-aliyun.1", GitCommit:"a4182a8", GitTreeState:"", BuildDate:"2019-08-27T06:03:13Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}

二、配置其能够访问Kubernetes

   在阿里云上面创建好Kubernetes集群后,在集群管理里面基本信息的下面部分可以看到通过 kubectl 连接 Kubernetes 集群 (通过 CloudShell 管理集群)的相关指导文档,根据其文档配置就好了,分为内网和外网情况,在阿里云和node节点同一VPC的机器可以视为内网,而访问SLB(Kubernetes集群提供负载均衡)外网IP视为外网访问,阿里云很贴心的给出了2份配置文件。

通过 kubectl 连接 公有云Kubernetes 集群

 在第一步里面CentOS 7.X 配置的为内网,使用内网相关的配置文件

通过 kubectl 连接 公有云Kubernetes 集群

在该机器的root账户家目录/.kube/config文件并将相关的配置复制到里面,

apiVersion: v1
clusters:
- cluster:
    server: https://10.101.175.223:6443
    certificate-authority-data: CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURHakNDQWdLZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREErTVNjd0ZBWURWUVFLRXcxaGJHbGkKWVdKaElHTnNiM1ZrTUE4R0ExVUVDaE1JYUdGdVozcG9iM1V4RXpBUkJnTlZCQU1UQ210MVltVnlibVYwWlhNdwpIaGNOTVRrd09URTRNRFl6TURFeVdoY05Namt3T1RFMU1EWXpNREV5V2pBK01TY3dGQVlEVlFRS0V3MWhiR2xpCllXSmhJR05zYjNWa01BOEdBMVVFQ2hNSWFHRnVaM3BvYjNVeEV6QVJCZ05WQkFNVENtdDFZbVZ5Ym1WMFpYTXcKZ2dFaU1BMEdDU3FHU0liM0RRRUJBUVVBQTRJQkR3QXdnZ0VLQW9JQkFRQzI3UFM5Z0g1UW00UDBDQWpMOVpvagpYTWNZQjhYbUJ0R1pUM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kubernetes-admin-cfd09da28dcfb4ad8856a5e57ab43f221
current-context: kubernetes-admin-cfd09da28dcfb4ad8856a5e57ab43f221
kind: Config
preferences: {}
users:
- name: "kubernetes-admin"
  user:
    client-certificate-data: CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN6VENDQWphZ0F3SUJBZ0lERVpLTk1BMEdDU3FHU0liM0RRRUJDd1VBTUdveEtqQW9CZ05WQkFvVElXTm0KWkRBNVpHRXlPR1JqWm1JMFlXUTRPRFUyWVRWbE5UZGhZalF6WmpJeU1URVFNQTRHQTFVRUN4TUhaR1ZtWVhWcwpkREVxTUNnR0ExVUVBeE1oWTJaa01EbGtZVEk0WkdObVlqUmhaRGc0TlRaaE5XVTFOMkZpTkRObU1qSXhNQjRYCkRURTVNRGt4T0RFeU1UTXdNRYRFRJeU1Ea3hOekV5TVRnek5Wb3dQekVYTUJVR0ExVUVDaE1PYzNsemRHVnQKT20xaGMzUmxjbk14Q1RBSEJnTlZCQXNUQURFWk1CY0dBMVVFQXhNUWEzVmlaWEp1WlhSbGN5MWhaRzFwYmpDQgpuekFOQmdrcWhraUc5dzBCQVFFRkFBT0JqUUF3Z1lrQ2dZRUExVVRjS2d2Tjk2dzFZUkhvallMZHMzRnJ6Sm8wCjdXbFVsdEU3cVdVZ2ZsdTZIUEpMSnh4dFVsalJKaGNyYkxJTnp6K1kwVVRDSFJEMDRWZ2VXRWc2R21tOWRFWU0KZUI0TEsvNVp5ejF3Kzd1R0c5cC9pTi8vY1pxZUNBdU9kcldIOXpQV3ZsazJMR2Fvc0JSV2hDT2tDeUx1Sy9DaApxcEM5Ri9lbENjTXAxTDBDQXdFQUFhT0JxekNCcURBT0JnTlZIUThCQWY4RUJBTUNCNEF3RXdZRFZSMGxCQXd3CkNnWUlLd1lCQlFVSEF3SXdEQVlEVlIwVEFRSC9CQUl3QURBOEJnZ3JCZ0VGQlFjQkFRUXdNQzR3TEFZSUt3WUIKQlFVSE1BR0dJR2gwZEhBNkx5OWpa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0SGdzci9sbkxQWEQ3dTRZYjJuK0kKMy85eG1wNElDNDUydFlmM005YStXVFlzWnFpd0ZGYUVJNlFMSXU0cjhLR3FrTDBYOTZVSnd5blV2UUlEQVFBQgpBb0dBSE5HcFRxVTV0L1kxWUdDakpEeEM2VFN5ZEh2Q0czTFlVMmtDdnVsRGNzL0NYcGIvUU5HMTdpTENZSHRSCkN1Y2twcFRaZWEzbzdUU1ZZcjRkbTZCbStWdVpHM3NDaGdZSWNhQVVXOG1PQThYTEFTZDk5QUZkQmIzY0YxRDgKTTZnZnFpNW1VM2s1NUZrTjhSWkxtN1RlNWtsYlNydU0vQ2tOMUF2TW9IdmhTZ0VDUVFEbTVBcW9vbWp0VytjQwpLdlBkcHl1REgvM3VIaEFvSmZXeGJsUmRUQm5iQmNHbC91aWVPbUhudjdIMXFwejRXLy94cUNXWm5XT1VvMFU1CnZYeDVadWV0QWtFQTdIWTdBM1NxenlPWjF6MkhwcnRTVHFLQlNvY1FyRUhPWXNZWmhJZ1JWdFFCendyall5NjgKeERoN2tiTjZBS2V4T2dQL3FyMUtSMjdISWNtWW11R0RVUUpBWEFCT0J2Z0FHMlJkWGszRE1uVWtMM0dWKzBYYgpaa1FKN0pOcjVta21mRkdFNGlINml1SDBsU1diUWFxa05ZcGpKK1c2WmNXREluMFVYbmVVbThOWFdRSkJBS2szCjR2dVNIanFtUzQzUkpxYVhIMHZPMTUreUVLaDFYM0VvTzFLdzNWZTUyS0dHTlR2aFVvMUpyb1NTWUJqZjZPQ1AKeEFNMysxUXJYcTQ3U3M1cjNFRUNRRlBjYUkvTTVYdkZMWkJablRlSjFYd2w4Y3dxVkY3TlMzNGlrMUtjVWxZNQpveDFLOCtEK1FBV0MxNnM3VUNSN1k2VXhEZDIwajNNclZnUTN6S3JtN1VNPQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=

执行相关命令验证

[root@izuf6beunt2a2ki2nqvphuz ~]# kubectl  get  nodes
NAME                         STATUS   ROLES    AGE   VERSION
cn-shanghai.10.101.175.227   Ready    <none>   33h   v1.14.6-aliyun.1
[root@izuf6beunt2a2ki2nqvphuz ~]# kubectl  get  pods
NAME                        READY   STATUS    RESTARTS   AGE
dashbord-7fbdc69845-fx76j   1/1     Running   0          26h
mysql-0                     1/1     Running   0          24h
mysql-1                     0/1     Pending   0          24h
redis-0                     1/1     Running   0          24h
[root@izuf6beunt2a2ki2nqvphuz ~]# kubectl  get  namespaces
NAME              STATUS   AGE
ahas              Active   27h
catalog           Active   24h
default           Active   33h
kube-node-lease   Active   33h
kube-public       Active   33h
kube-system       Active   33h
nginx-web         Active   32h

在MAC上配置阿里云外网访问是直接vi  $HOME/.kube/config 保存时会报错,可以选择先创建 $HOME/.kube/目录,然后填写配置文件就不会再报错,下面是配置后的验证过程,

kubectl  get pods
NAME                        READY   STATUS    RESTARTS   AGE
dashbord-7fbdc69845-fx76j   1/1     Running   0          27h
mysql-0                     1/1     Running   0          25h
mysql-1                     0/1     Pending   0          24h
redis-0                     1/1     Running   0          25h

kubectl  get  namespaces
NAME              STATUS   AGE
ahas              Active   27h
catalog           Active   24h
default           Active   33h
kube-node-lease   Active   33h
kube-public       Active   33h
kube-system       Active   33h
nginx-web         Active   32h

通过 kubectl 连接 公有云Kubernetes 集群

很明显的发现外网的执行要比内网要慢很多,这个主要取决于公网的带宽和网络质量,kubectl相关的命令可以参考官方文档和公有云产品文档

官方文档:https://kubernetes.io/docs/reference/kubectl/kubectl/

阿里云 https://help.aliyun.com/document_detail/93080.html?spm=a2c4g.11174283.6.964.3b972ceeYhCcoS

华为云:https://support.huaweicloud.com/usermanual-cce/cce_01_0023.html

Ucloud公有云:https://docs.ucloud.cn/compute/uk8s/manageviakubectl/intro_of_kubectl


推荐阅读:
  1. TungstenFabric+K8s轻松上手丨通过Kuber
  2. TF+K8s轻松上手丨通过Kubernetes Ingres

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

k8s kubernetes kubectl

上一篇:python抓取网站提示错误ssl.SSLCertVerificationError处理

下一篇:多年后再回头看C语言对未来职业的重大意义

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》