Kubernetes(十一)YAML配置文件管理资源

  • A+
所属分类:Kubernetes

配置文件说明

定义配置时,指定最新稳定版API(当前为v1);

配置文件应该存储在集群之外的版本控制仓库中。如果需要,可以快速回滚配置、重新创建和恢复;

应该使用YAML格式编写配置文件,而不是JSON。尽管这些格式都可以使用,但YAML对用户更加友好;

可以将相关对象组合成单个文件,通常会更容易管理;

不要没必要的指定默认值,简单和最小配置减少错误;

在注释中说明一个对象描述更好维护。

查看最新的稳定版

[root@k8s-master ~]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

编排nginx deployment yaml

[root@k8s-master tools]# mkdir /root/tools/yaml
[root@k8s-master tools]# vim /root/tools/yaml/nginx.deployment.yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
  labels:
    web: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
        labels:
          app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80

通过yaml创建deployment

[root@k8s-master tools]# kubectl create -f /root/tools/yaml/nginx.deployment.yaml

查看创建结果

[root@k8s-master tools]# kubectl get all -o wide
NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES       SELECTOR
deploy/nginx-deployment   3         3         3            3           1m        nginx        nginx:1.10   app=nginx

NAME                             DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES       SELECTOR
rs/nginx-deployment-67dccb759c   3         3         3         1m        nginx        nginx:1.10   app=nginx,pod-template-hash=67dccb759c

NAME                                   READY     STATUS    RESTARTS   AGE       IP            NODE
po/nginx-deployment-67dccb759c-4s6zc   1/1       Running   0          1m        172.17.5.2    192.168.20.213
po/nginx-deployment-67dccb759c-7mf8c   1/1       Running   0          1m        172.17.44.3   192.168.20.212
po/nginx-deployment-67dccb759c-qjbg2   1/1       Running   0          1m        172.17.44.2   192.168.20.212

NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE       SELECTOR
svc/kubernetes   ClusterIP   10.10.10.1   <none>        443/TCP   21h       <none>

编排nginx service yaml文件

[root@k8s-master tools]# vim /root/tools/yaml/nginx.service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  ports:
  - port: 88
    targetPort: 80
  selector:
    app: nginx

通过yaml部署service

[root@k8s-master tools]# kubectl create -f /root/tools/yaml/nginx.service.yaml

查看service

[root@k8s-master tools]# kubectl get svc
NAME            TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
kubernetes      ClusterIP   10.10.10.1    <none>        443/TCP   21h
nginx-service   ClusterIP   10.10.10.29   <none>        88/TCP    1m

YaLei

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: