您好,登錄后才能下訂單哦!
這篇文章主要介紹“kubeadm怎么搭建K8s”,在日常操作中,相信很多人在kubeadm怎么搭建K8s問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”kubeadm怎么搭建K8s”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
操作系統(tǒng):Centos7 (CentOS Linux release 7.9.2009)
Master 主節(jié)點: 1臺 虛擬機
Node 計算節(jié)點: 2臺 虛擬機
K8s version:v1.23.5 (選的較新的版本)
Docker version:20.10.14
虛擬機創(chuàng)建我就不詳細展開了
虛擬機網(wǎng)卡 選擇的是 1.網(wǎng)絡地址轉(zhuǎn)換NAT (主要用來訪問外網(wǎng)的) 2.僅主機網(wǎng)絡 (主要用來宿主機訪問虛擬機的)
把對應的 ip 設置如下
kube-master 192.168.56.20
kube-node-1 192.168.56.21
kube-node-2 192.168.56.22
cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" BOOTPROTO="none" #關閉 DEFROUTE="yes" IPV4_FAILURE_FATAL="no" IPV6INIT="yes" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_FAILURE_FATAL="no" IPV6_ADDR_GEN_MODE="stable-privacy" NAME="enp0s3" UUID="35eac2d4-234d-4859-848e-ed2a30a64a81" DEVICE="enp0s3" #添加部分 IPADDR="10.0.2.20" PREFIX="24" GATEWAY="10.0.2.2" DNS1="10.0.2.3" DNS2="114.114.114.114" ONBOOT="yes"
cat /etc/sysconfig/network-scripts/ifcfg-enp0s8
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #修改為 static 靜態(tài)ip DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=enp0s8 UUID=d06bac6d-294f-4bfe-bf08-5cb65c0a431f DEVICE=enp0s8 #新增部分 IPADDR=192.168.56.20 PREFIX=24 ONBOOT=yes
在 3臺虛擬機 /etc/hosts 中配置 如下
192.168.56.20 kube-master 192.168.56.21 kube-node-1 192.168.56.22 kube-node-2
yum -y install wget cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.repo.bak wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum clean all yum makecache #重新生成緩存
setenforce 0 #臨時關閉 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config #永久關閉
swapoff -a # 注釋掉關于swap分區(qū)的行 yes | cp /etc/fstab /etc/fstab_bak cat /etc/fstab_bak |grep -v swap > /etc/fstab
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
systemctl stop firewalld systemctl disable firewalld
echo 1 > /proc/sys/net/ipv4/ip_forward
注意 先換源 https://developer.aliyun.com/mirror/kubernetes?spm=a2c6h.13651102.0.0.64be1b11iddYWH 阿里換源文檔
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF yum install -y kubelet kubeadm kubectl
yum install -y kubelet kubeadm kubectl systemctl enable kubelet && systemctl start kubelet
由于官網(wǎng)未開放同步方式, 可能會有索引gpg檢查失敗的情況, 這時請用 yum install -y --nogpgcheck kubelet kubeadm kubectl
安裝
systemctl enable kubelet && systemctl start kubelet
此時執(zhí)行systemctl status kubelet
查看服務狀態(tài),此時肯定有錯誤(因為我們還未進行初始化操作),錯誤可使用journalctl -xe
查看錯誤信息。
Docker centos 安裝文檔 地址 https://docs.docker.com/engine/install/centos/
sudo yum install -y yum-utils sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo sudo yum install docker-ce docker-ce-cli containerd.io # 也可以安裝其他版本 yum list docker-ce --showduplicates | sort -r # sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io #啟動docker sudo systemctl enable docker && systemctl start docker
sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://9xxxx1clo.mirror.aliyuncs.com"] #請更換自己的 阿里云鏡像地址 } EOF
由于 cgroup-driver ,默認docker 是 cgroupfs ,而k8s 默認是 systemd,所以需要設置 docker 的 cgroup 為 systemd
推薦使用方式二進行修改
#查看cgroup-driver docker info | grep -i cgroup # 方式一 追加 --exec-opt native.cgroupdriver=systemd 參數(shù) sed -i "s#^ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd#g" /usr/lib/systemd/system/docker.service systemctl daemon-reload # 重新加載服務 systemctl enable docker # 啟用docker服務(開機自起) systemctl restart docker # 啟動docker服務 # 方式二 修改docker配置文件 vim /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"] }
可以通過
kubeadm config images list
查看所需的鏡像
kubeadm config images list k8s.gcr.io/kube-apiserver:v1.23.5 k8s.gcr.io/kube-controller-manager:v1.23.5 k8s.gcr.io/kube-scheduler:v1.23.5 k8s.gcr.io/kube-proxy:v1.23.5 k8s.gcr.io/pause:3.6 k8s.gcr.io/etcd:3.5.1-0 k8s.gcr.io/coredns/coredns:v1.8.6
如果拉取不到鏡像 可以通過 去 docker hub 上查找對應的 鏡像
docker pull k8simage/kube-apiserver:v1.23.5 docker tag k8simage/kube-apiserver:v1.23.5 k8s.gcr.io/kube-apiserver:v1.23.5 #重新tag改成上面所需的鏡像即可, 其他的都按照這樣來
Tips:
完成上面操作后 你可以通過 docker save 導出鏡像,以后再用的時候 可以通過 docker image load < 進行離線加載鏡像
#導出鏡像 ,批量導出所有鏡像 docker save $(docker images --format '{{.Repository}}:{{.Tag}}') -o k8s.images.tar #導入鏡像 ,注意 是 < 不是 > 。。 > 后會導致tar 無法使用需要重新上傳 docker image load < ...tar/zip
我這里選的是 flannel 網(wǎng)絡插件,先來看看 flannel 的配置文件
flannel.yml https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
不同的 網(wǎng)絡插件 網(wǎng)絡范圍不同
kubeadm init --pod-network-cidr=10.244.0.0/16 \ #網(wǎng)卡插件的 網(wǎng)絡范圍 --apiserver-advertise-address=192.168.56.20 #master 的 ip 地址
等一會就會有如下結(jié)果
可以把 kubeadm join 命令記下來 便于后面使用 保存輸出中的kubeadm join
部分內(nèi)容,用于添加node節(jié)點,或者使用kubeadm token list
和kubeadm token create --print-join-command
查看
執(zhí)行后續(xù)步驟,可以看到提示
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
前面提到了flannel 把對應的flannel.yml文件下載到服務器中,可以使用wget
可以看到 在沒配置 flannel 的時候 coredns 一直是 pending 狀態(tài)
wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml #可能會下載不到,文章最后我會提供內(nèi)容,直接copy過去即可 #Tips: 可能需要指定一下網(wǎng)卡 ,在kube-flannel.yml中 - /opt/bin/flanneld args: 后面添加 #- --iface=enp0s8 # 需要指定網(wǎng)卡,否則可能k8s會導致使用錯誤的網(wǎng)卡 kubectl apply -f kube-flannel.yml #應用該文件
kubectl get pods --all-namespaces -o wide #查看是否安裝成功,它會在 master 和 node 節(jié)點上都會自動部署
最后通過 kubectl get nodes -o wide 查看 節(jié)點
可以通過該命令進行查看 token
kubeadm token create --print-join-command #kubeadm join kube-master:6443 --token ii5xyw.mywlktar1e8f60cu --discovery-token-ca-cert-hash sha256:358f92418178342f79aa7b049e6c18a423f0fb88186189142459016fe6efb8e7
kubeadm join kube-master:6443 --token ii5xyw.mywlktar1e8f60cu --discovery-token-ca-cert-hash sha256:358f92418178342f79aa7b049e6c18a423f0fb88186189142459016fe6efb8e7
watch kubectl get nodes -o wide #可以通過watch 實時監(jiān)聽 加入情況
至此使用 kubeadm 已經(jīng)成功搭建k8s 集群
本篇主要講解了 如何使用 kubeadm 進行搭建 k8s 集群 ,搭建的是 k8s 較新版本 v1.23.5 版本,使用kubeadm 搭建相對來說還是比較簡單的 。
https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
https://developer.aliyun.com/mirror/kubernetes?spm=a2c6h.13651102.0.0.64be1b11iddYWH
https://hub.docker.com/r/k8simage/
https://docs.docker.com/engine/install/centos/
--- apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata: name: psp.flannel.unprivileged annotations: seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default spec: privileged: false volumes: - configMap - secret - emptyDir - hostPath allowedHostPaths: - pathPrefix: "/etc/cni/net.d" - pathPrefix: "/etc/kube-flannel" - pathPrefix: "/run/flannel" readOnlyRootFilesystem: false # Users and groups runAsUser: rule: RunAsAny supplementalGroups: rule: RunAsAny fsGroup: rule: RunAsAny # Privilege Escalation allowPrivilegeEscalation: false defaultAllowPrivilegeEscalation: false # Capabilities allowedCapabilities: ['NET_ADMIN', 'NET_RAW'] defaultAddCapabilities: [] requiredDropCapabilities: [] # Host namespaces hostPID: false hostIPC: false hostNetwork: true hostPorts: - min: 0 max: 65535 # SELinux seLinux: # SELinux is unused in CaaSP rule: 'RunAsAny' --- kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: flannel rules: - apiGroups: ['extensions'] resources: ['podsecuritypolicies'] verbs: ['use'] resourceNames: ['psp.flannel.unprivileged'] - apiGroups: - "" resources: - pods verbs: - get - apiGroups: - "" resources: - nodes verbs: - list - watch - apiGroups: - "" resources: - nodes/status verbs: - patch --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: flannel roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: flannel subjects: - kind: ServiceAccount name: flannel namespace: kube-system --- apiVersion: v1 kind: ServiceAccount metadata: name: flannel namespace: kube-system --- kind: ConfigMap apiVersion: v1 metadata: name: kube-flannel-cfg namespace: kube-system labels: tier: node app: flannel data: cni-conf.json: | { "name": "cbr0", "cniVersion": "0.3.1", "plugins": [ { "type": "flannel", "delegate": { "hairpinMode": true, "isDefaultGateway": true } }, { "type": "portmap", "capabilities": { "portMappings": true } } ] } net-conf.json: | { "Network": "10.244.0.0/16", "Backend": { "Type": "vxlan" } } --- apiVersion: apps/v1 kind: DaemonSet metadata: name: kube-flannel-ds namespace: kube-system labels: tier: node app: flannel spec: selector: matchLabels: app: flannel template: metadata: labels: tier: node app: flannel spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/os operator: In values: - linux hostNetwork: true priorityClassName: system-node-critical tolerations: - operator: Exists effect: NoSchedule serviceAccountName: flannel initContainers: - name: install-cni-plugin #image: flannelcni/flannel-cni-plugin:v1.0.1 for ppc64le and mips64le (dockerhub limitations may apply) image: rancher/mirrored-flannelcni-flannel-cni-plugin:v1.0.1 command: - cp args: - -f - /flannel - /opt/cni/bin/flannel volumeMounts: - name: cni-plugin mountPath: /opt/cni/bin - name: install-cni #image: flannelcni/flannel:v0.17.0 for ppc64le and mips64le (dockerhub limitations may apply) image: rancher/mirrored-flannelcni-flannel:v0.17.0 command: - cp args: - -f - /etc/kube-flannel/cni-conf.json - /etc/cni/net.d/10-flannel.conflist volumeMounts: - name: cni mountPath: /etc/cni/net.d - name: flannel-cfg mountPath: /etc/kube-flannel/ containers: - name: kube-flannel #image: flannelcni/flannel:v0.17.0 for ppc64le and mips64le (dockerhub limitations may apply) image: rancher/mirrored-flannelcni-flannel:v0.17.0 command: - /opt/bin/flanneld args: #- --iface=enp0s8 # 需要指定網(wǎng)卡,否則可能會導致問題 - --ip-masq - --kube-subnet-mgr resources: requests: cpu: "100m" memory: "50Mi" limits: cpu: "100m" memory: "50Mi" securityContext: privileged: false capabilities: add: ["NET_ADMIN", "NET_RAW"] env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - name: EVENT_QUEUE_DEPTH value: "5000" volumeMounts: - name: run mountPath: /run/flannel - name: flannel-cfg mountPath: /etc/kube-flannel/ - name: xtables-lock mountPath: /run/xtables.lock volumes: - name: run hostPath: path: /run/flannel - name: cni-plugin hostPath: path: /opt/cni/bin - name: cni hostPath: path: /etc/cni/net.d - name: flannel-cfg configMap: name: kube-flannel-cfg - name: xtables-lock hostPath: path: /run/xtables.lock type: FileOrCreate
到此,關于“kubeadm怎么搭建K8s”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。