K8S基本操作-07-Ubuntu安裝K8S
MicroK8S
Ubuntu 官方提供了一個 snap 套件可以超快的安裝 K8S , 稱為 MicroK8S。
sudo snap install microk8s --classic --channel=1.28
sudo usermod -a -G microk8s $USER
sudo chown -f -R $USER ~/.kube
su - $USER
安裝後檢查狀態是否安裝完成
microk8s status --wait-ready
使用 kubectl
microk8s kubectl get nodes
啟用額外功能 (Ingress 也要在此額外安裝),甚至有個 Container registry 存在。
microk8s enable dns
microk8s enable hostpath-storage
microk8s enable ingress
microk8s enable registry
microk8s enable helm
microk8s enable helm3
microk8s enable cert-manager
更多額外功能: https://microk8s.io/docs/addons#heading–list
開啟與關閉
microk8s start
microk8s stop
參見: https://microk8s.io/docs/getting-started
手動安裝官方 K8S
- 啟動橋接模式
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 立即生效
sudo sysctl --system
- 安裝必要套件
# 更新套件庫
sudo apt-get update
# 更新安裝 SSL 套件庫
sudo apt-get install -y apt-transport-https ca-certificates curl
# 讓APT keyring 有權限寫入
sudo mkdir -m 755 /etc/apt/keyrings
# 下載 keyring
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.27/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# 將 K8S 套件庫寫入 APT 套件
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.27/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# 更新套件
sudo apt-get update
# 檢查可安裝的 kubeadm 版本
sudo apt-cache madison kubeadm
# 下載對應版本的 K8S
sudo apt-get install -y kubelet=1.27.0-2.1 kubeadm=1.27.0-2.1 kubectl=1.27.0-2.1
# 防止自動更新
sudo apt-mark hold kubelet kubeadm kubectl
但這個時候節點其實沒有啟動,所以仍然還暫時無法放上 Pod 。
接著輸入以下指令
# 將 eth0 的 IP ,放入 Bash 變數 IP_ADDR
IP_ADDR=$(ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
# 然後用 kubeadm 啟動節點並且設置 無類別區隔路由(CIDR) 。
kubeadm init --apiserver-cert-extra-sans=controlplane --apiserver-advertise-address $IP_ADDR --pod-network-cidr=10.244.0.0/16
接著畫面會顯示下方輸出
controlplane ~ ➜ kubeadm init --apiserver-cert-extra-sans=controlplane --apiserver-advertise-address $IP_ADDR --pod-network-cidr=10.244.0.0/16
I1120 20:20:25.196365 19305 version.go:256] remote version is much newer: v1.28.4; falling back to: stable-1.27
[init] Using Kubernetes version: v1.27.8
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
W1120 20:20:25.702254 19305 images.go:80] could not find officially supported version of etcd for Kubernetes v1.27.8, falling back to the nearest etcd version (3.5.7-0)
W1120 20:20:33.820363 19305 checks.go:835] detected that the sandbox image "k8s.gcr.io/pause:3.6" of the container runtime is inconsistent with that used by kubeadm. It is recommended that using "registry.k8s.io/pause:3.9" as the CRI sandbox image.
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [controlplane kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.3.52.3]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [controlplane localhost] and IPs [192.3.52.3 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [controlplane localhost] and IPs [192.3.52.3 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
W1120 20:20:38.410133 19305 images.go:80] could not find officially supported version of etcd for Kubernetes v1.27.8, falling back to the nearest etcd version (3.5.7-0)
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 12.502254 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node controlplane as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node controlplane as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: v7s7co.mkpyly3lu7hf5t54
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
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
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.3.52.3:6443 --token v7s7co.mkpyly3lu7hf5t54 \
--discovery-token-ca-cert-hash sha256:ca60be8a123acded2e0220105b46e293cfd19f1ce8f7a324c5f824728466b76d
注意上方一大段輸出最後有個
kubeadm join 192.3.52.3:6443 --token v7s7co.mkpyly3lu7hf5t54 \
--discovery-token-ca-cert-hash sha256:ca60be8a123acded2e0220105b46e293cfd19f1ce8f7a324c5f824728466b76d
上面這段輸入要加入其他裝好 K8S 的節點就可以讓其他節點以 Work Node 加入了。
然後接著採用以下方式把配置檔案放到使用者資料夾,這樣就能使用 kubectl 了。
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
安裝網路套件 Flannel,需要有這個才能接通每個 Node。
# 下載 flannel 的佈署 YAML。
curl -LO https://raw.githubusercontent.com/flannel-io/flannel/v0.20.2/Documentation/kube-flannel.yml
將其中一段加以修改
containers:
- name: kube-flannel
#image: flannelcni/flannel:v0.20.2 for ppc64le and m>
image: docker.io/rancher/mirrored-flannelcni-flanne>
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
加入一行
containers:
- name: kube-flannel
#image: flannelcni/flannel:v0.20.2 for ppc64le and m>
image: docker.io/rancher/mirrored-flannelcni-flanne>
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=eth0
# 啟動 Flannel
kubectl apply -f kube-flannel.yml
安裝後要等一段時間,才可看到兩個 Node Ready。