1.kubectl explain rs查看KIND: ReplicaSet的信息。kubectl explain rs.spec查看可描述的资源信息。template <Object>可对pod进行详细定义。
[root@master ~]# kubectl explain rs
KIND: ReplicaSet
VERSION: extensions/v1beta1
DESCRIPTION:
DEPRECATED - This group version of ReplicaSet is deprecated by
apps/v1beta2/ReplicaSet. See the release notes for more information.
ReplicaSet ensures that a specified number of pod replicas are running at
any given time.
[root@master ~]# kubectl explain rs.spec
KIND: ReplicaSet
VERSION: extensions/v1beta1
RESOURCE: spec <Object>
DESCRIPTION:
Spec defines the specification of the desired behavior of the ReplicaSet.
More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
ReplicaSetSpec is the specification of a ReplicaSet.
FIELDS:
minReadySeconds <integer>
Minimum number of seconds for which a newly created pod should be ready
without any of its container crashing, for it to be considered available.
Defaults to 0 (pod will be considered available as soon as it is ready)
replicas <integer>
Replicas is the number of desired replicas. This is a pointer to
distinguish between explicit zero and unspecified. Defaults to 1. More
info:
https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
selector <Object>
Selector is a label query over pods that should match the replica count. If
the selector is empty, it is defaulted to the labels present on the pod
template. Label keys and values that must match in order to be controlled
by this replica set. More info:
https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
template <Object>
Template is the object that describes the pod that will be created if
insufficient replicas are detected. More info:
https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template
2.创建一个资源文件vim rs-demo.yaml。apiVersion: apps/v1定义api属组。kind: ReplicaSet定义资源类型。metadata:定义资源元数据信息。spec:定义资源配置信息。 selector: 中的matchLabels:定义选择器去获取的标签。template:对资源中的pod进行定义。metadata:定义元数据。labels: 定义标签(注意:这里定义的标签一定要能被selector所获取,否则创建将无意义)。spec:对pod的配置信息进行详细定义。kubectl create -f rs-demo.yaml把资源跑起来。kubectl get rs查看ReplicaSet资源的运行情况。
[root@master manifests]# vim rs-demo.yaml
[root@master manifests]# cat rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
name: myapp-pod
labels:
app: myapp
release: canary
environment: qa
spec:
containers:
- name: myapp-container
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
[root@master manifests]# kubectl create -f rs-demo.yaml
replicaset.apps/myapp created
[root@master manifests]# kubectl get rs
NAME DESIRED CURRENT READY AGE
myapp 2 2 2 18s
3.kubectl get pods查看pod的运行情况。kubectl describe pods myapp-27qbz查看详细的pod信息。curl 10.244.2.38尝试对pod进行网页访问。kubectl delete pods myapp-55csj尝试删除其中一个pod。kubectl get pods查看发现ReplicaSet控制器会自动按照资源清单再创建一个pod进行补全。
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 3d
liveness-httpget-pod 1/1 Running 4 13h
myapp-27qbz 1/1 Running 0 1m
myapp-55csj 1/1 Running 0 1m
pod-demo 2/2 Running 13 20h
poststart-pod 0/1 CrashLoopBackOff 32 11h
readiness-httpget-pod 1/1 Running 2 11h
[root@master manifests]# kubectl describe pods myapp-27qbz
Name: myapp-27qbz
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: node2.example.com/172.20.0.130
Start Time: Mon, 10 Dec 2018 21:03:13 -0500
Labels: app=myapp
environment=qa
release=canary
Annotations: <none>
Status: Running
IP: 10.244.2.38
Controlled By: ReplicaSet/myapp
Containers:
myapp-container:
Container ID: docker://8bdb25d5b4bf6dae755459b84b709a314a87e83d8f020989bdc369553e1719d3
Image: ikubernetes/myapp:v1
Image ID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 10 Dec 2018 21:03:14 -0500
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-s5rf4 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-s5rf4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-s5rf4
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m default-scheduler Successfully assigned default/myapp-27qbz to node2.example.com
Normal Pulled 2m kubelet, node2.example.com Container image "ikubernetes/myapp:v1" already present on machine
Normal Created 2m kubelet, node2.example.com Created container
Normal Started 2m kubelet, node2.example.com Started container
[root@master manifests]# curl 10.244.2.38
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@master manifests]# kubectl delete pods myapp-55csj
pod "myapp-55csj" deleted
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 3d
liveness-httpget-pod 1/1 Running 4 14h
myapp-27qbz 1/1 Running 0 11m
myapp-sjn54 1/1 Running 0 15s
pod-demo 2/2 Running 14 20h
poststart-pod 0/1 CrashLoopBackOff 34 11h
readiness-httpget-pod 1/1 Running 2 12h
4.kubectl get pods --show-labels查看所有Pod的标签。kubectl label pods pod-demo release=canary将其中一个pod的标签标记为ReplicaSet控制器选择的标签。kubectl get pods --show-labels查看发现ReplicaSet控制器会从原有的2个pod中随机选择一个终止。kubectl delete pods pod-demo将标记的Pod删除。kubectl get pods查看发现ReplicaSet控制器会自动创建一个pod进行补全。
[root@master manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Completed 0 3d run=client
liveness-httpget-pod 1/1 Running 4 14h <none>
myapp-27qbz 1/1 Running 0 14m app=myapp,environment=qa,release=canary
myapp-sjn54 1/1 Running 0 3m app=myapp,environment=qa,release=canary
pod-demo 2/2 Running 14 20h app=myapp,tier=frontend
poststart-pod 0/1 CrashLoopBackOff 35 11h <none>
readiness-httpget-pod 1/1 Running 2 12h <none>
[root@master manifests]# kubectl label pods pod-demo release=canary
pod/pod-demo labeled
[root@master manifests]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Completed 0 3d run=client
liveness-httpget-pod 1/1 Running 4 14h <none>
myapp-27qbz 1/1 Running 0 15m app=myapp,environment=qa,release=canary
pod-demo 2/2 Running 14 20h app=myapp,release=canary,tier=frontend
poststart-pod 0/1 CrashLoopBackOff 35 11h <none>
readiness-httpget-pod 1/1 Running 2 12h <none>
[root@master manifests]# kubectl delete pods pod-demo
pod "pod-demo" deleted
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 3d
liveness-httpget-pod 1/1 Running 4 14h
myapp-27qbz 1/1 Running 0 27m
myapp-kqkwk 1/1 Running 0 51s
poststart-pod 0/1 CrashLoopBackOff 37 11h
readiness-httpget-pod 1/1 Running 2 12h
5.kubectl edit rs myapp对ReplicaSet资源进行编辑,replicas: 2 ---》 replicas: 5修改运行pod数量。kubectl get pods查看发现Pod数量已经增加。kubectl edit rs myapp再次对ReplicaSet资源进行编辑,image: ikubernetes/myapp:v1 ---》 image: ikubernetes/myapp:v2修改使用的镜像(进行升版)。kubectl get rs -o wide查看发现ReplicaSet资源已经修改(注意:这里修改并不等于生效)。kubectl get pods -o wide查看pod信息。curl 10.244.2.38访问其中一个pod,发现镜像版本并没有修改,原因在于镜像版本要修改,需要重构Pod,而这里并没有重构pod。kubectl delete pods myapp-27qbz尝试删除其中一个pod,系统会自动创建一个pod补全,这时因为ReplicaSet资源已经发生改变,系统会按照新的资源进行重构。kubectl get pods -o wide找到新创建的资源信息。curl 10.244.2.40访问发现镜像已经使用新的版本,使用的是重构的pod。
[root@master manifests]# kubectl edit rs myapp
replicas: 2 ---》 replicas: 5
replicaset.extensions/myapp edited
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Completed 0 3d
liveness-httpget-pod 1/1 Running 4 14h
myapp-27qbz 1/1 Running 0 37m
myapp-8vlnp 1/1 Running 0 33s
myapp-jg26k 1/1 Running 0 33s
myapp-kqkwk 1/1 Running 0 11m
myapp-xkv74 1/1 Running 0 33s
poststart-pod 0/1 CrashLoopBackOff 39 11h
readiness-httpget-pod 1/1 Running 2 12h
[root@master manifests]# kubectl edit rs myapp
image: ikubernetes/myapp:v1 ---》 image: ikubernetes/myapp:v2
replicaset.extensions/myapp edited
[root@master manifests]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp 5 5 5 41m myapp-container ikubernetes/myapp:v2 app=myapp,release=canary
[root@master manifests]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
client 0/1 Completed 0 3d 10.244.2.3 node2.example.com
liveness-httpget-pod 1/1 Running 4 14h 10.244.2.34 node2.example.com
myapp-27qbz 1/1 Running 0 41m 10.244.2.38 node2.example.com
myapp-8vlnp 1/1 Running 0 4m 10.244.1.31 node1.example.com
myapp-jg26k 1/1 Running 0 4m 10.244.1.32 node1.example.com
myapp-kqkwk 1/1 Running 0 15m 10.244.1.30 node1.example.com
myapp-xkv74 1/1 Running 0 4m 10.244.2.39 node2.example.com
poststart-pod 0/1 CrashLoopBackOff 40 12h 10.244.2.36 node2.example.com
readiness-httpget-pod 1/1 Running 2 12h 10.244.2.33 node2.example.com
[root@master manifests]# curl 10.244.2.38
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@master manifests]# kubectl delete pods myapp-27qbz
pod "myapp-27qbz" deleted
[root@master manifests]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
client 0/1 Completed 0 3d 10.244.2.3 node2.example.com
liveness-httpget-pod 1/1 Running 4 14h 10.244.2.34 node2.example.com
myapp-8vlnp 1/1 Running 0 5m 10.244.1.31 node1.example.com
myapp-jg26k 1/1 Running 0 5m 10.244.1.32 node1.example.com
myapp-kqkwk 1/1 Running 0 16m 10.244.1.30 node1.example.com
myapp-xkv74 1/1 Running 0 5m 10.244.2.39 node2.example.com
myapp-zg4px 1/1 Running 0 18s 10.244.2.40 node2.example.com
poststart-pod 0/1 CrashLoopBackOff 40 12h 10.244.2.36 node2.example.com
readiness-httpget-pod 1/1 Running 2 12h 10.244.2.33 node2.example.com
[root@master manifests]# curl 10.244.2.40
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>