---
# System prepended metadata

title: '[雲端] K8S / 安裝 script'
tags: [雲端, 雲端 / K8s, K8s]

---

[雲端] K8S / 安裝 script
===
###### tags: `雲端 / K8s`
###### tags: `雲端`, `K8s`

<br>

[TOC]

<br>

## 若已經有 k8s，想要重新安裝，可執行 kubectl reset
- [詳細資訊](/kD5ynMNsRH-TWq9KUwXfCQ#kubectl-reset)

<br>

## k8s.sh
> ```
> sudo sh k8s.sh
> ```
> - 目前可下載最新版號：1.22.0 (2021/08/16)
> - 在執行 `kubeadm init` 前，將 driver 從 cgroupfs 變更為 systemmd

```shell=
#!/bin/bash

# Add Kubernetes Repository and Install Kubernetes
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt-get update
sudo swapoff -a

# List Kubernetes Versions
curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'

# Install Kubernetes with version 1.17.3
# sudo apt-get install -qy kubelet=1.17.3-00 kubectl=1.17.3-00 kubeadm=1.17.3-00
# ^^^ v1.21.1... OK
sudo apt-get install -qy kubelet kubectl kubeadm

# 2021.05.13, 
# when executing 'kubeadm init ...', dump the following log:
# [preflight] You can also perform this action in beforehand 
# using 'kubeadm config images pull'
sudo kubeadm config images pull

# Startup a Kubernetes Cluster (for master node)
# sudo kubeadm init --service-cidr 10.96.0.0/12 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.78.153.130
# ^^^ NOT OK

sudo kubeadm init --pod-network-cidr=10.244.0.0/16 


# Setup Admin Config
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# Install Flannel CNI
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

# Taint Master Node
kubectl taint nodes --all node-role.kubernetes.io/master-

# Install Kubernetes Dashboard
#kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc2/aio/deploy/recommended.yaml

# Get admin user token
#kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep dashboard-token | awk '{print $1}') > token
#echo "dashboard token="
#cat token
```
log:
```
To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf
```

- keywords: k8s/kubenetes, install, script

<br>

## Q&A
- [[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd".](https://blog.csdn.net/M82_A1/article/details/97626309)
    /lib/systemd/ 
    ```bash=
    $ sudo nano /etc/docker/daemon.json
    {
     "exec-opts":["native.cgroupdriver=systemd"]
    }
    ```
    ```bash=
    systemctl restart docker
    systemctl status docker
    ```
    ```
    $ sudo systemctl status docker 
    ● docker.service - Docker Application Container Engine
         Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
    ```
- 快速安裝 kubernete
    ```bash=
    sudo rm -rf $HOME/.kube/
    sudo rm -rf /etc/kubernetes/
    ```
    ```bash=
    sudo kubeadm init --pod-network-cidr=10.244.0.0/16
    ```
    ```bash=
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    ```
- ### [How do I find the join command for kubeadm on the master?](https://stackoverflow.com/questions/51126164/)
    ```
    kubeadm token create --print-join-command
    ```
    or
    ```
    kubeadm token list
    ```
    ```
    $ kubeadm join 10.0.2.15:6443 --token heynu5.1g65zecl0z4zxcb1 \
        --discovery-token-ca-cert-hash sha256:bed37343beb691328455f8a1eee50ee26e82595346ee779fe647bd62e4172f6f 
    # ------------------
    $ kubeadm token list
    TOKEN                     TTL         EXPIRES                     USAGES                   DESCRIPTION                                                EXTRA GROUPS
    heynu5.1g65zecl0z4zxcb1   23h         2021-05-15T08:54:24+08:00   authentication,signing   The default bootstrap token generated by 'kubeadm init'.   system:bootstrappers:kubeadm:default-node-token
    ```
    (2021.05.13, 待驗證)
    
- ### [Coredns in pending state in Kubernetes cluster](https://stackoverflow.com/questions/52609257)
    >　According to this error, you forgot to initialize a Kubernetes Pod network add-on. Looking at your settings, I suppose it should be Flannel.
    >　
    >　For flannel to work correctly, you must pass `--pod-network-cidr=10.244.0.0/16‵` to kubeadm init.

    ```bash=
    $ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/c5d10c8/Documentation/kube-flannel.yml
    ```

- ### [link text](https:// "title")
    > ### Control plane node isolation
    > By default, **your cluster will not schedule pods on the control-plane node for security reasons**. If you want to be able to schedule pods on the control-plane node, e.g. for a single-machine Kubernetes cluster for development, run:
    > ```
    > $ kubectl taint nodes --all node-role.kubernetes.io/master-
    > ```
    

- ### [my kubernetes cluster is down after reboot](https://stackoverflow.com/questions/55555987)
    - 原因
        Main PID: 2184 (code=exited, status=255/EXCEPTION)
    - 狀態
        [![](https://i.imgur.com/xGqM1JV.jpg)](https://i.imgur.com/xGqM1JV.jpg)
        
        [![](https://i.imgur.com/dwIsZjk.png)](https://i.imgur.com/dwIsZjk.png)
    - 解決方法：==`sudo swapoff -a`==
        ```
        $ curl 127.0.0.1:30080
        curl: (7) Failed to connect to 127.0.0.1 port 30080: Connection refused

        $ systemctl status kubelet
        ● kubelet.service - kubelet: The Kubernetes Node Agent
             Loaded: loaded (/lib/systemd/system/kubelet.service; enabled; vendor preset: enabled)
            Drop-In: /etc/systemd/system/kubelet.service.d
                     └─10-kubeadm.conf
             Active: activating (auto-restart) (Result: exit-code) since Fri 2021-05-14 09:15:53 CST; 4s ago
               Docs: https://kubernetes.io/docs/home/
            Process: 2184 ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS (code=exited, st>
           Main PID: 2184 (code=exited, status=255/EXCEPTION)

        $ kubectl get nodes
        The connection to the server 10.0.2.15:6443 was refused - did you specify the right host or port?
        
        $ kubectl get pod -A
        The connection to the server 10.0.2.15:6443 was refused - did you specify the right host or port?
        ```
        執行：
        ```bash
        $ sudo swapoff -a
        ```
        等待片刻
        ```
        $ kubectl get nodes
        NAME            STATUS   ROLES    AGE   VERSION
        tj-virtualbox   Ready    master   22m   v1.17.3
        
        $ kubectl get pod
        NAME           READY   STATUS   RESTARTS   AGE
        tj-pod-kuard   0/1     Error    3          6m8s
        
        ...
        ...
        ...
        
        $ kubectl get pod
        NAME           READY   STATUS    RESTARTS   AGE
        tj-pod-kuard   1/1     Running   4          7m21s
        
        $ curl 127.0.0.1:30080
        # OK
        ```
        
- ### [[[雲端] K8S / Helm] 安裝 Helm3](https://hackmd.io/5h33aCEKS5GJFoThP8hF6w#%E5%AE%89%E8%A3%9D-Helm3)
    ```
    curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 \
    && chmod 700 get_helm.sh \
    && ./get_helm.sh
    ```
    
<br>

# Good work
```bash=
sudo swapoff -a
docker ps -a
docker images

# Add Kubernetes Repository and Install Kubernetes
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt-get update
sudo swapoff -a

# List Kubernetes Versions
curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
sudo apt-get install -qy kubelet kubectl kubeadm
sudo kubeadm config images pull
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

kubectl taint nodes --all node-role.kubernetes.io/master-

sudo kubeadm join 10.0.2.15:6443 --token b272bh.pxlqf7r3xjswu4fc --discovery-token-ca-cert-hash sha256:7ae9817a12330ffadea5b0751579f0dad8ef2c95e12ce8aa5b5345cfbc437a48  --ignore-preflight-errors=all

alias k=kubectl
k get nodes
k get pod -A

```
![](https://i.imgur.com/i9s7z88.png)
