2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

YoctoLinuxで64bit RaspberryPi環境を構築し、k3sを使用する(2)

Last updated at Posted at 2020-04-12

(1)で64bitOS環境が準備出来たので、k3sの環境を作成します。

k3s

k3sの環境準備

以下を参考にさせていただきました。

Qiita k3s+MetalLBの環境を構築してKubernetes-Dashboardをデプロイする

  1. インストール
    k3sのLoadBalancerを無効化する設定でインストール

    $ curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--no-deploy servicelb" sh -
    [INFO]  Finding latest release
    [INFO]  Using v1.17.4+k3s1 as release
    [INFO]  Downloading hash https://github.com/rancher/k3s/releases/download/v1.17.4+k3s1/sha256sum-arm64.txt
    [INFO]  Downloading binary https://github.com/rancher/k3s/releases/download/v1.17.4+k3s1/k3s-arm64
    [INFO]  Verifying binary download
    [INFO]  Installing k3s to /usr/local/bin/k3s
    Password:            <-パスワード[raspberry]を入力
    sudo: chcon: command not found     <-SELinuxがインストールされていない場合に表示される、動作に問題はない模様
    [INFO]  Creating /usr/local/bin/kubectl symlink to k3s
    [INFO]  Creating /usr/local/bin/crictl symlink to k3s
    [INFO]  Creating /usr/local/bin/ctr symlink to k3s
    [INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
    [INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
    [INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
    [INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
    [INFO]  systemd: Enabling k3s unit
    Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
    [INFO]  systemd: Starting k3s
    

    動作状態を確認

    $ sudo kubectl get node
    Password:            <-パスワード[raspberry]を入力
    NAME       STATUS   ROLES    AGE   VERSION
    yoctopi3   Ready    master   12m   v1.17.4+k3s1
    
  2. MetalLBインストール
    公式サイトのInstallationに沿ってインストール

    $ sudo kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/namespace.yaml
    namespace/metallb-system created
    $ sudo kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/metallb.yaml
    podsecuritypolicy.policy/controller created
    podsecuritypolicy.policy/speaker created
    serviceaccount/controller created
    serviceaccount/speaker created
    clusterrole.rbac.authorization.k8s.io/metallb-system:controller created
    clusterrole.rbac.authorization.k8s.io/metallb-system:speaker created
    role.rbac.authorization.k8s.io/config-watcher created
    role.rbac.authorization.k8s.io/pod-lister created
    clusterrolebinding.rbac.authorization.k8s.io/metallb-system:controller created
    clusterrolebinding.rbac.authorization.k8s.io/metallb-system:speaker created
    rolebinding.rbac.authorization.k8s.io/config-watcher created
    rolebinding.rbac.authorization.k8s.io/pod-lister created
    daemonset.apps/speaker created
    deployment.apps/controller created
    $ sudo kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
    secret/memberlist created
    

    インストール後、Configurationの内容を見ながらconfig用yamlファイルを作る。
    単純にk3sのNodeと同じサブネットに参加させるlb-configmap.yamlを作成。

    lb-configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      namespace: metallb-system
      name: config
    data:
      config: |
        address-pools:
        - name: default
          protocol: layer2
          addresses:
          - 192.168.25.240-192.168.25.250
    

    適用する

    $ sudo kubectl apply -f lb-configmap.yaml
    configmap/config created
    

    確認

    $ sudo kubectl -n metallb-system get pods
    NAME                          READY   STATUS    RESTARTS   AGE
    controller-5c9894b5cd-g25hk   1/1     Running   0          20m
    speaker-8cjlx                 1/1     Running   0          20m
    
    $ sudo kubectl -n metallb-system describe configmap config
    Name:         config
    Namespace:    metallb-system
    Labels:       <none>
    Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"config":"address-pools:\n- name: default\n  protocol: layer2\n  addresses:\n  - 192.168.25.240-192.168.25.250\...
    
    Data
    ====
    config:
    ----
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.25.240-192.168.25.250
    
    Events:  <none>
    $
    
  3. nginxコンテナを動作させてみる

    レプリカ数3でデプロイしてみる

    $ sudo kubectl run nginx --image nginx --replicas 3
    kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
    deployment.apps/nginx created
    

    各種コマンドで確認

    $ sudo kubectl get deployments
    NAME    READY   UP-TO-DATE   AVAILABLE   AGE
    nginx   3/3     3            3           9m10s
    $ sudo kubectl get replicasets
    NAME               DESIRED   CURRENT   READY   AGE
    nginx-6db489d4b7   3         3         3       9m40s
    $ sudo kubectl get pods --output=wide
    NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
    nginx-6db489d4b7-czkxw   1/1     Running   0          10m   10.42.0.8    yoctopi3   <none>           <none>
    nginx-6db489d4b7-n6mfs   1/1     Running   0          10m   10.42.0.10   yoctopi3   <none>           <none>
    nginx-6db489d4b7-x5vt2   1/1     Running   0          10m   10.42.0.9    yoctopi3   <none>           <none>
    

    nginxのpodが3個起動し、IPアドレスが10.42.0.8~10.42.0.10が割り当てられている。
    これらにMetalLBを使ってアクセス出来る様にする。

    $ sudo kubectl expose deployment nginx --port 80 --type LoadBalancer --name nginxex
    service/nginxex exposed
    $ sudo kubectl get service
    NAME         TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)        AGE
    kubernetes   ClusterIP      10.43.0.1       <none>           443/TCP        67m
    nginxex      LoadBalancer   10.43.218.232   192.168.25.241   80:30262/TCP   12s
    

    nginxexサービスの「CLUSTER-IP」と「EXTERNAL-IP」がアクセス出来るIPアドレスになる。
    K3sを動作させているマシン(RaspberryPi)から「http://10.43.218.232/」(CLUSTER-IP)、他のマシンから「http://192.168.25.241/」(EXTERNAL-IP)でアクセスすると、nginxのWelcomeページが表示される。
    (「⁠PORT」のコロンの後ろが外部に見えているポートになっているので、これを使用して、「http://[RaspberryPiのIPアドレス]:30262/」でもアクセス出来る。)
    nginxexサービスの詳細を確認する。

    $ sudo kubectl describe service nginxex
    Name:                     nginxex
    Namespace:                default
    Labels:                   run=nginx
    Annotations:              <none>
    Selector:                 run=nginx
    Type:                     LoadBalancer
    IP:                       10.43.218.232
    LoadBalancer Ingress:     192.168.25.241
    Port:                     <unset>  80/TCP
    TargetPort:               80/TCP
    NodePort:                 <unset>  30262/TCP
    Endpoints:                10.42.0.10:80,10.42.0.8:80,10.42.0.9:80
    Session Affinity:         None
    External Traffic Policy:  Cluster
    Events:
      Type    Reason        Age   From                Message
      ----    ------        ----  ----                -------
      Normal  IPAllocated   3m8s  metallb-controller  Assigned IP "192.168.25.241"
      Normal  nodeAssigned  3m7s  metallb-speaker     announcing from node "yoctopi3"
    

    Endpointsはアクセス先podのIPアドレスが示される。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?