美文网首页
kubeadm快速搭建k8s集群

kubeadm快速搭建k8s集群

作者: 我是个杀手啊 | 来源:发表于2023-08-20 10:52 被阅读0次

    1. 环境准备

    节点信息 master node
    IP 172.28.14.103 172.28.14.104
    内存 4G 2G
    CPU 2核 2核
    系统 CentOS Linux release 7.8.2003 (Core) CentOS Linux release 7.8.2003 (Core)
    • master节点执行
    [root@k8smaster ~]# hostnamectl set-hostname k8smaster
    [root@k8smaster ~]# systemctl stop firewalld
    [root@k8smaster ~]# setenforce 0 
    setenforce: SELinux is disabled
    [root@k8smaster ~]# cat >> /etc/hosts << EOF
    172.28.14.103 k8smaster
    172.28.14.104 k8snode
    EOF
    [root@k8smaster ~]# reboot
    
    • node节点执行
    [root@k8snode ~]# hostnamectl set-hostname k8snode
    [root@k8snode ~]# systemctl stop firewalld
    [root@k8snode ~]# setenforce 0 
    setenforce: SELinux is disabled
    [root@k8snode ~]# cat >> /etc/hosts << EOF
    172.28.14.103 k8smaster
    172.28.14.104 k8snode
    EOF
    [root@k8snode ~]# reboot
    

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

    2.1. 安装docker

    mkdir /etc/yum.repos.d/back
    mv /etc/yum.repos.d/* /etc/yum.repos.d/back/
    wget -O /etc/yum.repos.d/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
    wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
    yum -y install docker-ce-18.06.1.ce-3.el7
     # 启动docker
    systemctl enable docker && systemctl start docker
    
    # 配置镜像加速器
    mkdir -p /etc/docker
    tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://6ysopw9t.mirror.aliyuncs.com"]
    }
    EOF
    systemctl daemon-reload
    systemctl restart docker
    
    

    2.2 添加阿里云YUM软件源

    # 添加阿里云yum源
    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
    
    

    2.3 安装kubeadm,kubelet和kubectl

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

    3. 部署Master

    master节点执行

    [root@k8smaster ~]# kubeadm init \
       --apiserver-advertise-address=172.28.14.103 \
       --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
    W0818 17:24:16.348436   30168 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
    [init] Using Kubernetes version: v1.18.0
    [preflight] Running pre-flight checks
            [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
    [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'
    [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
    [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 [k8smaster kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.28.14.103]
    [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 [k8smaster localhost] and IPs [172.28.14.103 127.0.0.1 ::1]
    [certs] Generating "etcd/peer" certificate and key
    [certs] etcd/peer serving cert is signed for DNS names [k8smaster localhost] and IPs [172.28.14.103 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
    [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"
    W0818 17:24:21.735976   30168 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
    [control-plane] Creating static Pod manifest for "kube-scheduler"
    W0818 17:24:21.737007   30168 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
    [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
    [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 20.502769 seconds
    [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
    [kubelet] Creating a ConfigMap "kubelet-config-1.18" 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 k8smaster as control-plane by adding the label "node-role.kubernetes.io/master=''"
    [mark-control-plane] Marking the node k8smaster as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
    [bootstrap-token] Using token: 7ykjbd.vdoyl2lgcw7gppx2
    [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
    
    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 172.28.14.103:6443 --token 7ykjbd.vdoyl2lgcw7gppx2 \
        --discovery-token-ca-cert-hash sha256:dc1430249616caf7c59fe02028854be9006e6640d5bc203ccf182fb8286bb5f8 
    
    

    4. 添加Node

    node节点执行

    [root@k8snode ~]# kubeadm join 172.28.14.103:6443 --token 7ykjbd.vdoyl2lgcw7gppx2     --discovery-token-ca-cert-hash sha256:dc1430249616caf7c59fe02028854be9006e6640d5bc203ccf182fb8286bb5f8 
    W0818 17:29:02.171704   13434 join.go:346] [preflight] WARNING: JoinControlPane.controlPlane settings will be ignored when control-plane flag is not set.
    [preflight] Running pre-flight checks
            [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
    [preflight] Reading configuration from the cluster...
    [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
    [kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.18" ConfigMap in the kube-system namespace
    [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
    [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
    [kubelet-start] Starting the kubelet
    [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
    
    This node has joined the cluster:
    * Certificate signing request was sent to apiserver and a response was received.
    * The Kubelet was informed of the new secure connection details.
    
    Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
    
    

    5. 部署网络插件

    master节点执行

    [root@k8smaster ~]# mkdir flannel
    [root@k8smaster ~]# cd flannel/
    [root@k8smaster flannel]# curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  4398  100  4398    0     0   5737      0 --:--:-- --:--:-- --:--:--  5734
    [root@k8smaster flannel]# ls
    kube-flannel.yml
    [root@k8smaster flannel]# docker pull quay.io/coreos/flannel:v0.14.0-rc1
    v0.14.0-rc1: Pulling from coreos/flannel
    801bfaa63ef2: Pull complete 
    e4264a7179f6: Pull complete 
    bc75ea45ad2e: Pull complete 
    7362b3b13577: Pull complete 
    fddb5353186c: Pull complete 
    071b96dd834b: Pull complete 
    37b23f1e6a4f: Pull complete 
    Digest: sha256:0394177305e08d560f3cb5f5dcd1ee12575a9fd5ab801471922ff0b8f5863e92
    Status: Downloaded newer image for quay.io/coreos/flannel:v0.14.0-rc1
    [root@k8smaster flannel]# export KUBECONFIG=/etc/kubernetes/admin.conf
    [root@k8smaster flannel]# source /etc/profile
    [root@k8smaster flannel]# kubectl apply -f kube-flannel.yml
    namespace/kube-flannel created
    clusterrole.rbac.authorization.k8s.io/flannel created
    clusterrolebinding.rbac.authorization.k8s.io/flannel created
    serviceaccount/flannel created
    configmap/kube-flannel-cfg created
    daemonset.apps/kube-flannel-ds created
    
    # 等一会Pending才会变成Running
    [root@k8smaster flannel]# kubectl get pods -n kube-system
    NAME                                READY   STATUS    RESTARTS   AGE
    coredns-7ff77c879f-g2qsm            1/1     Running   0          28m
    coredns-7ff77c879f-sv7cb            1/1     Running   0          28m
    etcd-k8smaster                      1/1     Running   0          29m
    kube-apiserver-k8smaster            1/1     Running   0          29m
    kube-controller-manager-k8smaster   1/1     Running   0          29m
    kube-proxy-jm62r                    1/1     Running   0          24m
    kube-proxy-s2dv7                    1/1     Running   0          28m
    kube-scheduler-k8smaster            1/1     Running   0          29m
    
    

    6. 测试

    6.1 创建服务

    [root@k8smaster ~]# kubectl create deployment nginx --image=nginx
    deployment.apps/nginx created
    [root@k8smaster ~]# 
    [root@k8smaster ~]# kubectl get pods
    NAME                    READY   STATUS    RESTARTS   AGE
    nginx-f89759699-t8267   1/1     Running   0          44s
    [root@k8smaster ~]# 
    [root@k8smaster ~]# 
    [root@k8smaster ~]# kubectl get pod,svc
    NAME                        READY   STATUS    RESTARTS   AGE
    pod/nginx-f89759699-t8267   1/1     Running   0          67s
    
    NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   2d17h
    [root@k8smaster ~]# 
    [root@k8smaster ~]# 
    [root@k8smaster ~]# kubectl expose deployment nginx --port=80 --type=NodePort
    service/nginx exposed
    [root@k8smaster ~]# 
    [root@k8smaster ~]# 
    [root@k8smaster ~]# kubectl get pod,svc
    NAME                        READY   STATUS    RESTARTS   AGE
    pod/nginx-f89759699-t8267   1/1     Running   0          94s
    
    NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        2d17h
    service/nginx        NodePort    10.109.4.150   <none>        80:32238/TCP   3s
    [root@k8smaster ~]# 
    

    6.2 测试访问

    测试访问结果

    相关文章

      网友评论

          本文标题:kubeadm快速搭建k8s集群

          本文链接:https://www.haomeiwen.com/subject/bfrrmdtx.html