首页 > Web开发 > 详细

Kubernetes 学习4 kubernetes应用快速入门

时间:2019-05-09 12:16:37      阅读:158      评论:0      收藏:0      [点我收藏+]

一、相关命令

  1、kubectl

    通过连接api server 进行各k8s对象资源的增删改查,如pod,service,controller(控制器),我们常用的pod控制器replicaset,deployment,statefulet,daemonset,job,cronjob等,甚至node都是对象。

[root@k8smaster ~]# kubectl --help
kubectl controls the Kubernetes cluster manager. 

Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner): #新手用的命令
  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

Basic Commands (Intermediate):  #中级的基础命令
  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 #自动改变,就是创建HPA的     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  #增加污点,给节点增加污点以后,能容忍该污点的pod才能被调度到该节点,默认master会有很多污点,所以创建的pod默认是不会在master上创建,这样确保了master只运行各系统组件        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  #和docker 中的attach相似       Attach to a running container
  exec  #和docker 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: #高级命令
  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 one condition on one or many resources
  convert #转换       Convert config files between different API versions

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: #其它命令
  alpha          Commands for features in alpha
  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         Runs a command-line plugin
  version        Print the client and server version information

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

   2、查看kubectl 版本信息或集群信息

[root@k8smaster ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.1", GitCommit:"b1b29978270dc22fecc592ac55d903350454310a", GitTreeState:"clean", BuildDate:"2018-07-17T18:53:20Z", GoVer
sion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.1", GitCommit:"b1b29978270dc22fecc592ac55d903350454310a", GitTreeState:"clean", BuildDate:"2018-07-17T18:43:26Z", GoVer
sion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}[root@k8smaster ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.10.10:6443
KubeDNS is running at https://192.168.10.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use kubectl cluster-info dump.

  3、kubectl run 命令

[root@k8smaster ~]# kubectl run --help
Create and run a particular image, possibly replicated. 

Creates a deployment or job to manage the created container(s).#基于这两种中的某一种创建容器(也就是pod)

Examples:
  # Start a single instance of nginx.
  kubectl run nginx --image=nginx  #基于nginx镜像启动pod
  
  # Start a single instance of hazelcast and let the container expose port 5701 .
  kubectl run hazelcast --image=hazelcast --port=5701
  
  # Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default"
in the container.
  kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
  
  # Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container.
  kubectl run hazelcast --image=nginx --labels="app=hazelcast,env=prod"
  
  # Start a replicated instance of nginx.
  kubectl run nginx --image=nginx --replicas=5 #启动5个pod
  
  # Dry run. Print the corresponding API objects without creating them.
  kubectl run nginx --image=nginx --dry-run #单跑模式
  
  # Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from
JSON.
  kubectl run nginx --image=nginx --overrides={ "apiVersion": "v1", "spec": { ... } }
  
  # Start a pod of busybox and keep it in the foreground, dont restart it if it exits.
  kubectl run -i -t busybox --image=busybox --restart=Never #默认容器结束了会自动补上去,加了此命令后就不会再自动补上去
  
  # Start the nginx container using the default command, but use custom arguments (arg1 .. argN) for that command.
  kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>
  
  # Start the nginx container using a different command and custom arguments.
  kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN> #加上自定义的命令
  
  # Start the perl container to compute π to 2000 places and print it out.
  kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle print bpi(2000)
  
  # Start the cron job to compute π to 2000 places and print it out every 5 minutes.
  kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle print bpi(2000)‘ #创建一个定时job

    使用kubectl run创建一个单跑模式的nginx容器

[root@k8smaster ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
deployment.apps/nginx-deploy created (dry run) #deployment控制器下所控制的应用程序,叫做nginx-deploy  

    使用kubectl run创建一个nginx容器

[root@k8smaster ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
deployment.apps/nginx-deploy created
[root@k8smaster ~]# kubectl get deployment
NAME           DESIRED(期望)   CURRENT(当前)   UP-TO-DATE   AVAILABLE(可用)   AGE
nginx-deploy   1         1         1            0           23s

    过一会儿查看显示已经可用

[root@k8smaster ~]# kubectl get deployment
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1         1         1            1           2m

    查看创建的pod

[root@k8smaster ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
nginx-deploy-5b595999-vw5vt   1/1       Running   0          3m
[root@k8smaster ~]# kubectl get pods -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP           NODE
nginx-deploy-5b595999-vw5vt   1/1       Running   0          4m        10.244.2.2   k8snode2

 

    

Kubernetes 学习4 kubernetes应用快速入门

原文:https://www.cnblogs.com/Presley-lpc/p/10837379.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!