Kubernetes部署入門

        Kubernetes正成爲雲計算中部署和管理軟件的新標準,提供了自動化部署、系統自愈、水平擴展、服務發現和負載均衡、自動更新和回滾、祕鑰和配置管理、存儲掛載等功能,前期在k8s中部署了springboot項目,把一些配置信息記錄下來。

         1) 創建Docker鏡像

#指定基礎鏡像
FROM hub.xfyun.cn/odeon/openjdk:8-jdk

#創建目錄
RUN groupadd -r hive && useradd -r -g hive hive
RUN mkdir -p /home/hive && chown -R hive:hive /home/hive

#切換到當前目錄
WORKDIR /home/hive

#解壓文件夾
COPY springboot-test-deploy.tar.gz .
RUN tar -xvf springboot-test-deploy.tar.gz && chown -R hive:hive /home/hive/springboot-test-deploy

USER hive
WORKDIR /home/hive/springboot-test-deploy
RUN chmod 755 bin/run.sh

#暴露端口
EXPOSE 9899

#啓動命令
CMD bin/run.sh

        以上爲docker鏡像化的配置文件,裏面主要是一些配置命令,其實就是正常在機器上部署文件時的配置操作。這裏有一點需要注意的是最後的啓動命令中不能採用後臺運行的方式,否則容器啓動之後會立即結束。  在docker鏡像化中這些操作已經被打到鏡像中了。寫好配置文件後,執行下面命令進行鏡像的編譯

docker build -t 172.16.59.153/odeon/springboot:test .

       編譯完成之後,執行命令docker push 172.16.59.153/odeon/springboot:test 將編譯好的鏡像上傳到鏡像庫中,這樣我們在部署文件中就可以引用到該鏡像。

 

       2) 創建部署文件

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: spring-server
    component: spring
  name: spring-server
  namespace: odeon
spec:
  replicas: 1
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  selector:
    matchLabels:
      app: spring-server
      component: spring
  template:
    metadata:
      labels:
        app: spring-server
        component: spring
        monitoring.jmx_exporter.enabled: 'true'
    spec:
      affinity:
        # 利用節點親和性,將服務部署在標籤爲meta的機器上
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: odeon-server
                operator: In
                values:
                - meta
      containers:
        - name: spring-server
          image: 172.16.59.153/odeon/springboot:test
          ports:
          - name: metrics
            containerPort: 9404
          # 容器的資源限定使用
          resources:
            limits:
              memory: "5Gi"
              cpu: "4"
            requests:
              memory: "3Gi"
              cpu: "2"
          volumeMounts:
            - name: server-properties 
              mountPath: /home/hive/springboot-test-deploy/config/server.properties
              subPath: server.properties
            - name: hadoop-conf
              mountPath: /etc/hadoop/conf
            - name: etc-hosts
              mountPath: /etc/hosts
      volumes:
        - name: server-properties
          configMap:
            name: server-properties
        - hostPath:
            path: /root/hadoop_conf_dir
          name: hadoop-conf
        - hostPath:
            path: /etc/hosts
          name: etc-hosts

         創建k8s鏡像化配置中一個主要工作就是掛載配置文件,因爲不同集羣下的配置信息不同,掛載配置的方式一般包括:

         1) 通過hostPath的方式將本地路徑掛載到容器內部,例如上述配置文件中的hadoop-conf

         2) 通過configmap的方式掛載配置

         這裏,我們將服務下面的配置文件創建爲一個configmap,並且在鏡像化時掛載到對應的目錄下,創建configmap的命令如下:

kubectl create configmap server-properties --from-file=server.properties -n odeon

         創建好配置文件和configmap之後,就可以啓動pod了,命令如下:

kubectl apply -f server-deploy.yaml

         啓動成功之後,查看下pod的情況:

        pod啓動成功,只有一個副本,該pod對外暴露的端口是9899,然而此時該端口在外部服務器上是ping不通的,因爲容器內部會虛擬出自己的網卡,並且配置自己的IP,只能在其他容器內部通過pod名方式去訪問,如果需要將服務提供給外部使用,可選的方案包括:

        1) 設置hostNetwork=true,即主機端口模式,此時容器和宿主機共用網卡,但需要注意端口不能和宿主機上已用服務衝突,

        2) 通過配置service來對pod進行統一的管理,service這裏可以充當Nginx的角色。

       通常爲了保證服務的高可用性,一般pod會啓動多個副本,而service將外部的流量轉發到各個pod上,這裏創建的service文件如下:

apiVersion: v1
kind: Service
metadata:
  name: springboot-server
  namespace: odeon
spec:
  type: NodePort
  ports:
   - port: 9899       //集羣中其它container訪問
     nodePort: 31016  //節點對外暴露的端口
  selector:
    component: spring

        啓動service:kubectl apply -f service-deploy.yaml,查看啓動的service服務:

        此時,訪問k8s集羣中的任意一臺機器加上端口31016就可以訪問到服務。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章