It's our wits that make us men.

Kubernetes (03) kubectl

Posted on By Freud Kang

什么是Kubectl

kubectl用于运行Kubernetes集群命令的管理工具。

安装Kubectl

需要翻墙!需要翻墙!需要翻墙!重要的事情说三遍!

macos

使用brew安装

brew install kubernetes-cli

linux(centos)

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl

更多安装方式参考官网Install and Set Up kubectl

kube config配置

有三种方式可以做kube config的配置。

  • 修改~/.kube/config文件
[root@localhost root]#mkdir ~/.kube
[root@localhost root]touch ~/.kube/config
#将kube config文件内容写入此文件
[root@localhost root]vi ~/.kube/config
#验证kube config是否生效
[root@localhost root]kubectl version
  • 在命令中指定kubeconfig
#配置kube config并验证kube config是否生效
[root@localhost root]kubectl version --kubeconfig="/root/kube_config"
  • 设置 KUBECONFIG 环境变量
[root@localhost root]export  KUBECONFIG=$KUBECONFIG:/root/kube_config
#验证kube config是否生效
[root@localhost root]kubectl version

常用命令

  • kubectl create : 创建组件信息
[root@k8s-master ~]# kubectl create namespace ci
namespace "ci" created
[root@k8s-master ~]# kubectl create -f test-configmap.yaml
configmap "test-configmap" created
test-configmap.yaml
-------------------
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-configmap
  namespace: ci
data:
  name: hifreud
  • kubectl get : 获取组件详细信息
[root@k8s-master ~]# kubectl get namespaces
NAME              STATUS    AGE
default           Active    108d
heptio-sonobuoy   Active    104d
kube-public       Active    108d
kube-system       Active    108d
ci                Active    8m
[root@k8s-master ~]# kubectl get configmap test-configmap -n ci
NAME             DATA      AGE
test-configmap   1         1m
  • kubectl edit : 修改组件信息
# 保存使用w,退出使用q,强制使用!
[root@k8s-master ~]# kubectl edit configmap test-configmap -n ci
[root@k8s-master ~]# kubectl edit -f test-configmap.yaml
  • kubectl delete : 删除组件
[root@k8s-master ~]# kubectl delete configmap test-configmap -n ci
configmap "test-configmap" deleted
[root@k8s-master ~]# kubectl delete -f test-configmap.yaml
  • kubectl cluster-info : 查看集群状态
[root@k8s-master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.75.139:6443
Heapster is running at https://192.168.75.139:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://192.168.75.139:6443/api/v1/namespaces/kube-system/services/kube-dns/proxy
monitoring-grafana is running at https://192.168.75.139:6443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
monitoring-influxdb is running at https://192.168.75.139:6443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
  • kubectl describe : 查看组件运行期状态
[root@k8s-master ~]# kubectl describe configmap test-configmap -n ci
Name:         test-configmap
Namespace:    ci
Labels:       <none>
Annotations:  <none>

Data
====
name:
----
hifreud
Events:  <none>
  • kubectl logs : 查看Pod的运行日志,跟docker log比较类似
[root@k8s-master ~]# kubectl get pod -n ci
NAME                           READY     STATUS    RESTARTS   AGE
jmeter-test-7bb4b79bd6-8vvkb   0/1       Pending   0          89d
# 在使用kubectl logs的时候不需要指定组件类型,如果要持续打印日志信息可以添加 -f参数
[root@k8s-master ~]# kubectl logs jmeter-test-7bb4b79bd6-8vvkb -n ci
  • kubectl port-forward : 端口转发,对于外界无法直接访问的Pod,可以通过端口转发达到可以在集群外部直接访问Pod的效果。
#在本地机器执行,这样通过在本地机器访问localhost:7070就可以访问k8s集群的pod了
kubectl port-forward jmeter-test-7bb4b79bd6-8vvkb 7070:7070 

参考资料

Kubernetes kubectl 概述 : http://docs.kubernetes.org.cn/61.html

Overview of kubectl : https://kubernetes.io/docs/reference/kubectl/kubectl/

Install and Set Up kubectl : https://kubernetes.io/docs/tasks/tools/install-kubectl/