Windows docker k8s asp.net core

在上一篇文章 Ubuntu 18 Kubernetes的Install and Deploy 咱們在ubuntu在部署了k8s集羣, 今天來看看windows下怎麼搞。html

主要點有:node

1) windows 下搭建k8s 單節點git

2)ap.net core 製做和發佈鏡像 ,重點在於發佈到私有的harbor上(Ubuntu18 安裝搭建Harbor​​​​​​​)github

3)部署到k8s集羣上web

安裝

1.下載 k8s-for-docker-desktop,前查看本身docker的版本(我這裏是19.03.1因此直接下載master),而後下載對應的分支docker

2.切換到對應的目錄,開始加載鏡像,這裏經過PowerShell的方式加載: .\load_images.ps1 (也能夠經過Bash Shell的方式加載:./load_images.sh), 若是出現:在此係統上禁止運行腳本 的錯誤,經過執行 set-ExecutionPolicy RemoteSigned 能夠解決shell

拉取完畢後(須要檢查是否所有成功拉取) 須要在在Docker for Windows中啓用K8Subuntu

配置K8s

1.切換運行上下文至docker-for-desktop ,驗證Kubernetes狀態(目前是單節點)windows

kubectl config use-context docker-for-desktop
kubectl cluster-info
kubectl get nodes

2.部署Kubernetes Dashboard& 開啓API Server訪問代理,向外部提供面板訪問api

kubectl create -f kubernetes-dashboard.yaml #須要在k8s-for-docker-desktop 目錄下執行
kubectl proxy --address='0.0.0.0' --port=8008 --accept-hosts='^*$'

建立用戶並獲取token

kubectl create serviceaccount dashboard -n default
kubectl create clusterrolebinding dashboard-admin -n default --clusterrole=cluster-admin --serviceaccount=default:dashboard
kubectl -n kube-system get secret
kubectl -n kube-system describe secret  <name>


訪問http://localhost:8008/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/login 如圖:

輸入token後

準備一個k8sWebApi

在建立webapi項目的(建立的時候選擇啓用docker 會自動生成Dockerfiel文件)

而後切換到目錄自做鏡像 併發布到私有倉庫:(有關harbor請參考 Ubuntu18 安裝搭建Harbor

docker build -t k8swebapi .
#給鏡像打tag(鏡像的格式爲,鏡像倉庫IP:端口/鏡像名稱)
docker tag  k8swebapi 192.168.100.3:80/repo-test/k8swebapi:20190809
docker push 192.168.100.3:80/repo-test/k8swebapi
爲了保險 咱們找一個docker 環境驗證一下:

部署webapi到k8s

1.準備namespace.yaml 

apiVersion: v1
kind: Namespace
metadata:
   name: aspnetcore
   labels:
     name: aspnetcore

deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8swebapi
  namespace: aspnetcore
  labels:
    name: k8swebapi
spec:
  replicas: 2
  selector:
    matchLabels:
      name: k8swebapi
  template:
    metadata:
      labels:
        name: k8swebapi
    spec:
      imagePullSecrets:
      - name: regsecret
      containers:
      - name: k8swebapi
        image: 192.168.100.3:80/repo-test/k8swebapi:20190809
        ports:
        - containerPort: 80
        imagePullPolicy: Always

---

kind: Service
apiVersion: v1
metadata:
  name: k8swebapi
  namespace: aspnetcore
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
  selector:
    name: k8swebapi

這裏這個deploy.yaml就會告訴K8S關於你的API的全部信息,以及經過什麼樣的方式暴露出來讓外部訪問。須要注意的是,這裏咱們提早爲要部署的ASP.NET Core WebAPI項目建立了一個namespace,叫作aspnetcore,所以這裏寫的namespace : aspnetcore。K8S中經過標籤來區分不一樣的服務,所以這裏統一name寫成了k8swebapi。在多實例的配置上,經過replicas : 2這個設置告訴K8S給我啓動2個實例起來,固然你能夠寫更大的一個數量值。 最後,在spec中告訴K8S我要經過NodePort的方式暴露出來公開訪問。這裏由於是私有的harbor因此須要建立regsecret認證,而後在deploy文件引用它:有關向信息能夠參考https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/

kubectl create secret docker-registry regsecret --docker-server=192.168.100.3:80 --docker-username=admin --docker-password=xxx -n=aspnetcore
#kubectl delete secret regsecret  -n=aspnetcore

注意咱們的deploy是有名稱空間的, 因此在建立 secret的時候必須指定-n=aspnetcore

2.經過kubectl部署到K8S (如下操做在ubuntu下的k8s集羣一樣適用)

首先,確保你的Docker for Windows以及Kubernetes都啓動起來了。而後,在Powershell中經過kubectl完成API的部署,而後驗證,

kubectl apply -f namespace.yaml
kubectl apply -f deploy.yaml
kubectl get svc -n aspnetcore
#刪除的時候順序要倒過來
#kubectl delete -f deploy.yaml
#kubectl delete -f namespace.yaml

#查看k8swebapi狀態
kubectl get deployment k8swebapi -n aspnetcore
kubectl describe deployment k8swebapi -n aspnetcore
kubectl describe replicaset -n aspnetcore #查看ReplicaSet的狀態
kubectl describe pod -n aspnetcore #查看Pod的狀態

我這裏的http://localhost:xxx/api/values 在ubuntu下是不能訪問的(在windows下的k8s 是1.14.3,ubuntu是1.15.2 估計是版本的問題)

3.在K8S中對WebAPI的伸縮

在Dashboard中,咱們能夠可視化地對咱們的Deployment進行容器實例的伸縮,在彈出的伸縮選項對話框中輸入個數,例如咱們這裏從2個縮減爲1個,而後肯定。以下圖所示:

除了在Dashboard中可視化地操做進行伸縮,也能夠經過kubectl來進行,例以下面這句命令,將容器實例擴展到3個。須要注意的是,因爲咱們的k8swebapi所在的命名空間是在aspnetcore下,所以也須要指明--namespace=aspnetcore。

 kubectl scale deployment k8swebapi --replicas=3 --namespace=aspnetcore

在K8S中,提供了一個autoscale接口來實現服務的自動伸縮,它會採用默認的自動伸縮策略(例如根據CPU的負載狀況)來幫助咱們實現彈性伸縮的功能。例以下面這句命令能夠實現咱們的k8s-demo能夠伸縮的範圍是1~3個,根據負載狀況本身伸縮,在沒有多少請求量壓力很小時收縮爲一個,在壓力較大時啓動另外一個實例來下降負載。

kubectl autoscale deployment k8swebapi --min=1 --max=3 --namespace=aspnetcore

來一個ubuntu18 k8s集羣下面的機截圖

--2019-8-15

滾動更新

修改deploy.yaml文件,並執行 kubectl apply -f deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8swebapi
  namespace: aspnetcore
  labels:
    name: k8swebapi
spec:
  replicas: 5
  minReadySeconds: 10 
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    
  selector:
    matchLabels:
      name: k8swebapi
  template:
    metadata:
      labels:
        name: k8swebapi
    spec:
      imagePullSecrets:
      - name: regsecret
      containers:
      - name: k8swebapi
        image: 192.168.100.5:80/admin/k8swebapi:20190809
        ports:
        - containerPort: 80
        imagePullPolicy: Always

---

kind: Service
apiVersion: v1
metadata:
  name: k8swebapi
  namespace: aspnetcore
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
  selector:
    name: k8swebapi

修改程序從新push

修改deploy的鏡像路徑  image: 192.168.100.5:80/admin/k8swebapi:20190815 ,而後執行 kubectl apply -f deploy.yaml 更新後以下,api已經發生變化

參考 

K8S的滾動升級RollingUpdate​​​​​​​

k8s 滾動升級

K8s-yaml的使用及命令

ASP.NET Core on K8S深刻學習(1)K8S基礎知識與集羣搭建

ASP.NET Core on K8S學習初探(2)K8S基本概念快速一覽

ASP.NET Core on K8S學習初探(3)部署API到K8S

ASP.NET Core on K8S深刻學習(3)Deployment

相關文章
相關標籤/搜索