一、kubernetes应用入门
1、kubectl命令
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
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
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.
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)
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 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 svc nginx-service
- kubectl describe:查看资源的详细信息
~]# kubectl describe svc nginx-service #查询svc的详细信息
~]# kubectl scale --replicas=3 deployment nginx-deploy
- kubectl set:灰度升级镜像版本
- kubectl rollout:回滚
3、使用示例
- 创建2个提供web的pod
~]# kubectl run myapp --image=dongfeimg/myapp:v1 --replicas=2
- 创建一个客户端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
- 为myapp创建一个service
~]# kubectl expose deployment myapp --name=myapp --port=80
- 在client测试,service可以实现轮询调度
[root@client /]# curl myapp/hostname.html
myapp-6fd6445cc6-2hx5h
[root@client /]# curl myapp/hostname.html
myapp-6fd6445cc6-5p676
- 将pod扩展至5个副本
~]# kubectl scale --replicas=5 deployment myapp
- 镜像升级(灰度)
~]# kubectl set image deployment myapp myapp=dongfeimg/myapp:v2
~]# kubectl rollout status deployment myapp #查看更新进度
- 回滚上一版本
~]# 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