目录
2.19、在命名空间ky10中创建一个创建一个名为"ky15-nginx"的部署,并使用镜像nginx
2.22、kubectl exec可以跨主机登录容器,docker exec 只能在容器所在主机上登录
3.3、更新 kubectl set更改现有应用资源一些信息。
3.4、回滚 kubectl rollout 对资源进行回滚管理
主要依赖命令行工具kubectl进行管理
可以满足90%以上的使用场景
对资源的增、删、查操作比较容易
命令冗长,复杂,难以记忆
特定场景下,无法实现管理需求
对资源的修改麻烦,需要patch来使用json串更改。
主要依赖统一资源配置清单进行管理
主要依赖图形化操作界面进行管理
k8s中文文档:http://docs.kubernetes.org.cn/683.html
kubectl version

kubectl api-resources

kubectl cluster-info

1、直接命令行输出
source <(kubectl completion bash)
2、进入配置文件进行自动配置
- vim /root/.bashrc
- source <(kubectl completion bash)

journalctl -u kubelet -f
2.6、基本信息查看
kubectl get
[-o wide|json|yaml] [-n namespace]
获取资源的相关信息,-n 指定命令空间,-o 指定输出格式
resource可以是具体资源名称,如pod nginx-xxx;也可以是资源类型,如pod;或者all(仅展示几种核心资源,并不完整)
--all-namespaces 或 -A :表示显示所有命令空间,
--show-labels :显示所有标签
-l app :仅显示标签为app的资源
-l app=nginx :仅显示包含app标签,且值为nginx的资源
kubectl get --all namespces -A pod

2.8、查看pod wide格式相关详细信息
kubectl get pod -owide


kubectl create deployment ky29 --image=soscscs/myapp:v1

kubectl get pod

2.13、查看 master 节点状态- kubectl get componentstatuses
- kubectl get cs


命令空间的作用:用于允许不同 命令空间 的 相同类型 的资源 重名的
- kubectl get namespace
- kubectl get ns


kubectl get all -n default


- kubectl create ns app
- kubectl get ns

创建命名空间ky10,并查看所有的命名空间

- kubectl delete namespace app
- kubectl get ns


创建成功

kubectl describe pod ky15-nginx -n ky10

kubectl get pods -n ky10

kubectl exec -it ky15-nginx bash -n ky10

- kubectl get pod -o wide
- kubectl delete deployments.apps ky29


2.24、扩缩容:
- kubectl delete deployment nginx-wl -n kube-public
- kubectl delete deployment/nginx-wl -n kube-public
创建-->发布-->更新-->回滚-->删除
kubectl create --help

启动 nginx 实例,暴露容器端口 80,设置副本数 3
- kubectl create deployment ky30 --image=nginx:1.14 --port=80 --replicas=3
- kubectl get pods
- kubectl get all



将资源暴露为新的 Service。
kubectl expose --help
为deployment的ky30创建service,并通过Service的80端口转发至容器的80端口上,类型为NodePort

Kubernetes 之所以需要 Service,一方面是因为 Pod 的 IP 不是固定的(Pod可能会重建),另一方面则是因为一组 Pod 实例之间总会有负载均衡的需求。
Service 通过 Label Selector 实现的对一组的 Pod 的访问。
对于容器应用而言,Kubernetes 提供了基于 VIP(虚拟IP) 的网桥的方式访问 Service,再由 Service 重定向到相应的 Pod。
service 的 type 类型:
●ClusterIP:提供一个集群内部的虚拟IP以供Pod访问(service默认类型)●NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过 NodeIp:NodePort 的方式Kubernetes集群外部的程序可以访问Service。
每个端口只能是一种服务,端口范围只能是 30000-32767。●LoadBalancer:通过设置LoadBalancer映射到云服务商提供的LoadBalancer地址。这种用法仅用于在公有云服务提供商的云平台上设置Service的场景。通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。
在service提交后,Kubernetes就会调用CloudProvider在公有云上为你创建一个负载均衡服务,并且把被代理的Pod的IP地址配置给负载均衡服务做后端。●externalName:将service名称映射到一个DNS域名上,相当于DNS服务的CNAME记录,用于让Pod去访问集群外部的资源,它本身没有绑定任何的资源。
创建部署新的
kubectl create deployment nginx-ky30 --image=nginx:1.14 --port=80 --replicas=4


先创建 kubectl create命令,创建并运行一个或多个容器镜像
发布 kubectl expose命令,将资源暴露为新的 Service。
查看pod网络状态详细信息和 Service暴露的端口
注意:不创建发布会失败,会报错

kubectl exec可以跨主机登录容器

式样访问登录是否是轮询(每个容器都改一下)


访问


查看关联后端的节点
kubectl describe svc nginx

查看 service 的描述信息
kubectl describe svc ky10

在master01操作 查看访问日志
kubectl logs ky10-56b94fcf84-fzppc

kubectl set --help

- Configure application resources
-
- These commands help you make changes to existing application resources.
-
- Available Commands:
- env Update environment variables on a pod template
- image Update image of a pod template
- resources Update resource requests/limits on objects with pod templates
- selector Set the selector on a resource
- serviceaccount Update ServiceAccount of a resource
- subject Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding
-
- Usage:
- kubectl set SUBCOMMAND [options]
-
- Use "kubectl
--help" for more information about a given command. - Use "kubectl options" for a list of global command-line options (applies to all commands)
kubectl set image --help
- Configure application resources
-
- These commands help you make changes to existing application resources.
-
- Available Commands:
- env Update environment variables on a pod template
- image Update image of a pod template
- resources Update resource requests/limits on objects with pod templates
- selector Set the selector on a resource
- serviceaccount Update ServiceAccount of a resource
- subject Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding
-
- Usage:
- kubectl set SUBCOMMAND [options]
-
- Use "kubectl
--help" for more information about a given command. - Use "kubectl options" for a list of global command-line options (applies to all commands).
- [root@master01 ~]# kubectl set image --help
- Update existing container image(s) of resources.
-
- Possible resources include (case insensitive):
-
- pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), replicaset (rs)
-
- Examples:
- # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to
- 'busybox'.
- kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
-
- # Update all deployments' and rc's nginx container's image to 'nginx:1.9.1'
- kubectl set image deployments,rc nginx=nginx:1.9.1 --all
-
- # Update image of all containers of daemonset abc to 'nginx:1.9.1'
- kubectl set image daemonset abc *=nginx:1.9.1
-
- # Print result (in yaml format) of updating nginx container image from local file, without hitting
- the server
- kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml
-
- Options:
- --all=false: Select all resources, including uninitialized ones, in the namespace of the
- specified resource types
- --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or
- map key is missing in the template. Only applies to golang and jsonpath output formats.
- --dry-run='none': Must be "none", "server", or "client". If client strategy, only print the
- object that would be sent, without sending it. If server strategy, submit server-side request
- without persisting the resource.
- --field-manager='kubectl-set': Name of the manager used to track field ownership.
- -f, --filename=[]: Filename, directory, or URL to files identifying the resource to get from a
- server.
- -k, --kustomize='': Process the kustomization directory. This flag can't be used together with -f
- or -R.
- --local=false: If true, set image will NOT contact api-server but run locally.
- -o, --output='': Output format. One of:
- json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
- --record=false: Record current kubectl command in the resource annotation. If set to false, do
- not record the command. If set to true, record the command. If not set, default to updating the
- existing annotation value only if one already exists.
- -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you
- want to manage related manifests organized within the same directory.
- -l, --selector='': Selector (label query) to filter on, not including uninitialized ones, supports
- '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
- --template='': Template string or path to template file to use when -o=go-template,
- -o=go-template-file. The template format is golang templates
- [http://golang.org/pkg/text/template/#pkg-overview].
-
- Usage:
- kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ...
- CONTAINER_NAME_N=CONTAINER_IMAGE_N [options]
-
- Use "kubectl options" for a list of global command-line options (applies to all commands).
curl -I http://192.168.41.30:31924

kubectl set image deployment/ky10 nginx=nginx:1.15
更新世可能会有延迟


处于动态监听 pod 状态,由于使用的是滚动更新方式,所以会先生成一个新的pod,然后删除一个旧的pod,往后依次类推
kubectl get pods -w

kubectl rollout --help
- Manage the rollout of a resource.
-
- Valid resource types include:
-
- * deployments
- * daemonsets
- * statefulsets
-
- Examples:
- # Rollback to the previous deployment
- kubectl rollout undo deployment/abc
-
- # Check the rollout status of a daemonset
- kubectl rollout status daemonset/foo
-
- Available Commands:
- history View rollout history
- pause Mark the provided resource as paused
- restart Restart a resource
- resume Resume a paused resource
- status Show the status of the rollout
- undo Undo a previous rollout
-
- Usage:
- kubectl rollout SUBCOMMAND [options]
-
- Use "kubectl
--help" for more information about a given command. - Use "kubectl options" for a list of global command-line options (applies to all commands).
kubectl rollout history deployment/nginx

kubectl rollout undo deployment/ky10

版本回滚以前的IP地址

版本回滚以后的IP地址



kubectl rollout status deployment/ky10


kubectl delete deployments/nginx-ky30


kubectl delete svc/nginx

项目生命周期
创建
kubectl create <资源类型> <资源名称> --image=<镜像名> [ --port= --replicas= }
发布
kubectl expose <资源类型> <资源名称> --port=--target-porttype={ClusteIPINodePort}
更新
kubectl set image <资源类型> <资源名称> 容器名=镜像名
回滚
- kubectl rollout unde <资源类型><资源名称> 默认是回滚到上一个版本
- --torevision= 可滚到你指定的版木
删除
kubectl delete <资源类型><资源名称>