• 使用kubeadm搭建一个k8s集群


    kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。

    这个工具能通过两条指令完成一个kubernetes集群的部署:

    # 创建一个 Master 节点
    $ kubeadm init
    
    # 将一个 Node 节点加入到当前集群中
    $ kubeadm join <Master节点的IP和端口 >
    

    1. 安装要求

    在开始之前,部署Kubernetes集群机器需要满足以下几个条件:

    • 一台或多台机器,操作系统 CentOS7.x-86_x64
    • 硬件配置:2GB或更多内存,2个CPU或更多CPU,硬盘30GB或更多
    • 可以联网,需要拉取镜像,如果服务器不能上网,需要提前下载镜像并导入节点

    2. 准备环境

    角色 IP
    master 192.168.152.140
    node1 192.168.152.141
    node2 192.168.152.142

    3台机器执行以下操作:

    # 关闭防火墙
    systemctl stop firewalld
    systemctl disable firewalld
    
    # 关闭selinux
    sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
    setenforce 0  # 临时
    
    # 关闭swap
    swapoff -a  # 临时
    sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久
    
    # 根据规划设置主机名
    hostnamectl set-hostname <hostname>
    
    # 在master添加hosts
    cat >> /etc/hosts << EOF
    192.168.152.140 master
    192.168.152.141 node1
    192.168.152.142 node2
    EOF
    
    # 将桥接的IPv4流量传递到iptables的链
    cat > /etc/sysctl.d/k8s.conf << EOF
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
    EOF
    sysctl --system  # 生效
    
    # 时间同步
    yum install ntpdate -y
    ntpdate time.windows.com
    

    3. 所有节点安装Docker/kubeadm/kubelet

    Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。3台机器执行以下操作:

    3.1 安装Docker

    yum-config-manager 
        --add-repo 
        https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        
    yum install -y docker-ce-18.09.3 docker-ce-cli-18.09.3 containerd.io
    
    systemctl start docker
    
    systemctl enable docker
    
    $ cat > /etc/docker/daemon.json << EOF
    {
      "registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
    }
    EOF
    
    #使配置文件生效
    $ systemctl daemon-reload
    
    #重启docker
    $ systemctl restart docker
    

    3.2 添加阿里云YUM软件源

    3台机器执行以下操作:

    $ cat > /etc/yum.repos.d/kubernetes.repo << EOF
    [kubernetes]
    name=Kubernetes
    baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    enabled=1
    gpgcheck=0
    repo_gpgcheck=0
    gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF
    

    3.3 安装kubeadm,kubelet和kubectl

    3台机器执行以下操作:

    由于版本更新频繁,这里指定版本号部署:

    $ yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
    $ systemctl enable kubelet
    

    4. 部署Kubernetes Master

    在192.168.152.140(Master)执行:

    $ kubeadm init 
      --apiserver-advertise-address=192.168.152.140 
      --image-repository registry.aliyuncs.com/google_containers 
      --kubernetes-version v1.18.0 
      --service-cidr=10.96.0.0/12 
      --pod-network-cidr=10.244.0.0/16
    

    由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址。

    使用kubectl工具:

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

    5. 加入Kubernetes Node

    向集群添加新节点node1和node2,在node1,node2中执行在kubeadm init输出结果中的下面几行:

    $ kubeadm join 192.168.152.140:6443 --token esce21.q6hetwm8si29qxwn 
        --discovery-token-ca-cert-hash sha256:00603a05805807501d7181c3d60b478788408cfe6cedefedb1f97569708be9c5
    

    默认token有效期为24小时,当过期之后,该token就不可用了。这时就需要重新创建token,操作如下:

    $ kubeadm token create --print-join-command   #将输出结果复制,在要加入的结点中粘贴执行
    

    6. 部署CNI网络插件

    以下操作只在master执行:

    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    
    kubectl get pods -n kube-system
    NAME                          READY   STATUS    RESTARTS   AGE
    kube-flannel-ds-amd64-2pc95   1/1     Running   0          72s
    

    如果以上kubectl apply命令连不上网,则可以用以下操作代替:

    touch kube-flannel.yml   #创建文件
    vi 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 unsed 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: |
        {
          "cniVersion": "0.2.0",
          "name": "cbr0",
          "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: lizhenliang/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: lizhenliang/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
    
    
    kubectl apply -f kube-flannel.yml
    
    kubectl get pods -n kube-system
    NAME                          READY   STATUS    RESTARTS   AGE
    kube-flannel-ds-amd64-2pc95   1/1     Running   0          72s
    

    7. 测试kubernetes集群

    在master中执行:

    在Kubernetes集群中创建一个pod,验证是否正常运行:

    $ kubectl create deployment nginx --image=nginx
    $ kubectl expose deployment nginx --port=80 --type=NodePort
    $ kubectl get pod,svc
    NAME                        READY   STATUS    RESTARTS   AGE
    pod/nginx-f89759699-lgg76   1/1     Running   0          2m34s
    
    NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
    service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        11m
    service/nginx        NodePort    10.106.148.244   <none>        80:31990/TCP   2m30s
    

    使用浏览器访问:http://任意一个node结点访问的IP地址:31990

    (31990对应上面输出结果中的那个端口号)

  • 相关阅读:
    BPM平台在企业业务系统中使用的价值讨论
    零售餐饮行业的信息化建设
    LINQ 与Oracle应用 :转帖
    k2之于.NET流程应用开发者
    利用xslt导出复杂样式的excel,支持多个worksheet
    利用偏移量快速定位数据内容
    简单天气项目中观察者模式解析
    作业3:基于墨刀的:视频剪辑软件原型设计
    必做作业2:视频剪辑软件调研
    .Net Core项目依赖项问题
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/14223438.html
Copyright © 2020-2023  润新知