0%

kubectl

kubectl命令行工具可让您控制kubernetes集群。

安装kubectl

linux

配置k8s的yum源

1
2
3
4
5
6
7
8
9
10
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

安装kubectl

1
yum install -y kubectl

验证安装

1
kubectl version

mac

1
brew install kubectl

获取kubectl config文件

1
2
master节点执行
cat /etc/kubernetes/admin.conf

输出内容如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJeE1Ea3dNakF3TXpjeE9Gb1hEVE14TURnek1UQXdNemN4T0Zvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTG50Cmw0Y3pqSnY3NkhYcG9OUlN0ZzBhWWZaei9nSUtHcHVMNkRSODNaSmMzL2c5YWdkbVdyNm81Rk5Ua3plK3gxRXEKMEs1bkRJUkVKVFNhUzlaTkZRd3l5RUpNOTdxTVQ4Njk2aFBTSVhMb1AvSGRtckhCczRaLzRUYmFETXRkNnhScwpKMFVNMU9DM3d5QkJzeVo2d1pTcEVFZ2V5Uzh4WDFZbWo0WWYzVVUvUnc4bDBrODdqOGwvU2xVZlJuL0lRRXZsCkNENk5QNVo2RWpUbTJtMm9CbzFEbHliTzN0TFQ1TzNRRWNFdE9NYVZhVmZXbkc3ODZUSGpabnl0eHJ1Tjdxb2kKd1BoRFIwVjBIa0FvelVpTlpjdThhUitDUER6ck5JbHNBZVppWWYwRzNqUGQ5N1htQnRRVXlOekltaWVZNUVkRgo2ZXhUQXlSc0RjT0RwZmJWcDc4Q0F3RUFBYU5DTUVBd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0hRWURWUjBPQkJZRUZMTE5pcTBvSUdNV1NxejZTTVk5b2NacUdFTThNQTBHQ1NxR1NJYjMKRFFFQkN3VUFBNElCQVFCeko1YkRjdVYyMW9nOGpTNmJQSzZKWHcydWJqYWpTakFtUUZMRDErZ0lWRjBUeENmdwo4amszbnpIREFmaFUwcmV4eHR4Wkd5OEM1cE4zZytNS25zNjFQM1lZQ2VwSWs0R3lWeDNhU0NkZUpWR1BnUkRrCnljOGVFa1ZRcWpGanoxNk12cFh5NGhUYTROMW1KOEZpTk5KSHVKQmdhbmtVTkYxNmlaUUtXZ1c5R3BFeGJCa3gKZ2tyN0xtL0hCYjkxajJsOXAwd3N4OS9lRS8zVlJFN0gwM29tYVZEZjd4M0sxRXNwVm5xYjRVRWg2dzBOSGVTOQphUDI5NmU3dE5hNXY2c0IrQW5XQzV1ZVJJNzA2dm1mRUJBSk4wOFRPOGlsTWpRWG1kM3I2TXZWajNCNDlybTZ2Cll5UXRienJyVUVZSFhrWTJuWHZZZGNiU0VFZXROR3FsdEFyagotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server: https://apiserver.dev.zhaohongye.com:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURJVENDQWdtZ0F3SUJBZ0lJRVFjRTFQREpXYVF3RFFZSktvWklodmNO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G1Oc1FLQmdIOGJwNVJ0bTNEeW9HTUFIRjBxZHREVkkyRDNRdmFBNUxVaG4vVVcKRWRLNGVLeHYrVmNMOVAwUW1oNU9ET1U0aUhJU29pM1dMZStqV2l1MnJ0U1dRUDNsTm5mL0pCYmdPUUp1YmdjQwpBTDJyYlJ0bS9QcVNUczZpN1p5REI3dXVOOXBmQTZGOG9KdHpYaGpveFZmNEZ4U0g2TFJ1YXBYYjd2T3FReVNECnk1bGJBb0dBY3lSQkJUaDBNWDdXR3F3QzZ6V1pJbUtLUnMxY1JJTHlkQUdCUVdmZWh0MTlZanhZaFVTVXBIQ2IKTyswRU5STjlwTDg4U29kZVN5dHlXek5LaE5Qdk5YY2hkWGs2dmlYMjUySk1IQTVJZFRSZGwyUnNKbmRFNHpNKwpVUHFLNjZjUUZsTEhPdGhIVE8xeUlOTCtMdFkyZ0dDZDNxRTBHdmlBQU01QUFwUjh3Tjg9Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

配置kubectl客户端

1
vim ~/.kube/config

将上面获取的config复制到此处

验证结果

1
2
kubectl get nodes
kubectl get pods -n kube-system

kubectl 参考文档推荐

https://kubernetes.io/docs/reference/kubectl/overview/