kubectl命令详解、Pod创建过程、Pod的生命周期、定制Pod、资源对象文件

集群管理命令

命令说明

子命令说明
help用于查看命令及子命令的帮助信息
cluster-info显示集群的相关配置信息
version查看服务器及客户端的版本信息
api-resources查看当前服务器上所有的资源对象
api-versions查看当前服务器上所有资源对象的版本
config管理当前节点上kubeconfig 的认证信息
help
# 查看帮助命令信息
[root@master ~]# kubectl help version
Print the client and server version information for the current context.
Examples:
  # Print the client and server versions for the current context
  kubectl version
... ...
cluster-info
# 查看集群状态信息
[root@master ~]# kubectl cluster-info 
Kubernetes control plane is running at https://192.168.1.50:6443
CoreDNS is running at https://192.168.1.50:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
... ...
version
# 查看服务端与客户端版本信息
[root@master ~]# kubectl version
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.0", GitCommit:"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d", GitTreeState:"clean", BuildDate:"2022-12-08T19:58:30Z", GoVersion:"go1.19.4", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.7
Server Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.0", GitCommit:"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d", GitTreeState:"clean", BuildDate:"2022-12-08T19:51:45Z", GoVersion:"go1.19.4", Compiler:"gc", Platform:"linux/amd64"}
api-resources
# 查看资源对象类型
[root@master ~]# kubectl api-resources 
NAME             SHORTNAMES     APIVERSION      NAMESPACED      KIND
bindings                        v1              true            Binding
endpoints        ep             v1              true            Endpoints
events           ev             v1              true            Event
... ...
api-versions
# 查看资源对象版本
[root@master ~]# kubectl api-versions 
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
... ...
config
# 查看当前认证使用的用户及证书
[root@master ~]# kubectl config get-contexts 
CURRENT   NAME                          CLUSTER      AUTHINFO
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin
# 使用 view 查看详细配置
[root@master ~]# kubectl config view 
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.1.50: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

主机管理授权

[root@harbor ~]# vim /etc/hosts
192.168.1.30    harbor
192.168.1.50    master
192.168.1.51    node-0001
192.168.1.52    node-0002
192.168.1.53    node-0003
192.168.1.54    node-0004
192.168.1.55    node-0005
[root@harbor ~]# dnf install -y kubectl
[root@harbor ~]# mkdir -p $HOME/.kube
[root@harbor ~]# rsync -av master:/etc/kubernetes/admin.conf $HOME/.kube/config
[root@harbor ~]# chown $(id -u):$(id -g) $HOME/.kube/config
[root@harbor ~]# kubectl get nodes
NAME        STATUS   ROLES           AGE   VERSION
master      Ready    control-plane   24h   v1.26.0
node-0001   Ready    <none>          22h   v1.26.0
node-0002   Ready    <none>          22h   v1.26.0
node-0003   Ready    <none>          22h   v1.26.0
node-0004   Ready    <none>          22h   v1.26.0
node-0005   Ready    <none>          22h   v1.26.0

Pod管理命令

创建Pod

# 创建 Pod
[root@master ~]# kubectl run myweb --image=myos:nginx
pod/myweb created
# 查询资源对象
[root@master ~]# kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE
myweb   1/1     Running   0          3s    10.244.1.3   node-0001
[root@master ~]# curl http://10.244.1.3
Nginx is running !

Pod 管理命令(一)

子命令说明备注
run/create创建资源对象可输出资源文件模板
get查看资源对象的状态信息常用参数: -o 显示格式
describe查询资源对象的属性信息
logs查看容器的报错信息常用参数: -c 容器名称
get
# 查看 Pod 资源对象
[root@master ~]# kubectl get pods
NAME     READY   STATUS    RESTARTS   AGE
myweb    1/1     Running   0          10m
# 只查看资源对象的名字
[root@master ~]# kubectl get pods -o name
pod/myweb
# 查看资源对象运行节点的信息
[root@master ~]# kubectl get pods -o wide
NAME     READY   STATUS    RESTARTS   AGE   IP           NODE
myweb    1/1     Running   0          10m   10.244.1.3   node-0001
# 查看资源对象详细信息,Yaml 格式
[root@master ~]# kubectl get pod myweb -o yaml
apiVersion: v1
kind: Pod
metadata:
  name: myweb
... ...
# 查看名称空间
[root@master ~]# kubectl get namespaces 
NAME              STATUS   AGE
default           Active   39h
kube-node-lease   Active   39h
kube-public       Active   39h
kube-system       Active   39h
# 查看 kube-system 名称空间中的 Pod 信息
[root@master ~]# kubectl -n kube-system get pods
NAME                             READY   STATUS    RESTARTS      AGE
etcd-master                      1/1     Running   0             39h
kube-apiserver-master            1/1     Running   0             39h
kube-controller-manager-master   1/1     Running   0             39h
kube-scheduler-master            1/1     Running   0             39h
... ...
create
# 创建名称空间资源对象
[root@master ~]# kubectl create namespace work
namespace/work created
# 查看名称空间
[root@master ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   39h
kube-node-lease   Active   39h
kube-public       Active   39h
kube-system       Active   39h
work              Active   11s
run
# 创建简单 Pod 资源对象
[root@master ~]# kubectl -n work run myhttp --image=myos:httpd
pod/myhttp created
# 查询资源对象
[root@master ~]# kubectl -n work get pods -o wide
NAME     READY   STATUS    RESTARTS   AGE   IP           NODE
myhttp   1/1     Running   0          3s    10.244.2.2   node-0002
# 访问验证
[root@master ~]# curl http://10.244.2.2
Welcome to The Apache.
describe
# 查看资源对象的属性信息
[root@master ~]# kubectl describe pod myweb
Name:             myweb
Namespace:        default
Priority:         0
Service Account:  default
Node:             node-0001/192.168.1.51
... ...
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  16m   default-scheduler  Successfully assigned default/myweb to node-0001
  Normal  Pulled     16m   kubelet            Container image "myos:nginx" already present on machine
  Normal  Created    16m   kubelet            Created container myweb
  Normal  Started    16m   kubelet            Started container myweb
# 查看 work 名称空间下的 pod 信息
[root@master ~]# kubectl -n work describe pod myhttp
Name:             myhttp
Namespace:        work
Priority:         0
Service Account:  default
Node:             node-0002/192.168.1.52
... ...
logs
# 访问服务,并查看日志
[root@master ~]# curl http://10.244.1.3/info.php
[root@master ~]# curl http://10.244.2.2/info.php
... ...
# 查看 myweb 日志
[root@master ~]# kubectl logs myweb 
2022/11/12 18:28:54 [error] 7#0: *2 open() "/usr/local/nginx/html/info.php" failed (2: No such file or directory), client: 10.244.0.0, server: localhost, request: "GET /info.php HTTP/1.1", host: "10.244.2.12"
# 查看 myhttp 日志
[root@master ~]# kubectl -n work logs myhttp
[root@master ~]# 

Pod 管理命令(二)

子命令说明备注
exec在某一个容器内执行特定的命令可选参数: -c 容器名称
cp在容器和宿主机之间拷贝文件或目录可选参数: -c 容器名称
delete删除资源对象可选参数: -l 标签
exec
# 在容器内执行命令
[root@master ~]# kubectl exec myweb -- ls 
50x.html
index.html
[root@master ~]# kubectl exec -it myweb -- /bin/bash
[root@myweb html]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        inet 10.244.1.3  netmask 255.255.255.0  broadcast 10.244.2.255
        ether 3a:32:78:59:ed:25  txqueuelen 0  (Ethernet)
... ...
cp
# 与容器进行文件或目录传输
[root@master ~]# kubectl cp myweb:/etc/yum.repos.d /root/aaa
tar: Removing leading `/' from member names
[root@master ~]# tree /root/aaa
/root/aaa
├── local.repo
├── Rocky-AppStream.repo
├── Rocky-BaseOS.repo
└── Rocky-Extras.repo
 
0 directories, 4 files
[root@master ~]# kubectl -n work cp /etc/passwd myhttp:/root/mima
[root@master ~]# kubectl -n exec myweb -- ls /root/
mima
delete
# 删除资源对象
[root@master ~]# kubectl delete pods myweb 
pod "myweb" deleted
# 删除 work 名称空间下所有 Pod 对象
[root@master ~]# kubectl -n work delete pods --all
pod "myhttp" deleted
# 删除名称空间
[root@master ~]# kubectl delete namespaces work 
namespace "work" deleted

资源对象文件

Pod 资源对象文件

[root@master ~]# vim myweb.yaml
---
kind: Pod
apiVersion: v1
metadata:
  name: myweb
spec:
  containers:
  - name: webserver
    image: myos:nginx

资源文件管理命令

子命令说明备注
create创建文件中定义的资源支持指令式和资源对象文件配置
apply创建(更新)文件中定义的资源只支持资源对象文件(声明式)
delete删除文件中定义的资源支持指令式和资源对象文件配置
create
# 创建资源对象
[root@master ~]# kubectl create -f myweb.yaml 
pod/myweb created
# 不能更新,重复执行会报错
[root@master ~]# kubectl create -f myweb.yaml 
Error from server (AlreadyExists): error when creating "myweb.yaml": pods "myweb" already exists
delete
# 使用资源对象文件删除
[root@master ~]# kubectl delete -f myhttp.yaml
pod "myhttp" deleted
[root@master ~]# kubectl get pods
No resources found in default namespace.
apply
# 创建资源对象
[root@master ~]# kubectl apply -f myweb.yaml 
pod/myweb created
# 更新资源对象
[root@master ~]# kubectl apply -f myweb.yaml 
pod/myweb configured
# 删除资源对象
[root@master ~]# kubectl delete -f myweb.yaml 
pod "myweb" deleted
# 拓展提高
# 与 kubectl apply -f myweb.yaml 功能相同
[root@master ~]# cat myweb.yaml |kubectl apply -f -  
  • 15
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值