zoukankan      html  css  js  c++  java
  • 3、kubernetes应用快速入门190625

    一、kubernetes应用入门

    1、kubectl命令

    • Basic Commands
      create         Create a resource from a file or from stdin.
      expose         Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
      run            Run a particular image on the cluster
      set            Set specific features on objects
      explain        Documentation of resources
      get            Display one or many resources
      edit           Edit a resource on the server
      delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector
    
    • Deploy Commands
      rollout        Manage the rollout of a resource
      scale          Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
      autoscale      Auto-scale a Deployment, ReplicaSet, or ReplicationController
    
    • Cluster Management Commands
      certificate    Modify certificate resources.
      cluster-info   Display cluster info
      top            Display Resource (CPU/Memory/Storage) usage.
      cordon         Mark node as unschedulable
      uncordon       Mark node as schedulable
      drain          Drain node in preparation for maintenance
      taint          Update the taints on one or more nodes
    
    • Troubleshooting and Debugging Commands
      describe       Show details of a specific resource or group of resources
      logs           Print the logs for a container in a pod
      attach         Attach to a running container
      exec           Execute a command in a container
      port-forward   Forward one or more local ports to a pod
      proxy          Run a proxy to the Kubernetes API server
      cp             Copy files and directories to and from containers.
      auth           Inspect authorization
    
    • Advanced Commands
      diff           Diff live version against would-be applied version
      apply          Apply a configuration to a resource by filename or stdin
      patch          Update field(s) of a resource using strategic merge patch
      replace        Replace a resource by filename or stdin
      wait           Experimental: Wait for a specific condition on one or many resources.
      convert        Convert config files between different API versions
      kustomize      Build a kustomization target from a directory or a remote url.
    
    • Settings Commands
      label          Update the labels on a resource
      annotate       Update the annotations on a resource
      completion     Output shell completion code for the specified shell (bash or zsh)
    
    • Other Commands
      api-resources  Print the supported API resources on the server
      api-versions   Print the supported API versions on the server, in the form of "group/version"
      config         Modify kubeconfig files
      plugin         Provides utilities for interacting with plugins.
      version        Print the client and server version information
    

    2、k8s使用快速入门

    • kubectl run和delete:创建一个deployment或者job控制器控制启动的容器
    ~]# kubectl run nginx-deploy --image=nginx:1.14 --port=80 --replicas=2
    	--port=80:端口暴露
    	--replicas=2:副本个数
    	--dry-run=true:干跑一次
    ~]# kubectl delete pods nginx-deploy-bc9ff65dd-lj9bg  #删除pods,控制器会自动再起一个,让副本保持在2个
    
    • kubectl expose:创建service服务,为pod提供固定访问端点
    ~]# kubectl expose deployment nginx-deploy --name nginx-service --port=80 --target-port=80 --protocol=TCP
    	deployment nginx-deploy:指定暴露的控制器下的pod
    	--name nginx-service:service名称
    	--port=80:service端口
    	--target-port=80:后端pod端口
    	--protocol=TCP:协议
    
    • kubectl get:获取资源信息
    ~]# kubectl get deployment
    ~]# kubectl get pods
    ~]# kubectl get pods -o wide  #查看pod运行的详细信息
    ~]# kubectl get pods --show-labels  #显示标签
    ~]# kubectl get pods -n kube-system -o wide  #显示system名称空间的详细信息
    ~]# kubectl get svc  #获取当前集群的svc
    ~]# kubectl get svc -n kube-system -o wide
    
    • kubectl edit:修改编辑资源
    ~]# kubectl edit svc nginx-service
    
    • kubectl describe:查看资源的详细信息
    ~]# kubectl describe svc nginx-service  #查询svc的详细信息
    
    • kubectl scale:扩展/缩减副本pod
    ~]# kubectl scale --replicas=3 deployment nginx-deploy
    
    • kubectl set:灰度升级镜像版本
    • kubectl rollout:回滚

    3、使用示例

    1. 创建2个提供web的pod
    ~]# kubectl run myapp --image=dongfeimg/myapp:v1 --replicas=2
    
    1. 创建一个客户端pod
    ~]# kubectl run client --image=centos --replicas=1 -it --restart=Never
    [root@client /]# curl 10.244.1.3
    Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
    [root@client /]# curl 10.244.1.3/hostname.html
    myapp-6fd6445cc6-5p676
    
    1. 为myapp创建一个service
    ~]# kubectl expose deployment myapp --name=myapp --port=80
    
    1. 在client测试,service可以实现轮询调度
    [root@client /]# curl myapp/hostname.html
    myapp-6fd6445cc6-2hx5h
    [root@client /]# curl myapp/hostname.html
    myapp-6fd6445cc6-5p676
    
    1. 将pod扩展至5个副本
    ~]# kubectl scale --replicas=5 deployment myapp
    
    1. 镜像升级(灰度)
    ~]# kubectl set image deployment myapp myapp=dongfeimg/myapp:v2
    ~]# kubectl rollout status deployment myapp  #查看更新进度
    
    1. 回滚上一版本
    ~]# kubectl rollout undo deployment myapp
    

    4、补充:配置为可在集群外访问service

    ~]# kubectl edit svc myapp
    type: NodePort
    ~]# kubectl get svc
    myapp           NodePort    10.96.37.191   <none>        80:30804/TCP   17m
    在集群外访问:http://${NODE_IP}:30804
    
  • 相关阅读:
    30行js让你的rem弹性布局适配所有分辨率(含竖屏适配)(转载)
    JavaScript事件流原理解析
    Java中this和super的用法和区别
    Java多态面试题案例几解题思路
    Java多态的向上转型和向下转型
    Java方法的重载和重写
    Java冒泡具体的原理,以及下标的变化
    Java中的冒泡排序和选择排序
    使用Java实现对一个数组的增删改查以及初始化
    Java中构造函数传参数在基本数据类型和引用类型之间的区别
  • 原文地址:https://www.cnblogs.com/L-dongf/p/11083325.html
Copyright © 2011-2022 走看看