k8s用本身話說,就是一種容器編排工具,部署好應用,再建立綁定應用的服務,就能夠實現的服務訪問了。這個理論仍是得去看重點談理論的文章,此處咱們只記錄本項目部署過程。html
以前已實現gitlab-ci自動集成代碼,部署到tomcat容器,並推送到阿里雲鏡像倉庫。
此項目使用阿里雲的k8s容器服務部署應用。node
快速建立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,進行如下操做nginx
爲了代碼安全,咱們將鏡像倉庫設爲私有,私有倉庫就須要對k8s的受權,在k8s master建secretgit
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,在文件所在路徑下執行docker
kubectl create configmap test.properties --from-file test.properties
建立yaml文件vim
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文件生成相關資源,並查看api
kubectl create -f appstore.yaml kubectl get deployment|grep test-backend-tomcat
建立yaml文件tomcat
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三臺master對應的節點端口。