k8s記錄-k8s常用對象(Object)類型


3. k8s常用對象(Object)類型

3.1. deployment

主要用於部署pod,支持滾動升級。

 
復制代碼
apiVersion: apps/v1
#對象類型
kind: Deployment
metadata:
  name: nginx-deployment  #deployment名字
  labels:
    app: nginx   #deployment標簽,可以自由定義
spec:
  replicas: 3   #pod 副本數量
  selector:     #pod選擇器定義,主要用於定義根據什么標簽搜索需要管理的pod
    matchLabels:
      app: nginx  #pod標簽
  template:  #pod模版定義
    metadata:
      labels:   #pod 標簽定義
        app: nginx
    spec: 
      containers: #容器數組定義
      - name: nginx  #容器名
        image: nginx:1.7.9  #鏡像地址
        command: #容器啟動命令,【可選】
            - /alidata/www/scripts/start.sh
        ports:  #定義容器需要暴露的端口
            - containerPort: 80
        env: #環境變量定義【可選】
            - name: CONSOLE_URL #變量名
              value: https://www.xxx.com #變量值   
復制代碼

3.2. service

服務定義,主要用於暴露pods容器中的服務。

 
復制代碼
apiVersion: v1
#對象類型
kind: Service
metadata:
  name: my-service #服務名
spec:
  selector: #pod選擇器定義,由這里決定請求轉發給那些pod處理
    app: nginx #pod 標簽
  ports: #服務端口定義
  - protocol: TCP  #協議類型,主要就是TCP和UDP
    port: 80 # 服務端口
    targetPort: 80 #pod 容器暴露的端口
復制代碼

3.3. ingress

http路由規則定義,主要用於將service暴露到外網中

復制代碼
apiVersion: extensions/v1beta1
#對象類型
kind: Ingress
metadata:
  name: my-ingress #ingress應用名
spec:
  rules: #路由規則
    - host: www.xxx.com  #域名
      http:
        paths:  #訪問路徑定義
          - path: /   #代表所有請求路徑
            backend:  #將請求轉發至什么服務,什么端口
              serviceName: my-service #服務名
              servicePort: 80 #服務端口
復制代碼

3.4. ConfigMap

主要用於容器配置管理。

復制代碼
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config #配置項名字
data:
    key1: value1
    key2: value2
復制代碼

定義完配置后,可以通過以下方式在容器中應用配置:

復制代碼
#通過環境變量注入配置
apiVersion: v1
kind: Pod
metadata:
   name: config-pod-1
spec:
   containers:
     - name: test-container
       image: busybox
       command: [ "/bin/sh", "-c", "env" ]
       env:
         - name: SPECIAL_LEVEL_KEY                ## 環境變量
           valueFrom:                             ##使用valueFrom來指定env引用配置項的value值
             configMapKeyRef:
               name: my-config           ##引用的配置文件名稱
               key: key1                 ##引用的配置項key
   restartPolicy: Never
復制代碼
復制代碼
#通過數據卷注入配置
apiVersion: v1
kind: Pod
metadata:
   name: config-pod-4
spec:
   containers:
     - name: test-container
       image: busybox
       command: [ "/bin/sh", "-c", "ls /etc/config/" ]   ##列出該目錄下的文件名
       volumeMounts:
       - name: config-volume    #配置項名字
         mountPath: /etc/config #容器中的掛載目錄
   volumes:         #數據卷定義
     - name: config-volume  #數據卷名
       configMap:           #數據卷類型
         name: my-config    #配置項名字
   restartPolicy: Never


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM