k8s声明式资源管理办法
声明式资源管理方法依赖于——资源配置清单(yaml/json)
查看资源配置清单的方法
[root@hdss7-21 ~]# kubectl get svc nginx-dp -o yaml -n kube-public
解释资源配置清单
[root@hdss7-21 ~]# kubectl explain service.metadata
创建资源配置清单
vim /root/nginx-ds-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-ds
name: nginx-ds
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-ds
# sessionAffinity: None 默认就是none
type: ClusterIP
创建
[root@hdss7-21 ~]# kubectl create -f nginx-ds-svc.yaml
service/nginx-ds created
[root@hdss7-21 ~]# kubectl get svc -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 14d
nginx-ds ClusterIP 192.168.17.122 <none> 80/TCP 62s
修改yaml文件,把端口改为8080
[root@hdss7-21 ~]# kubectl delete svc nginx-ds
service "nginx-ds" deleted
[root@hdss7-21 ~]# kubectl apply -f nginx-ds-svc.yaml
service/nginx-ds created
[root@hdss7-21 ~]# kubectl get svc -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 14d
nginx-ds ClusterIP 192.168.101.10 <none> 8080/TCP 14s
修改资源配置分为两类
-
在线修改(不推荐,使用该方法没有历史记录)
[root@hdss7-21 ~]# kubectl edit svc nginx-ds service/nginx-ds edited [root@hdss7-21 ~]# kubectl get svc -n default NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 15d nginx-ds ClusterIP 192.168.101.10 <none> 80/TCP 4m21s
又将8080改为80
-
离线修改(推荐:有历史记录)
修改对应的yaml文件,并用
kubectl apply -f nginx-ds-svc.yaml
命令使修改生效。
有些段是默认的,不用全部列出来。
拉取nginx镜像
[root@hdss7-21 ~]# docker login docker.io
[root@hdss7-21 ~]# docker pull xxxxxxxxxxxx/nginx:curl
[root@hdss7-21 ~]# docker images | grep curl
xxxxxxxxxxxx/nginx curl 6f10e7047510 3 days ago 136MB
[root@hdss7-21 ~]# docker tag 6f10e7047510 harbor.od.com/public/nginx:curl
[root@hdss7-21 ~]# docker images | grep curl
harbor.od.com/public/nginx curl 6f10e7047510 3 days ago 136MB
xxxxxxxxxxxx/nginx curl 6f10e7047510 3 days ago 136MB
[root@hdss7-21 ~]# docker push harbor.od.com/public/nginx:curl
The push refers to repository [harbor.od.com/public/nginx]
761966e456bc: Pushed
4258832b2570: Pushed
683a28d1d7fd: Pushed
d626a8ad97a1: Pushed
curl: digest: sha256:094f8762228ad310514b54fde096ee9594741171b33da2deee21d32a612eba76 size: 1160
修改资源配置清单,把v1.7.9改成curl
[root@hdss7-21 ~]# vim nginx-ds.yaml
........
image: harbor.od.com/public/nginx:curl
[root@hdss7-21 ~]# kubectl apply -f nginx-ds.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply daemonset.extensions/nginx-ds configured
[root@hdss7-21 ~]# kubectl get svc nginx-dp -o yaml -n kube-public > nginx-dp-svc.yaml
查看
[root@hdss7-21 ~]# kubectl describe ds nginx-ds
Name: nginx-ds
Selector: app=nginx-ds
Node-Selector: <none>
Labels: app=nginx-ds
Annotations: deprecated.daemonset.template.generation: 2
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"extensions/v1beta1","kind":"DaemonSet","metadata":{"annotations":{},"name":"nginx-ds","namespace":"default"},"spec":{"templ...
Desired Number of Nodes Scheduled: 2
Current Number of Nodes Scheduled: 2
Number of Nodes Scheduled with Up-to-date Pods: 0
Number of Nodes Scheduled with Available Pods: 2
Number of Nodes Misscheduled: 0
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx-ds
Containers:
my-nginx:
Image: harbor.od.com/public/nginx:curl
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events: <none>
因此,发现声明式资源管理清单方法,修改参数时,非常方便。
[root@hdss7-21 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-ds-nmgjn 1/1 Running 1 7d23h
nginx-ds-v7hrn 1/1 Running 1 7d23h
陈述式删除(推荐),增,查,删方便
[root@hdss7-21 ~]# kubectl delete svc nginx-ds
service "nginx-ds" deleted
kubectl delete -f nginx-dp-svc.yaml
声明式删除,使用delete -f指定一个yaml文件
[root@hdss7-21 ~]# kubectl delete -f nginx-dp-svc.yaml
service "nginx-dp" deleted
[root@hdss7-21 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 15d
[root@hdss7-21 ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-dp-5dfc689474-k2k8j 1/1 Running 0 22h
[root@hdss7-21 ~]# kubectl get pods nginx-dp-5dfc689474-k2k8j -o yaml -n kube-public
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2020-08-17T14:59:06Z"
generateName: nginx-dp-5dfc689474-
labels:
app: nginx-dp
pod-template-hash: 5dfc689474
name: nginx-dp-5dfc689474-k2k8j
namespace: kube-public
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: nginx-dp-5dfc689474
uid: c13ba8c0-38a5-47aa-9937-e9c040d5cff6
resourceVersion: "1016545"
selfLink: /api/v1/namespaces/kube-public/pods/nginx-dp-5dfc689474-k2k8j
uid: dd34958f-d695-4444-976f-c936c2829303
spec:
containers:
- image: harbor.od.com/public/nginx:v1.7.9
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-bqfhp
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: hdss7-22.host.com
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-bqfhp
secret:
defaultMode: 420
secretName: default-token-bqfhp
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2020-08-17T14:59:06Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2020-08-17T14:59:08Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2020-08-17T14:59:08Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2020-08-17T14:59:06Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://5fbb01b7f9b984acd1ea862d0aceebc46089100b2b7afe05539d50ed659e75aa
image: harbor.od.com/public/nginx:v1.7.9
imageID: docker-pullable://harbor.od.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2
lastState: {}
name: nginx
ready: true
restartCount: 0
state:
running:
startedAt: "2020-08-17T14:59:07Z"
hostIP: 10.4.7.22
phase: Running
podIP: 172.7.22.3
qosClass: BestEffort
startTime: "2020-08-17T14:59:06Z"
获取service
[root@hdss7-21 ~]# kubectl get svc -n kube-public
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.222.251 <none> 80/TCP 22h
[root@hdss7-21 ~]# kubectl get svc nginx-dp -o yaml -n kube-public
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2020-08-17T15:04:08Z"
labels:
app: nginx-dp
name: nginx-dp
namespace: kube-public
resourceVersion: "1016979"
selfLink: /api/v1/namespaces/kube-public/services/nginx-dp
uid: 99681724-4920-4d24-9d40-b6b9618d2362
spec:
clusterIP: 192.168.222.251
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-dp
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
查看相关说明(学习yaml最重要的命令是explain)
[root@hdss7-21 ~]# kubectl explain service.metadata
KIND: Service
VERSION: v1
RESOURCE: metadata <Object>
DESCRIPTION:
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations