gitlab-ci + k8s 之k8s (二)


k8s用自己話說,就是一種容器編排工具,部署好應用,再創建綁定應用的服務,就可以實現的服務訪問了。這個理論還是得去看重點談理論的文章,此處我們只記錄本項目部署過程。

背景介紹

之前已實現gitlab-ci自動集成代碼,部署到tomcat容器,並推送到阿里雲鏡像倉庫。
此項目使用阿里雲的k8s容器服務部署應用。

一系列准備工作,先參考阿里雲文檔

快速創建Kubernetes集群
https://help.aliyun.com/document_detail/85903.html?spm=a2c4g.11186623.6.555.534c4b4bY7F07R
SSH訪問Kubernetes集群
https://help.aliyun.com/document_detail/86491.html?spm=5176.11065259.1996646101.searchclickresult.37524c20dma8gQ
通過以上SSH訪問Kubernetes集群的負載均衡地址登錄到集群master,進行以下操作

創建secret

為了代碼安全,我們將鏡像倉庫設為私有,私有倉庫就需要對k8s的授權,在k8s master建secret

kubectl create secret docker-registry test-secret --docker-server=registry-vpc.cn-shanghai.aliyuncs.com/testimage/test --docker-username=uraliyun_name --docker-password=repo_passwd --docker-email=mail@mail.com

創建掛載卷

由於我項目的特殊需求,在鏡像里除了war包外還有一個外部配置文件,我需要將外部配置文件掛載到pod的指定目錄下
現將配置文件傳到本服務器中,此處先將配置文件做成configmap,在文件所在路徑下執行

kubectl create configmap test.properties --from-file test.properties

以yaml文件的形式創建deploy

創建yaml文件

vim test.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: test-backend-tomcat
  labels:
    app: testbackend
spec:
  replicas: 1     #副本集,定義pod數
  selector:
    matchLabels:
      app: testbackend
  template:
    metadata:
      labels:       #標簽,pod svc等資源是以標簽關聯,所以不同應用要打上不同標簽
        app: testbackend
    spec:
      imagePullSecrets:                                            #創建的secret
      - name: test-backend-secret
      containers:
      - name: test-backend-tomcat
        image: registry-vpc.cn-shanghai.aliyuncs.com/testimage/test-backend:1  #鏡像
        resources:                                       #資源限制,最大值與請求值
          limits:
            cpu: "0.5"
            memory: 100Mi
          requests:
            cpu: "0.2"
            memory: 50Mi
        ports:
        - containerPort: 8080                                  
        volumeMounts:                                              #掛載卷
        - name: test-backend-volume
          mountPath: /rogueone/testconfig/
        - name: test-backend-log-volume   #將worker節點上/logs/test-backend做成一個名為test-backend-log-volume的存儲卷,再將該存儲卷掛載在pod的容器里的/logs下
          mountPath: /logs 
      volumes:
      - name: test-backend-volume
        configMap:
         name: test.properties
      - name: test-backend-log-volume        
        hostPath:                                     #卷的類型是hostpath,即worker節點目錄掛載到容器類型的卷
          path: /logs/test-backend
          type: Directory

以yaml文件生成相關資源,並查看

kubectl create -f appstore.yaml
kubectl get deployment|grep  test-backend-tomcat

創建關聯以上deploy的nodeport 類型的svc

創建yaml文件

vim test_svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  ports:
  - port: 30080                   #服務端口,供集群內部訪問使用
    targetPort: 8080           # 容器端口,容器中提供服務的端口
    nodePort: 30001           #節點端口,在節點上對外開放的端口
 selector:
   app: testbackend         #此處標簽要與deploy中matchlabel一致

根據以上yaml文件創建對應的服務,並查看

kubectl create -f  test_svc.yaml
kubectl get svc |grep nginx-service

此時在k8s的master與worker上都開放了對應的節點端口可以訪問到容器服務

通過阿里雲負載均衡訪問到k8s

創建一個負載均衡,通過不同端口區別不同服務,同一個端口負載到k8s三台master對應的節點端口。


免責聲明!

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



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