使用kubectl管理容器集群

使用kubectl管理容器集群

  • kubectl 是一个用于操作容器集群的命令行接口,通过利用 kubectl 的各种命令可以实现各种功能。

解决方案

  1. kubectl run 语法

    语法:kubectl run podNAME --image=image [–env=“key=value”] [–port=port]

    例:创建一个名为nginx的pod

    kubectl run nginx --image=nginx:latest --image-pull-policy='IfNotPresent' --port=80
    
    # nginx:latest 镜像 ,--port=暴露容器端口 80 
    # --image-pull-policy='IfNotPresent' # 如果本地不存在镜像,就从外网下载镜像
    
  2. 使用 kubectl delete 删除创建的对象

    删除资源文件中定义的资源

    # kubectl delete -f nginx-test.yaml
    

    删除名为nginx的pod

    # kubectl delete pod nginx
    

    强制删除名为nginx的pod

    # kubectl delete pod nginx --force --grace-period=0
    

    删除名为test1的service

    kubectl delete svc test1
    

    删除具有nginx-test标签的pod

    # kubectl delete pod -l app=test2
    

    删除default命令空间下所有pod、serivce和deployment

    # kubectl delete pod,svc,deployment  --all -n default
    

    删除指定名称空间内的所有资源

    # kubectl delete all -n NS #谨慎操作
    

    删除所有名称空间中的所有资源

    # kubectl delete all --all #危险操作
    
  3. kubectl apply -f 加载 yaml 文件生成 deployment

    # kubectl apply -f mysql-deployment.yaml
    
  4. 使用 get 参数查看 pod 详细信息

    # kubectl get pod -owide 
    
  5. 使用 describe 查看集群中详细信息

    语法: kubectl describe pod pod 名字
    语法: kubectl describe node node 名字
    语法: kubectl describe deployment deployment 名字
    
    1.使用 describe 查看 pod 的详细描述信息
    # kubectl describe pod mysql-76f8906f79-j6twz
    
    2.使用 describe 查看 node 的详细描述信息
    # kubectl describe node master1 #查看详细信息
    
    3. 使用 describe 查看 deployment 的详细描述信息
    # kubectl describe deployment mysql
    
  6. 使用 logs 查看集群日志

    # kubectl logs my-nginx-5684588fff-ctqxf
    

    流式输出pod的日志

    # kubectl logs -f my-nginx-5684588fff-ctqxf
    

    查看 pod 具体容器日志

    # kubectl logs my-nginx-5684588fff-ctqxf  -c my-nginx
    
  7. 使用exec参数执行命令

    exec 命令用于到 pod 中执行一条命令,到 mysql 的镜像中执行 cat /etc/my.cnf 命令

    # kubectl exec mysql-76f8866f79-j6twz cat /etc/my.cnf
    

    使用参数 exec -it ,进入pod

    # kubectl exec -it mysql-76f7766f79-j6twz -- bash
    bash-4.2# exit
    

    使用参数-c 进入pod中的mysql容器

    # kubectl exec -it mysql-76f7766f79-j6twz -c mysql -- bash
    
  8. 使用cp参数拷贝文件

    从 pod 中拷出 hosts 文件到物理机的/tmp 下

    # kubectl cp mysql-76f8866f79-j6twz:/etc/hosts /tmp/hosts
    

    注:使用cp命令容器实例中必须有 tar 库;如果镜像中 tar 命令不存在,那么 kubectl cp 将失败

  9. 使用attach 参数取得 pod 中容器的实时信息

    kubectl attach 用于取得 pod 中容器的实时信息,可以直接查看容器中以 daemon 形式运行的进 程输出,可以持续不断实时的取出消息。像 tail -f /var/log/messages 动态查看日志的作用。

    # kubectl attach my-nginx-5684588fff-5kbgd
    
  10. 使用edit 修改服务配置

    kubectl get -o 格式化输出,-o 指定输出的消息为 yaml 类型

    [root@master1]# kubectl get service nginx -o yaml
    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kubectl.kubernetes.io/last-applied-configuration: 
    ....
      ipFamilyPolicy: SingleStack
      ports:
      - nodePort: 31004
        port: 3000
        protocol: TCP
        targetPort: 80
       
    

    使用edit 这条命令用于编辑服务器上的资源

    例 1:改端口 31004 为 31005

    [root@master1]# kubectl edit service nginx -o yaml
    ...
      ports:
      - nodePort: 31005 #该为31005
        port: 3000
        protocol: TCP
       ....
    

    保存退出后查看service

    [root@master1]# kubectl get service
    NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
    kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          71d
    nginx        NodePort    10.103.99.37   <none>        3000:31005/TCP   10m
    
    

    注:edit 编辑修改配置文件时,不需要停止服务。改完后立即生效

  11. 使用kubectl replace 来替换正在运行中的配置参数

    replace 替换,把 nodeport 端口改为 31003

    [root@master1]# kubectl get service nginx -o yaml > nginx_relace.yaml
    [root@master1]# vim nginx_relace.yaml 
    
     ....
     - nodePort: 31003 #改为31003
        port: 3000
    ...
    [root@master1]# kubectl replace -f nginx_relace.yaml 
    service/nginx replaced
    [root@master1]# kubectl get service 
    NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
    kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          71d
    nginx        NodePort    10.103.99.37   <none>        3000:31003/TCP   35m
    
    
    
  12. 使用apply 命令更改配置信息

    [root@master1]# vim nginx-svc.yaml
    改:11 nodePort: 31003
    为:11 nodePort: 31007
    执行 apply 命令,执行设定文件可以在运行状态修改 port 信息
    [root@master1]# kubectl apply -f nginx-svc.yaml
    Warning: kubectl apply should be used on resource created by either kubectl create --
    save-config or kubectl apply
    service/nginx configured
    注:警告:kubectl apply 应用于由 kubectl create--save config 或 kubectl apply 创建的资源
    
    [root@master1]# kubectl get svc
    NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
    kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          71d
    nginx        NodePort    10.103.99.37   <none>        3000:31007/TCP   46m
    
    
  13. scale横向扩展

    scale 命令用于横向扩展,是 kubernetes 或者 swarm 这类容器编辑平台的重要功能之一

    查看 nginx 现在在哪个结点上运行:

    [root@master1]# kubectl get pods -owide
    NAME                     READY   STATUS    RESTARTS   AGE   IP               NODE       NOMINATED NODE   READINESS GATES
    nginx-78f7fb5756-m5xfx   1/1     Running   0          59m   10.244.209.144   node1   <none>           <none>
    nginx-78f7fb5756-vqvjt   1/1     Running   0          59m   10.244.187.125   node2   <none> 
    

    经过确认此 pod 在 node1 和 node2 上运行 执行 scale 命令,使用 scale 命令进行横向扩展,将原本为 2 的副本提高到 3。相当于开 3 台 nginx 服务,来处理数据

    [root@master1]# kubectl scale --current-replicas=2 --replicas=3 deployment/nginx
    
    [root@master1]# kubectl get pods -owide
    NAME                     READY   STATUS    RESTARTS   AGE   IP               NODE       NOMINATED NODE   READINESS GATES
    nginx-78f7fb5756-989gp   1/1     Running   0          43s   10.244.209.148   node2   <none>           <none>
    nginx-78f7fb5756-m5xfx   1/1     Running   0          61m   10.244.209.144   node2   <none>           <none>
    nginx-78f7fb5756-vqvjt   1/1     Running   0          61m   10.244.187.125   node1   <none>           <none>
    
    
  14. autoscale 自动扩展确认

    autoscale 命令用于自动扩展确认,scale 需要手动执行,而 autoscale 则会根据负载进行调解。而 这条命令则可以对 Deployment 进行设定,通过最小值和最大值的指定进行设定

    [root@master1]# kubectl autoscale deployment nginx --min=2 --max=5
    horizontalpodautoscaler.autoscaling/nginx autoscaled
    
    删除自动扩展确认
    [root@master1]# kubectl delete hpa nginx 
    horizontalpodautoscaler.autoscaling "nginx" deleted
    
    
  15. cordon设置节点是维护状态

    [root@master1]# kubectl cordon node1
    
  16. uncordon解除节点维护状态

    [root@master1]# kubectl uncordon node1
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值