Kubernetes yaml 檔案設定

test_service.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
  • apiVersion 定義k8s讀此文件的版本
  • kine 定義要建立的物件種類:Pod, Deployment, Service等
  • metadata 定義物件的屬性,如:name 名稱
  • spec 定義建立物件的規格,可參考:K8s API Reference
  • 執行
kubectl apply -f test_service.yaml

apply result

Pod 介紹

  • A Pod is a grouping of Docker containers running as a unit and one of the main building blocks of kubernetes.
    Pod

service.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx  // 名稱
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9  // container 映像檔
    ports:
    - containerPort: 80  // 對外port

Pod service

deployment.yaml

  • A Deployment manages multiple pods Pods through controllers called ReplicaSets.
apiVersion: apps/v1beta2
kind: Deployment  // 類型
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3  // 3個 instances
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
  • defines how will the deployments target which pods are under its management
  • The templae defines the template of the pods being created
  • 更新 Pod
kubectl apply -f deployment.yaml

Service 介紹

  • A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them – sometimes called a micro-service.
  • service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
  • 執行
kubectl apply -f service.yaml