k8s资源编排

资源编排

语法:

缩进表示层级关系

不支持制表符"tab"缩进,使用空格缩进

通常开头缩进2个空格

字符后缩进一个空格,如冒号,逗号

“—” 表示yaml格式,一个文件的开头

“#“ 注释

创建目录

[root@master opt]# mkdir manifest
[root@master opt]# ls
manifest

查看deployment类型资源编排怎么定义

deployment类型


[root@master ~]# kubectl explain deployment
KIND:     Deployment
VERSION:  apps/v1

DESCRIPTION:
     Deployment enables declarative updates for Pods and ReplicaSets.
.......

pod类型

[root@master ~]# kubectl explain pods
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   
     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 
     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     
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec 
     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       
     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


 
  

service类型

[root@master ~]# kubectl explain service
KIND:     Service
VERSION:  v1

DESCRIPTION:
     Service is a named abstraction of software service (for example, mysql)
     consisting of local port (for example 3306) that the proxy listens on, and
     the selector that determines which pods will answer requests sent through
     the proxy.

FIELDS:
   apiVersion   
     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 
     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     
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec 
     Spec defines the behavior of a service.
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       
     Most recently observed status of the service. Populated by the system.
     Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
 
  

创建一个yaml文件

[root@master manifest]# cat deployment.yaml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels: 
      app: apache
  template:
    metadata:
      labels: 
        app: apache
    spec:
      containers:
      - image: sktystwd/apache:v0.1
        imagePullPolicy: IfNotPresent
        name: httpd

执行文件

[root@master manifest]# kubectl apply -f deployment.yaml 
deployment.apps/web unchanged
[root@master manifest]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-6bcd5cffd7-4vbd8   1/1     Running   0          8m37s
web-6bcd5cffd7-jrgh9   1/1     Running   0          8m37s
web-6bcd5cffd7-m5wlt   1/1     Running   0          8m37s

给容器创建service

[root@master manifest]# cat svc-deployment.yaml 
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec: 
  ports:
  - port: 8001
    protocol: TCP
    targetPort: 80
  selector:
    app: apache
  type: NodePort

执行

[root@master manifest]# kubectl apply -f svc-deployment.yaml
service/web unchanged
[root@master manifest]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1               443/TCP          2d23h
web          NodePort    10.105.226.37           8001:30220/TCP   8m55s

删除

[root@master manifest]# kubectl delete -f deployment.yaml 
deployment.apps "web" deleted
[root@master manifest]# kubectl get podsNAME                   READY   STATUS        RESTARTS   AGE
web-6bcd5cffd7-4vbd8   1/1     Terminating   0          10m
web-6bcd5cffd7-jrgh9   1/1     Terminating   0          10m
web-6bcd5cffd7-m5wlt   1/1     Terminating   0          10m
[root@master manifest]# kubectl get podsNo resources found in default namespace.

删除service

[root@master manifest]# kubectl delete -f svc-deployment.yaml 
service "web" deleted
[root@master manifest]# kubectl get svcNAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1            443/TCP   2d23h

也可以写成一个文件

[root@master manifest]# cat deployment.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels: 
      app: apache
  template:
    metadata:
      labels: 
        app: apache
    spec:
      containers:
      - image: sktystwd/apache:v0.1
        imagePullPolicy: IfNotPresent
        name: httpd

---
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec: 
  ports:
  - port: 8001
    protocol: TCP
    targetPort: 80
  selector:
    app: apache
  type: NodePort

执行

[root@master manifest]# kubectl apply -f deployment.yaml 
deployment.apps/web created
service/web created
[root@master manifest]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-6bcd5cffd7-4fdjd   1/1     Running   0          18s
web-6bcd5cffd7-scnnx   1/1     Running   0          18s
web-6bcd5cffd7-txxg2   1/1     Running   0          18s
[root@master manifest]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1                443/TCP          2d23h
web          NodePort    10.101.176.221           8001:32673/TCP   22s

更新

那先访问一下

k8s资源编排_第1张图片

修改一下文件

[root@master manifest]# cat deployment.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels: 
      app: apache
  template:
    metadata:
      labels: 
        app: apache
    spec:
      containers:
      - image: sktystwd/apache:v0.2   #改成需要更新的容器名及版本
        imagePullPolicy: IfNotPresent
        name: httpd

---
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec: 
  ports:
  - port: 8001
    protocol: TCP
    targetPort: 80
  selector:
    app: apache
  type: NodePort

执行

[root@master manifest]# kubectl apply -f deployment.yaml 
deployment.apps/web configured
service/web unchanged

查看

[root@master manifest]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-5dc777c866-9xz9b   1/1     Running   0          95s
web-5dc777c866-bpp2c   1/1     Running   0          97s
web-5dc777c866-zf4js   1/1     Running   0          96s


[root@master manifest]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.96.0.1                443/TCP          3d
web          NodePort    10.101.176.221           8001:32673/TCP   8m2s

访问

k8s资源编排_第2张图片

自动生成yaml

[root@master manifest]# kubectl create deployment nginx --image=nginx  --dry-run=client -o yaml > nginx.yaml
[root@master manifest]# ls
deployment.yaml  nginx.yaml  svc-deployment.yaml
[root@master manifest]# cat nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

用get命令导出

[root@master manifest]# kubectl create deployment nginx --image nginx --replicas 3
deployment.apps/nginx created
[root@master manifest]# kubectl get deployment nginx -o yaml > nginx2.yaml
[root@master manifest]# ls
deployment.yaml  nginx2.yaml
nginx.yaml       svc-deployment.yaml

[root@master manifest]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-6dnjv   1/1     Running   0          71s
nginx-6799fc88d8-9cvms   1/1     Running   0          71s
nginx-6799fc88d8-tr4fv   1/1     Running   0          71s


[root@master manifest]# cat nginx2.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2021-12-21T15:08:24Z"
  generation: 1
  labels:
    app: nginx
  managedFields:
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:labels:
          .: {}
          f:app: {}
      f:spec:
        f:progressDeadlineSeconds: {}
        f:replicas: {}
        f:revisionHistoryLimit: {}
        f:selector: {}
        f:strategy:
          f:rollingUpdate:
            .: {}
            f:maxSurge: {}
            f:maxUnavailable: {}
          f:type: {}
        f:template:
          f:metadata:
            f:labels:
              .: {}
              f:app: {}
          f:spec:
            f:containers:
              k:{"name":"nginx"}:
                .: {}
                f:image: {}
                f:imagePullPolicy: {}
                f:name: {}
                f:resources: {}
                f:terminationMessagePath: {}
                f:terminationMessagePolicy: {}
            f:dnsPolicy: {}
            f:restartPolicy: {}
            f:schedulerName: {}
            f:securityContext: {}
            f:terminationGracePeriodSeconds: {}
    manager: kubectl-create
    operation: Update
    time: "2021-12-21T15:08:24Z"
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:deployment.kubernetes.io/revision: {}
      f:status:
        f:availableReplicas: {}
        f:conditions:
          .: {}
          k:{"type":"Available"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
          k:{"type":"Progressing"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
        f:observedGeneration: {}
        f:readyReplicas: {}
        f:replicas: {}
        f:updatedReplicas: {}
    manager: kube-controller-manager
    operation: Update
    time: "2021-12-21T15:08:52Z"
  name: nginx
  namespace: default
  resourceVersion: "70307"
  uid: 04a19f3a-b899-4586-856f-5b3c7ce1487c
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 3
  conditions:
  - lastTransitionTime: "2021-12-21T15:08:52Z"
    lastUpdateTime: "2021-12-21T15:08:52Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2021-12-21T15:08:25Z"
    lastUpdateTime: "2021-12-21T15:08:52Z"
    message: ReplicaSet "nginx-6799fc88d8" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 3
  replicas: 3
  updatedReplicas: 3

命名空间

[root@master manifest]# cat deployment2.yaml 
---
apiVersion: v1     #命名空间
kind: Namespace
metadata:
  name: runtime

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: runtime #跟命名空间一致
spec:
  replicas: 3
  selector:
    matchLabels: 
      app: apache
  template:
    metadata:
      labels: 
        app: apache
    spec:
      containers:
      - image: sktystwd/apache:v0.2
        imagePullPolicy: IfNotPresent
        name: httpd

---
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: runtime   #跟命名空间一致
spec: 
  ports:
  - port: 8001
    protocol: TCP
    targetPort: 80
  selector:
    app: apache
  type: NodePort



[root@master manifest]# kubectl create -f deployment2.yaml 
namespace/runtime created
deployment.apps/web created
service/web created

查看命名空间

[root@master manifest]# kubectl get ns
NAME              STATUS   AGE
default           Active   3d1h
kube-node-lease   Active   3d1h
kube-public       Active   3d1h
kube-system       Active   3d1h
runtime           Active   92s
[root@master manifest]# kubectl get pods -n runtime
NAME                   READY   STATUS    RESTARTS   AGE
web-5dc777c866-cngw5   1/1     Running   0          2m10s
web-5dc777c866-sbxzf   1/1     Running   0          2m10s
web-5dc777c866-shxhp   1/1     Running   0          2m10s

[root@master manifest]# kubectl get svc -n runtime
NAME   TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
web    NodePort   10.110.105.67           8001:32530/TCP   21s

#创建了命名空间只能这样查看
[root@master manifest]# kubectl get deployment -n runtime
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    3/3     3            3           40s
[root@master manifest]# kubectl get deployment
No resources found in default namespace.

创建haproxy

创建镜像

[root@master manifest]# kubectl run web --image sktystwd/apache:v0.1
pod/web created

[root@master manifest]# kubectl run web1 --image sktystwd/apache:v0.2
pod/web1 created

添加service

[root@master manifest]# kubectl expose pods web --port 80 --target-port 80
service/web exposed
[root@master manifest]# kubectl expose pods web1 --port 80 --target-port 80
service/web1 exposed
[root@master manifest]# kubectl get svcNAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1               443/TCP   3d1h
web          ClusterIP   10.109.58.148           80/TCP    10s
web1         ClusterIP   10.107.246.15           80/TCP    4s

访问

[root@master manifest]# curl 10.109.58.148
test page with v1
[root@master manifest]# curl 10.107.246.15
test page with v2

把IP写入一个文件中

[root@master opt]# mkdir data
[root@master opt]# ls
apache  cni  containerd  date  manifest
[root@master opt]# cd  date/
[root@master date]# ls
[root@master date]# vim bs.tst
[root@master date]# cat bs.tst 
10.109.58.148
10.107.246.15

创建haproxy

[root@master manifest]# cat deployment3.yaml 
---
apiVersion: v1
kind: Pod
metadata:
  name: haproxy
  namespace: default
spec:
  containers:
  - image: sktystwd/haproxy:v0.1
    imagePullPolicy: IfNotPresent
    name: haproxy
    volumeMounts:
    - name: sys-bs
      mountPath: /config
  volumes:
  - name: sys-bs
    hostPath:
      path: /opt/date

---
apiVersion: v1
kind: Service
metadata:
  name: haproxy
  namespace: default
spec: 
  ports:
  - port: 8008
    protocol: TCP
    targetPort: 80
  selector:
    app: haproxy
  type: NodePort


执行

[root@master manifest]# kubectl create -f deployment3.yaml 
pod/haproxy created
service/haproxy created
[root@master manifest]# kubectl get pods
NAME      READY   STATUS              RESTARTS   AGE
haproxy   0/1     ContainerCreating   0          58s
web       1/1     Running             0          54m
web1      1/1     Running             0          54m

你可能感兴趣的:(docker,k8s)