前言:为了更好地的理解Kubernetes,我们平时在写yaml文件的时候可以直直接使用kubectl的一些命令直接创建,好处是能够快速的创建我们需要的资源类型,坏处是他的可定制性没有直接写yaml文件高。
step1创建deployment并创建service
[root@k8s-master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
[root@k8s-master ~]# kubectl expose deployment myapp --name=myapp --port=80
service/myapp exposed
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 39d
myapp ClusterIP 10.96.88.100 <none> 80/TCP 5s
[root@k8s-master ~]# curl 10.96.88.100
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl 10.96.88.100/hostname.html
myapp-7c468db58f-xrmqx
[root@k8s-master ~]# curl 10.96.88.100/hostname.html
myapp-7c468db58f-rdpkq
[root@k8s-master ~]#
总结:clusterIP的service类型只能在集群内部访问,而且访问的方式是轮询
step2增加副本
[root@k8s-master ~]# kubectl scale --replicas=5 deployment myapp
deployment.apps/myapp scaled
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-7c468db58f-84c4k 1/1 Running 0 8s
myapp-7c468db58f-dvll2 1/1 Running 0 8s
myapp-7c468db58f-qx45d 1/1 Running 0 8s
myapp-7c468db58f-rdpkq 1/1 Running 0 11m
myapp-7c468db58f-xrmqx 1/1 Running 0 11m
step3减少副本
[root@k8s-master ~]# kubectl scale --replicas=3 deployment myapp
deployment.apps/myapp scaled
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-7c468db58f-dvll2 1/1 Running 0 60s
myapp-7c468db58f-rdpkq 1/1 Running 0 12m
myapp-7c468db58f-xrmqx 1/1 Running 0 12m
step4镜像的更新
[root@k8s-master ~]# kubectl set image deployments.apps myapp myapp=ikubernetes/myapp:v2
deployment.apps/myapp image updated
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-64758bffd4-7w2vc 1/1 Running 0 5s
myapp-64758bffd4-frgpq 1/1 Running 0 6s
[root@k8s-master ~]# kubectl describe pod myapp-64758bffd4-7w2vc |grep Image
Image: ikubernetes/myapp:v2
[root@k8s-master ~]# curl 10.96.88.100
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
step5回滚镜像
[root@k8s-master ~]# kubectl rollout undo deployment myapp
deployment.apps/myapp rolled back
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-7c468db58f-9dksr 1/1 Running 0 17s
myapp-7c468db58f-dxhxz 1/1 Running 0 20s
myapp-7c468db58f-tmfxh 1/1 Running 0 19s
[root@k8s-master ~]# curl 10.96.88.100
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
将type的类型从ClusterIP改写成NodePort
[root@k8s-master ~]# kubectl edit service myapp
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2020-08-29T07:06:48Z"
labels:
run: myapp
name: myapp
namespace: default
resourceVersion: "79947"
selfLink: /api/v1/namespaces/default/services/myapp
uid: 9f9e448b-82a0-4c7f-b8bd-46ca89ef28df
spec:
clusterIP: 10.96.88.100
externalTrafficPolicy: Cluster
ports:
- nodePort: 31051
port: 80
protocol: TCP
targetPort: 80
selector:
run: myapp
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
查看暴露的端口
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 39d
myapp NodePort 10.96.88.100 <none> 80:31051/TCP 27m


备注:上面的实验是在平时不想写yaml文件的时候可以尝试的使用对新手比较友好
要是平时写的时候我们搞忘了某个字段的拼写我们可以通过以下的方式去查找我们需要的相关信息。
[root@k8s-master ~]# kubectl explain -h
List the fields for supported resources
This command describes the fields associated with each supported API resource.
Fields are identified via a simple JSONPath identifier:
<type>.<fieldName>[.<fieldName>]
Add the --recursive flag to display all of the fields at once without
descriptions. Information about each field is retrieved from the server in
OpenAPI format.
Use "kubectl api-resources" for a complete list of supported resources.
Examples:
# Get the documentation of the resource and its fields
kubectl explain pods
# Get the documentation of a specific field of a resource
kubectl explain pods.spec.containers
Options:
--api-version='': Get different explanations for particular API version
--recursive=false: Print the fields of fields (Currently only 1 level
deep)
Usage:
kubectl explain RESOURCE [options]
Use "kubectl options" for a list of global command-line options (applies to all
commands).
[root@k8s-master ~]#
可以简单的将explain的字段查看方式处理为kubectl <type>.<fieldName>[.<fieldName>]
示例
[root@k8s-master ~]# kubectl explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
[root@k8s-master ~]#
通过上面的帮助文档我们可以大致的分析出构建一个pod的基本需要的字段,如果想查看更多的字段的信息加上参数。
[root@k8s-master ~]# kubectl explain pod --recursive
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
kind <string>
metadata <Object>
annotations <map[string]string>
clusterName <string>
creationTimestamp <string>
deletionGracePeriodSeconds <integer>
deletionTimestamp <string>
finalizers <[]string>
generateName <string>
generation <integer>
labels <map[string]string>
managedFields <[]Object>
apiVersion <string>
fieldsType <string>
fieldsV1 <map[string]>
manager <string>
operation <string>
time <string>
name <string>
namespace <string>
ownerReferences <[]Object>
apiVersion <string>
blockOwnerDeletion <boolean>
controller <boolean>
kind <string>
name <string>
uid <string>
resourceVersion <string>
selfLink <string>
uid <string>
spec <Object>
activeDeadlineSeconds <integer>
affinity <Object>
nodeAffinity <Object>
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
...................
查看某个字段的相信信息.只要字段的类型是object都可以使用这种方式查看帮助信息
[root@k8s-master ~]# kubectl explain pod.spec.containers --recursive
KIND: Pod
VERSION: v1
RESOURCE: containers <[]Object>
DESCRIPTION:
List of containers belonging to the pod. Containers cannot currently be
added or removed. There must be at least one container in a Pod. Cannot be
updated.
A single application container that you want to run within a pod.
FIELDS:
args <[]string>
command <[]string>
env <[]Object>
name <string>
value <string>
valueFrom <Object>
configMapKeyRef <Object>
key <string>
name <string>
optional <boolean>
fieldRef <Object>
apiVersion <string>
fieldPath <string>
resourceFieldRef <Object>
containerName <string>
divisor <string>
resource <string>
secretKeyRef <Object>
key <string>
name <string>
optional <boolean>
envFrom <[]Object>
使用kubectl命令行管理Kubernetes副本与服务
本文介绍了如何使用kubectl命令行工具进行Kubernetes资源管理,包括创建deployment和服务,增减副本,更新镜像,回滚操作,以及将服务类型从ClusterIP改为NodePort。适合初学者快速上手Kubernetes。

被折叠的 条评论
为什么被折叠?



