文章目录
kubectl安装以后,默认是没有命令补全,也就是不能使用 Tab 键来补全命令,可以通过安装以下 软件包来实现。
# yum -y install bash-completion
# source /usr/share/bash-completion/bash_completion
# source <(kubectl completion bash)
# echo "source <(kubectl completion bash)" >> ~/.bashrc
1. create 和 expose
创建资源
#创建一个deployment,并创建两个副本,这里使用的是我自己写的镜像
[root@k8s-master ~]# kubectl create deployment my-dep --image=lxr909626/httpd:v0.1 --replicas=2
#暴露端口号
[root@k8s-master ~]# kubectl expose deploy my-dep --port=80 --type=NodePort
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 33h
my-dep NodePort 10.103.225.228 <none> 80:30806/TCP 18s
nginx NodePort 10.102.195.230 <none> 80:31410/TCP 33h
2. get
查看资源
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5dbb7d6f78-9b97g 1/1 Running 0 31m
my-dep-5dbb7d6f78-bt28j 1/1 Running 0 31m
nginx-6799fc88d8-jgk25 1/1 Running 0 33h
[root@k8s-master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 33h
my-dep NodePort 10.103.225.228 <none> 80:30806/TCP 11m
nginx NodePort 10.102.195.230 <none> 80:31410/TCP 33h
[root@k8s-master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 2/2 2 2 31m
nginx 1/1 1 1 33h
//查看详细信息
[root@k8s-master ~]# kubectl get deployments -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
my-dep 2/2 2 2 32m httpd lxr909626/httpd:v0.1 app=my-dep
nginx 1/1 1 1 33h nginx nginx app=nginx
3. delete
删除资源
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5dbb7d6f78-5sjkv 1/1 Running 0 14m
my-dep-5dbb7d6f78-s94lq 1/1 Running 0 14m
nginx-6799fc88d8-jgk25 1/1 Running 0 33h
[root@k8s-master ~]# kubectl delete deployments/my-dep
deployment.apps "my-dep" deleted
[root@k8s-master ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 33h
4. describe
查看详细信息
[root@k8s-master ~]# kubectl describe deploy nginx
Name: nginx
Namespace: default
CreationTimestamp: Wed, 25 Aug 2021 11:48:06 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-6799fc88d8 (1/1 replicas created)
Events: <none>
5. exec
进入容器
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5dbb7d6f78-5sjkv 1/1 Running 0 14m
my-dep-5dbb7d6f78-s94lq 1/1 Running 0 14m
nginx-6799fc88d8-jgk25 1/1 Running 0 33h
[root@k8s-master ~]# kubectl exec -it pods/my-dep-5dbb7d6f78-5sjkv -- /bin/sh
/ # wget -O - -q my-dep
hello world
/ # wget -O - -q my-dep
hello world
/ # wget -O - -q my-dep
myapp v0
/ #
//可以实现负载均衡
6. get namespace
查看所有的名称空间
[root@k8s-master ~]# kubectl get namespace
NAME STATUS AGE
default Active 34h
kube-node-lease Active 34h
kube-public Active 34h
kube-system Active 34h
7. config view
查看默认的配置
[root@k8s-master ~]# kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://192.168.249.141: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: REDACTED
client-key-data: REDACTED
8. log
查看日志
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5dbb7d6f78-5sjkv 1/1 Running 0 14m
my-dep-5dbb7d6f78-s94lq 1/1 Running 0 14m
nginx-6799fc88d8-jgk25 1/1 Running 0 33h
[root@k8s-master ~]# kubectl logs my-dep-5dbb7d6f78-5sjkv
#更多的举例
语法:kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
# Examples:
kubectl logs my-pod
# 输出一个单容器pod my-pod的日志到标准输出
kubectl logs nginx-78f5d695bd-czm8z -c nginx
# 输出多容器pod中的某个nginx容器的日志
kubectl logs -l app=nginx
# 输出所有包含app-nginx标签的pod日志
kubectl logs -f my-pod
# 加上-f参数跟踪日志,类似tail -f
kubectl logs my-pod -p
# 输出该pod的上一个退出的容器实例日志。在pod容器异常退出时很有用
kubectl logs my-pod --since-time=2018-11-01T15:00:00Z
# 指定时间戳输出日志
kubectl logs my-pod --since=1h
# 指定时间段输出日志,单位s/m/h
9. cluster-info
查看master和集群服务的地址
[root@k8s-master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.249.141:6443
KubeDNS is running at https://192.168.249.141:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
//查看集群详细日志
[root@k8s-master ~]# kubectl cluster-info dump
//查看Kubernetes集群和客户端版本
[root@k8s-master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.0", GitCommit:"af46c47ce925f4c4ad5cc8d1fca46c7b77d13b38", GitTreeState:"clean", BuildDate:"2020-12-08T17:59:43Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.0", GitCommit:"af46c47ce925f4c4ad5cc8d1fca46c7b77d13b38", GitTreeState:"clean", BuildDate:"2020-12-08T17:51:19Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
10. edit
编辑资源信息
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-dep-5dbb7d6f78-5sjkv 1/1 Running 0 26m
my-dep-5dbb7d6f78-s94lq 1/1 Running 0 26m
nginx-6799fc88d8-jgk25 1/1 Running 0 34h
[root@k8s-master ~]# kubectl edit deployment nginx
# 执行以后它会以vi编辑器打开这个资源信息,然后可以编辑
11. label
添加标签
[root@k8s-master ~]# kubectl label pods nginx-6799fc88d8-jgk25 unhealthy=true
pod/nginx-6799fc88d8-jgk25 labeled
//可以用describe查看是否添加成功
[root@k8s-master ~]# kubectl describe pod nginx
Name: nginx-6799fc88d8-jgk25
Namespace: default
Priority: 0
Node: k8s-node1.example.com/192.168.249.145
Start Time: Wed, 25 Aug 2021 21:41:20 +0800
Labels: app=nginx
pod-template-hash=6799fc88d8
unhealthy=true
//删除标签
[root@k8s-master ~]# kubectl label pods nginx-6799fc88d8-jgk25 unhealthy-
//给 namespace 中的所有 pod 添加 label
# kubectl label pods --all status=unhealthy
12. run
启动一个容器
[root@k8s-master ~]# kubectl run my-nginx --image=nginx --replicas=1 --port=80
13. set
修改镜像的版本
[root@k8s-master ~]# kubectl set image deployment/my-dep httpd=httpd:latest