きなこもち.net

.NET Framework × UiPath,Orchestrator × Azure × AWS × Angularなどの忘備録

Docker for Windows × 環境構築 × インストールからダッシュボード設定まで

この記事の目的

この記事では、
Docker for WindowsでWindows10にk8s環境を構築すること
を目的としています。

本題

★はじめに

これまで、とにかくk8sを実行してイメージをつかみたい。という目的のもとAzureのサービス(AKS)を使って作業を進めてきました。ある程度動作を確認することができたので、今度は、腰を据えてk8s基本動作から学びなおそうと思います。基本が身についたら、自作アプリの運用などの方面に手を伸ばしていきたいと考えています!
 本記事では、基礎学習を行うにあたり、ローカルにk8sの環境を構築します。そして、Azureで良しなに整備されてきたいろいろな処理や設定を一つ一つ見直していこうと思います。

★環境

K8sの環境をローカルに構築するにあたり、以下の環境を利用します。
Windows 10 Pro バージョン1803
Docker for Windows バージョン18.06.1-ce-win73(19507)
なお、Docker for Windowsはすでにインストール済みの状態から始めます。

★インストール

早速インストールをしていきます。Docker for Windows のアイコンを右クリックし、【コンテキストメニュー】-【Settings】をクリックします。
f:id:kinakomotitti:20180906235206p:plain
すると、Settingsダイアログが表示されます。
f:id:kinakomotitti:20180906235213p:plain
左のメニューにKubernetesがありますね・・・この【Kubernetes】をクリックします。
f:id:kinakomotitti:20180906235217p:plain
この画面からKubernetesを有効にすることができます。【Enable Kubernetes】のチェックボックスをOnにします。すると、デフォルトのDoker stack commandsを【kubernetes】と【swarm】の2つから選択することができます。迷わず【kubernetes】を選択します。また、Enable Kubernetesの下のチェックボックスに【Show system containers】チェックボックスがあります。Docker コマンドを実行するとき、Kubernetesの内部コンテナを表示するかを設定することができるようです。advancedとなっているので、こちらもチェックしておきます。
f:id:kinakomotitti:20180906235224p:plain
そして、【Apply】ボタンを押下します。すると、時間がかかるけどいいか?(´▽`)的なメッセージが表示されるので、【Install】ボタンを押下してインストールを実行します。
f:id:kinakomotitti:20180906235232p:plain

10分後・・・
f:id:kinakomotitti:20180906235236p:plain
キタ――(゚∀゚)――!!

★kubectlのインストール

続いて、k8sを管理するコマンドをインストールしていきます。kubectlをインストールは、公式のSetupに従い【choco】コマンドを利用します。実行するときには、コマンドプロンプトを管理者権限で起動しておくことを忘れないで下さい。インストール自体は、以下のコマンドを実行することでできます。
参考)
chocolatey.org

choco install kubernetes-cli
#The install of kubernetes-cli was successful.
k8sダッシュボードのインストール

以下のコマンドを実行して、k8sダッシュボードをインストールします。インストール・・・というかデプロイする方法は以下の通りですが、証明書の設定とか避けたいので、今回は、代替設定を利用します。

#推奨設定でデプロイ
#HTTPSでアクセスします。証明書が必要です。
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.8.3/src/deploy/recommended/kubernetes-dashboard.yaml

[https://raw.githubusercontent.com/kubernetes/dashboard/v1.8.3/src/deploy/recommended/kubernetes-dashboard.yaml]
→kubernetes-dashboard   ClusterIP    10.96.247.93   <none>        443:30551/TCP   1h

#代替設定でデプロイ
#セキュリティが弱くなりますが、HTTPでアクセスできるようになります。
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/alternative/kubernetes-dashboard.yaml
→kubernetes-dashboard   ClusterIP    10.96.199.140   <none>        80:30666/TCP    1m

参考)
github.com

学習のため、このコマンドで使っているyamlファイルの中身も確認しておきます。ServiceAccount、Role、RoleBinding、Deployment、Serviceがそれぞれどのようなパラメータとして生成されているかを中心に見ます(/・ω・)/

# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------- Dashboard Service Account ------------------- #
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
---
# ------------------- Dashboard Role & Role Binding ------------------- #
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
rules:
  # Allow Dashboard to create 'kubernetes-dashboard-key-holder' secret.
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create"]
  # Allow Dashboard to create 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["create"]
  # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
- apiGroups: [""]
  resources: ["secrets"]
  resourceNames: ["kubernetes-dashboard-key-holder"]
  verbs: ["get", "update", "delete"]
  # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
  resources: ["configmaps"]
  resourceNames: ["kubernetes-dashboard-settings"]
  verbs: ["get", "update"]
  # Allow Dashboard to get metrics from heapster.
- apiGroups: [""]
  resources: ["services"]
  resourceNames: ["heapster"]
  verbs: ["proxy"]
- apiGroups: [""]
  resources: ["services/proxy"]
  resourceNames: ["heapster", "http:heapster:", "https:heapster:"]
  verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: kubernetes-dashboard-minimal
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard
  namespace: kube-system
---
# ------------------- Dashboard Deployment ------------------- #
kind: Deployment
apiVersion: apps/v1beta2
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      containers:
      - name: kubernetes-dashboard
        image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.0
        ports:
        - containerPort: 9090
          protocol: TCP
        args:
          # Uncomment the following line to manually specify Kubernetes API server Host
          # If not specified, Dashboard will attempt to auto discover the API server and connect
          # to it. Uncomment only if the default does not work.
          # - --apiserver-host=http://my-address:port
        volumeMounts:
          # Create on-disk volume to store exec logs
        - mountPath: /tmp
          name: tmp-volume
        livenessProbe:
          httpGet:
            path: /
            port: 9090
          initialDelaySeconds: 30
          timeoutSeconds: 30
      volumes:
      - name: tmp-volume
        emptyDir: {}
      serviceAccountName: kubernetes-dashboard
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
---
# ------------------- Dashboard Service ------------------- #
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  ports:
  - port: 80
    targetPort: 9090
  selector:
    k8s-app: kubernetes-dashboard

はい。見ましたねw
では、次に、コマンドを実行した後、正しくリソースが生成されているかを確認します。

#生成されたノードの確認
kubectl get pod --namespace=kube-system

#生成されたサービスの確認
kubectl get service --namespace=kube-system

正しくリソースが生成され、podStatusがReadyになっていることを確認したら、いよいよブラウザでアクセスします。以下のURLにアクセスします。




と、言いたいところですが、もうひと作業します。今のところ、作成したServiceのtypeは、「ClusterIP」が設定されています。クラスター外部からダッシュボードにアクセスするためには、k8sProxyサーバーを起動するか、このサービスを「NodePort」typeに変更するかのどちらかの作業が必要です。本記事では、後者のNodePortに変更するを選択します。
参考)
github.com

以下のコマンドを実行し、yamlをnotepadで開きます。開いたらserviceのtypeをNodePortに変更→保存→閉じるします。

kubectl -n kube-system edit service kubernetes-dashboard

もう一度、サービスの状態を確認して、タイプが変更されていることが確認できたらOKです。

kubectl get service --namespace=kube-system
#出力
#kubernetes-dashboard   NodePort    10.96.199.140   <none>        80:30666/TCP    8m

以上で設定が完了となります。ブラウザを開いて、http://127.0.0.1:30666/にアクセスします。すると、こんな感じの画面が表示されます。
f:id:kinakomotitti:20180906235250p:plain

Kubectl getコマンドで見ていた情報がGUIで分かりやすく表示されています。これはいいですね(*'▽')
と、いうことで、環境構築はここまでとなります。