溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

CentOS中怎么搭建K8S

發(fā)布時(shí)間:2021-06-12 17:22:46 來(lái)源:億速云 閱讀:314 作者:Leah 欄目:系統(tǒng)運(yùn)維

這篇文章將為大家詳細(xì)講解有關(guān)CentOS中怎么搭建K8S,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

環(huán)境:

  •  操作系統(tǒng):win10

  •  虛擬機(jī):virtual box

  •  linux發(fā)行版:CentOS7

  •  linux內(nèi)核(使用uname -r查看):3.10.0-957.el7.x86_64

  •  master和node節(jié)點(diǎn)通信的ip(master):192.168.99.104

二. 安裝docker-ce 18.09.9(所有機(jī)器)

所有安裝k8s的機(jī)器都需要安裝docker,命令如下:

# 安裝docker所需的工具  yum install -y yum-utils device-mapper-persistent-data lvm2  # 配置阿里云的docker源  yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  # 指定安裝這個(gè)版本的docker-ce  yum install -y docker-ce-18.09.9-3.el7 # 啟動(dòng)docker  systemctl enable docker && systemctl start docker

三. 設(shè)置k8s環(huán)境準(zhǔn)備條件(所有機(jī)器)

安裝k8s的機(jī)器需要2個(gè)CPU和2g內(nèi)存以上,這個(gè)簡(jiǎn)單,在虛擬機(jī)里面配置一下就可以了。然后執(zhí)行以下腳本做一些準(zhǔn)備操作。所有安裝k8s的機(jī)器都需要這一步操作。

# 關(guān)閉防火墻  systemctl disable firewalld  systemctl stop firewalld  # 關(guān)閉selinux  # 臨時(shí)禁用selinux  setenforce 0  # 永久關(guān)閉 修改/etc/sysconfig/selinux文件設(shè)置  sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux  sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config  # 禁用交換分區(qū)  swapoff -a  # 永久禁用,打開(kāi)/etc/fstab注釋掉swap那一行。  sed -i 's/.*swap.*/#&/' /etc/fstab  # 修改內(nèi)核參數(shù)  cat <<EOF >  /etc/sysctl.d/k8s.conf  net.bridge.bridge-nf-call-ip6tables = 1  net.bridge.bridge-nf-call-iptables = 1  EOF  sysctl --system

四. 安裝k8s v1.16.0 master管理節(jié)點(diǎn)

如果還沒(méi)安裝docker,請(qǐng)參照本文步驟二安裝docker-ce 18.09.9(所有機(jī)器)安裝。如果沒(méi)設(shè)置k8s環(huán)境準(zhǔn)備條件,請(qǐng)參照本文步驟三設(shè)置k8s環(huán)境準(zhǔn)備條件(所有機(jī)器)執(zhí)行。

以上兩個(gè)步驟檢查完畢之后,繼續(xù)以下步驟。

      1.   安裝kubeadm、kubelet、kubectl

由于官方k8s源在google,國(guó)內(nèi)無(wú)法訪(fǎng)問(wèn),這里使用阿里云yum源

# 執(zhí)行配置k8s阿里云源  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  # 安裝kubeadm、kubectl、kubelet  yum install -y kubectl-1.16.0-0 kubeadm-1.16.0-0 kubelet-1.16.0-0  # 啟動(dòng)kubelet服務(wù)  systemctl enable kubelet && systemctl start kubelet

      2.  初始化k8s 以下這個(gè)命令開(kāi)始安裝k8s需要用到的docker鏡像,因?yàn)闊o(wú)法訪(fǎng)問(wèn)到國(guó)外網(wǎng)站,所以這條命令使用的是國(guó)內(nèi)的阿里云的源(registry.aliyuncs.com/google_containers)。另一個(gè)非常重要的是:這里的--apiserver-advertise-address使用的是master和node間能互相ping通的ip,我這里是192.168.99.104,剛開(kāi)始在這里被坑了一個(gè)晚上,你請(qǐng)自己修改下ip執(zhí)行。這條命令執(zhí)行時(shí)會(huì)卡在[preflight] You can also perform this action in beforehand using ''kubeadm config images pull,大概需要2分鐘,請(qǐng)耐心等待。

# 下載管理節(jié)點(diǎn)中用到的6個(gè)docker鏡像,你可以使用docker images查看到  # 這里需要大概兩分鐘等待,會(huì)卡在[preflight] You can also perform this action in beforehand using ''kubeadm config images pull  kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.16.0 --apiserver-advertise-address 192.168.99.104 --pod-network-cidr=10.244.0.0/16 --token-ttl 0

上面安裝完后,會(huì)提示你輸入如下命令,復(fù)制粘貼過(guò)來(lái),執(zhí)行即可。

# 上面安裝完成后,k8s會(huì)提示你輸入如下命令,執(zhí)行  mkdir -p $HOME/.kube  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config  sudo chown $(id -u):$(id -g) $HOME/.kube/config

      3.  記住node加入集群的命令 上面kubeadm init執(zhí)行成功后會(huì)返回給你node節(jié)點(diǎn)加入集群的命令,等會(huì)要在node節(jié)點(diǎn)上執(zhí)行,需要保存下來(lái),如果忘記了,可以使用如下命令獲取。

kubeadm token create --print-join-command

以上,安裝master節(jié)點(diǎn)完畢??梢允褂胟ubectl get nodes查看一下,此時(shí)master處于NotReady狀態(tài),暫時(shí)不用管。

CentOS中怎么搭建K8S

五. 安裝k8s v1.16.0 node工作節(jié)點(diǎn)

如果還沒(méi)安裝docker,請(qǐng)參照本文步驟二安裝docker-ce 18.09.9(所有機(jī)器)安裝。如果沒(méi)設(shè)置k8s環(huán)境準(zhǔn)備條件,請(qǐng)參照本文步驟三設(shè)置k8s環(huán)境準(zhǔn)備條件(所有機(jī)器)執(zhí)行。

以上兩個(gè)步驟檢查完畢之后,繼續(xù)以下步驟。

      1.  安裝kubeadm、kubelet

# 執(zhí)行配置k8s阿里云源  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  # 安裝kubeadm、kubectl、kubelet  yum install -y  kubeadm-1.16.0-0 kubelet-1.16.0-0  # 啟動(dòng)kubelet服務(wù)  systemctl enable kubelet && systemctl start kubelet

      2.  加入集群 這里加入集群的命令每個(gè)人都不一樣,可以登錄master節(jié)點(diǎn),使用kubeadm token create --print-join-command 來(lái)獲取。獲取后執(zhí)行如下。

# 加入集群,如果這里不知道加入集群的命令,可以登錄master節(jié)點(diǎn),使用kubeadm token create --print-join-command 來(lái)獲取  kubeadm join 192.168.99.104:6443 --token ncfrid.7ap0xiseuf97gikl \      --discovery-token-ca-cert-hash sha256:47783e9851a1a517647f1986225f104e81dbfd8fb256ae55ef6d68ce9334c6a2

加入成功后,可以在master節(jié)點(diǎn)上使用kubectl get nodes命令查看到加入的節(jié)點(diǎn)。

六. 安裝flannel(master機(jī)器)

以上步驟安裝完后,機(jī)器搭建起來(lái)了,但狀態(tài)還是NotReady狀態(tài),如下圖,master機(jī)器需要安裝flanneld。     

CentOS中怎么搭建K8S

      1.  下載官方fannel配置文件 使用wget命令,地址為:(https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml),這個(gè)地址國(guó)內(nèi)訪(fǎng)問(wèn)不了,所以我把內(nèi)容復(fù)制下來(lái),為了避免前面文章過(guò)長(zhǎng),我把它粘貼到文章末尾第八個(gè)步驟附錄了。這個(gè)yml配置文件中配置了一個(gè)國(guó)內(nèi)無(wú)法訪(fǎng)問(wèn)的地址(quay.io),我已經(jīng)將其改為國(guó)內(nèi)可以訪(fǎng)問(wèn)的地址(quay-mirror.qiniu.com)。我們新建一個(gè)kube-flannel.yml文件,復(fù)制粘貼該內(nèi)容即可。

      2.  安裝fannel

kubectl apply -f kube-flannel.yml

七. 大功告成

至此,k8s集群搭建完成,如下圖節(jié)點(diǎn)已為Ready狀態(tài),大功告成,完結(jié)撒花。

CentOS中怎么搭建K8S

八. 附錄

這是kube-flannel.yml文件的內(nèi)容,已經(jīng)將無(wú)法訪(fǎng)問(wèn)的地址(quay.io)全部改為國(guó)內(nèi)可以訪(fǎng)問(wèn)的地址(quay-mirror.qiniu.com)。我們新建一個(gè)kube-flannel.yml文件,復(fù)制粘貼該內(nèi)容即可。

---  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']    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/v1beta1  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/v1beta1  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-amd64    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: beta.kubernetes.io/os                      operator: In                      values:                        - linux                    - key: beta.kubernetes.io/arch                      operator: In                      values:                        - amd64        hostNetwork: true        tolerations:        - operator: Exists          effect: NoSchedule        serviceAccountName: flannel        initContainers:        - name: install-cni          image: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64          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: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-amd64          command:          - /opt/bin/flanneld          args:          - --ip-masq          - --kube-subnet-mgr          resources:            requests:              cpu: "100m"              memory: "50Mi"            limits:              cpu: "100m"              memory: "50Mi"          securityContext:            privileged: false            capabilities:              add: ["NET_ADMIN"]          env:          - name: POD_NAME            valueFrom:              fieldRef:                fieldPath: metadata.name          - name: POD_NAMESPACE            valueFrom:              fieldRef:                fieldPath: metadata.namespace          volumeMounts:          - name: run            mountPath: /run/flannel          - name: flannel-cfg            mountPath: /etc/kube-flannel/        volumes:          - name: run            hostPath:              path: /run/flannel          - name: cni            hostPath:              path: /etc/cni/net.d          - name: flannel-cfg            configMap:              name: kube-flannel-cfg  ---  apiVersion: apps/v1  kind: DaemonSet  metadata:    name: kube-flannel-ds-arm64    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: beta.kubernetes.io/os                      operator: In                      values:                        - linux                    - key: beta.kubernetes.io/arch                      operator: In                      values:                        - arm64        hostNetwork: true        tolerations:        - operator: Exists          effect: NoSchedule        serviceAccountName: flannel        initContainers:        - name: install-cni          image: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-arm64          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: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-arm64          command:          - /opt/bin/flanneld          args:          - --ip-masq          - --kube-subnet-mgr          resources:            requests:              cpu: "100m"              memory: "50Mi"            limits:              cpu: "100m"              memory: "50Mi"          securityContext:            privileged: false            capabilities:               add: ["NET_ADMIN"]          env:          - name: POD_NAME            valueFrom:              fieldRef:                fieldPath: metadata.name          - name: POD_NAMESPACE            valueFrom:              fieldRef:                fieldPath: metadata.namespace          volumeMounts:          - name: run            mountPath: /run/flannel          - name: flannel-cfg            mountPath: /etc/kube-flannel/        volumes:          - name: run            hostPath:              path: /run/flannel          - name: cni            hostPath:              path: /etc/cni/net.d          - name: flannel-cfg            configMap:              name: kube-flannel-cfg  ---  apiVersion: apps/v1  kind: DaemonSet  metadata:    name: kube-flannel-ds-arm    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: beta.kubernetes.io/os                      operator: In                      values:                        - linux                    - key: beta.kubernetes.io/arch                      operator: In                      values:                        - arm        hostNetwork: true        tolerations:        - operator: Exists          effect: NoSchedule        serviceAccountName: flannel        initContainers:        - name: install-cni          image: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-arm          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: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-arm          command:          - /opt/bin/flanneld          args:          - --ip-masq          - --kube-subnet-mgr          resources:            requests:              cpu: "100m"              memory: "50Mi"            limits:              cpu: "100m"              memory: "50Mi"          securityContext:            privileged: false            capabilities:               add: ["NET_ADMIN"]          env:          - name: POD_NAME            valueFrom:              fieldRef:                fieldPath: metadata.name          - name: POD_NAMESPACE            valueFrom:              fieldRef:                fieldPath: metadata.namespace          volumeMounts:          - name: run            mountPath: /run/flannel          - name: flannel-cfg            mountPath: /etc/kube-flannel/        volumes:          - name: run            hostPath:              path: /run/flannel          - name: cni            hostPath:              path: /etc/cni/net.d          - name: flannel-cfg            configMap:              name: kube-flannel-cfg  ---  apiVersion: apps/v1  kind: DaemonSet  metadata:    name: kube-flannel-ds-ppc64le    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: beta.kubernetes.io/os                      operator: In                      values:                        - linux                    - key: beta.kubernetes.io/arch                      operator: In                      values:                        - ppc64le        hostNetwork: true        tolerations:        - operator: Exists          effect: NoSchedule        serviceAccountName: flannel        initContainers:        - name: install-cni          image: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-ppc64le          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: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-ppc64le          command:          - /opt/bin/flanneld          args:          - --ip-masq          - --kube-subnet-mgr          resources:            requests:              cpu: "100m"              memory: "50Mi"            limits:              cpu: "100m"              memory: "50Mi"          securityContext:            privileged: false            capabilities:               add: ["NET_ADMIN"]          env:          - name: POD_NAME            valueFrom:              fieldRef:                fieldPath: metadata.name          - name: POD_NAMESPACE            valueFrom:              fieldRef:                fieldPath: metadata.namespace          volumeMounts:          - name: run            mountPath: /run/flannel          - name: flannel-cfg            mountPath: /etc/kube-flannel/        volumes:          - name: run            hostPath:              path: /run/flannel          - name: cni            hostPath:              path: /etc/cni/net.d          - name: flannel-cfg            configMap:              name: kube-flannel-cfg  ---  apiVersion: apps/v1  kind: DaemonSet  metadata:    name: kube-flannel-ds-s390x    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: beta.kubernetes.io/os                      operator: In                      values:                        - linux                    - key: beta.kubernetes.io/arch                      operator: In                      values:                        - s390x        hostNetwork: true        tolerations:        - operator: Exists          effect: NoSchedule        serviceAccountName: flannel        initContainers:        - name: install-cni          image: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-s390x          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: quay-mirror.qiniu.com/coreos/flannel:v0.11.0-s390x          command:          - /opt/bin/flanneld          args:          - --ip-masq          - --kube-subnet-mgr          resources:            requests:              cpu: "100m"              memory: "50Mi"            limits:              cpu: "100m"              memory: "50Mi"          securityContext:            privileged: false            capabilities:               add: ["NET_ADMIN"]          env:          - name: POD_NAME            valueFrom:              fieldRef:                fieldPath: metadata.name          - name: POD_NAMESPACE            valueFrom:              fieldRef:                fieldPath: metadata.namespace          volumeMounts:          - name: run            mountPath: /run/flannel          - name: flannel-cfg            mountPath: /etc/kube-flannel/        volumes:          - name: run            hostPath:              path: /run/flannel          - name: cni            hostPath:              path: /etc/cni/net.d          - name: flannel-cfg            configMap:              name: kube-flannel-cfg

關(guān)于CentOS中怎么搭建K8S就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI