重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Centos7中怎么搭建k8s环境

Centos7中怎么搭建k8s环境,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

焉耆网站建设公司成都创新互联公司,焉耆网站设计制作,有大型网站制作公司丰富经验。已为焉耆近千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的焉耆做网站的公司定做!

1. 安装kubeadm、kubelet、kubectl

由于官方k8s源在google,国内无法访问,这里使用阿里云yum源

# 执行配置k8s阿里云源  cat < /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  # 启动kubelet服务  systemctl enable kubelet && systemctl start kubelet

2. 初始化k8s

以下这个命令开始安装k8s需要用到的docker镜像,因为无法访问到国外网站,所以这条命令使用的是国内的阿里云的源(registry.aliyuncs.com/google_containers)。

另一个非常重要的是:这里的--apiserver-advertise-address使用的是master和node间能互相ping通的ip,我这里是192.168.99.104,刚开始在这里被坑了一个晚上,你请自己修改下ip执行。

这条命令执行时会卡在[preflight] You can also perform this action in beforehand using ''kubeadm config images pull,大概需要2分钟,请耐心等待。

# 下载管理节点中用到的6个docker镜像,你可以使用docker images查看到  # 这里需要大概两分钟等待,会卡在[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

上面安装完后,会提示你输入如下命令,复制粘贴过来,执行即可。

# 上面安装完成后,k8s会提示你输入如下命令,执行  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执行成功后会返回给你node节点加入集群的命令,等会要在node节点上执行,需要保存下来,如果忘记了,可以使用如下命令获取。

kubeadm token create --print-join-command

以上,安装master节点完毕。可以使用kubectl get nodes查看一下,此时master处于NotReady状态,暂时不用管。

五. 安装k8s v1.16.0 node工作节点

如果还没安装docker,请参照本文步骤二安装docker-ce 18.09.9(所有机器)安装。

如果没设置k8s环境准备条件,请参照本文步骤三设置k8s环境准备条件(所有机器)执行。

以上两个步骤检查完毕之后,继续以下步骤。

1. 安装kubeadm、kubelet

# 执行配置k8s阿里云源  cat < /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  # 启动kubelet服务  systemctl enable kubelet && systemctl start kubelet

2. 加入集群

这里加入集群的命令每个人都不一样,可以登录master节点,使用kubeadm token create --print-join-command 来获取。获取后执行如下。

# 加入集群,如果这里不知道加入集群的命令,可以登录master节点,使用kubeadm token create --print-join-command 来获取   kubeadm join 192.168.99.104:6443 --token ncfrid.7ap0xiseuf97gikl \      --discovery-token-ca-cert-hash sha256:47783e9851a1a517647f1986225f104e81dbfd8fb256ae55ef6d68ce9334c6a2

加入成功后,可以在master节点上使用kubectl get nodes命令查看到加入的节点。

六. 安装flannel(master机器)

以上步骤安装完后,机器搭建起来了,但状态还是NotReady状态,如下图,master机器需要安装flanneld。

1. 下载官方fannel配置文件

使用wget命令,地址为:(https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml),这个地址国内访问不了,所以我把内容复制下来,为了避免前面文章过长,我把它粘贴到文章末尾第八个步骤附录了。

这个yml配置文件中配置了一个国内无法访问的地址(quay.io),我已经将其改为国内可以访问的地址(quay-mirror.qiniu.com)。我们新建一个kube-flannel.yml文件,复制粘贴该内容即可。

搜索Java知音公众号,回复“后端面试”,送你一份Java面试题宝典.pdf

2. 安装fannel

kubectl apply -f kube-flannel.yml

七. 大功告成

至此,k8s集群搭建完成,如下图节点已为Ready状态,大功告成,完结撒花。

八. 附录

这是kube-flannel.yml文件的内容,已经将无法访问的地址(quay.io)全部改为国内可以访问的地址(quay-mirror.qiniu.com)。我们新建一个kube-flannel.yml文件,复制粘贴该内容即可。

---  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

关于Centos7中怎么搭建k8s环境问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章题目:Centos7中怎么搭建k8s环境
文章起源:http://cqcxhl.cn/article/gjecjc.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP