一、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

最新文章

  1. CSS中LI圆点样式li {list-style-type:符号名称}
  2. 安装Oracle报错,全部为未知!
  3. [c++] Associative Containers
  4. python的高级特性3:神奇的__call__与返回函数
  5. 建立和断开与MySQL服务器的连接
  6. 创建Struct2的web应用(一)
  7. Pro Git(中文版)
  8. AndroidManifest File Features
  9. JDBC ----常用数据库的驱动程序及JDBC URL:
  10. SQL Server(基本) 关键字的使用 一
  11. MVC中如何避免POST请求中出现的重复提交
  12. XAMPP中MySQL无法启动解决办法
  13. Unix环境高级编程:fork, vfork, clone
  14. pow 的使用和常见问题
  15. Drying
  16. MUI的踩坑笔记
  17. Java基础-初识面向对象编程(Object-Oriented-Programming)
  18. 基于libhid/libusb进行开发
  19. [深入理解Android卷一全文-第四章]深入理解zygote
  20. php独特的语法

热门文章

  1. codeforces 633D D. Fibonacci-ish(dfs+暴力+map)
  2. android 网络编程--socket tcp/ip udp http之间的关系
  3. bzoj 4031: 小Z的房间 矩阵树定理
  4. JS通过经纬度计算两个地方的距离
  5. jenkins pipline 用法收集
  6. VIJOS:P1706(舞会)
  7. Go语言是如何处理栈的
  8. js点滴知识(1) -- 获取DOM对象和编码
  9. 问题:JsonConvert;结果:JSON详解
  10. shell入门-awk-3