知芯

k8s-with-kubeadm

2020-04-21

0. 虚拟机安装

  1. vmware 安装虚拟机

  2. 在宿主机ping虚拟机ip地址,如果不通进入第3步。

  3. 在Vmware Workstation中,点击编辑=》虚拟网络编辑器,进入虚拟网络编辑器,查看发现 NAT 模式的连接采用的网络适配器名称为VMnet8

    记录VMnet8的ip地址,并在宿主机ping,一般可以通。

  4. 在宿主机修改vmnet8的ip属性

    image-20200421094857699

    这样从宿主机就可ping通虚拟机了。

  5. 开启ssh连接

    1
    2
    $ yum install openssh-server
    $ vim /ect/ssh/sshd-config
    1
    2
    3
    4
    5
    6
    7
    8
    Port 22
    #AddressFamily any
    ListenAddress 0.0.0.0
    ListenAddress ::

    PermitRootLogin yes

    PasswordAuthentication yes
    1
    $ service sshd start
  6. 添加用户,并设置sudo权限

    1
    2
    3
    $ vim /etc/sudoers

    wbq813 ALL=(ALL) ALL

1. docker安装

  1. 卸载旧版本

    1
    2
    3
    4
    5
    6
    7
    8
    $ yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine
  1. 使用docker仓库安装

    1
    2
    3
    $ yum install -y yum-utils \
    device-mapper-persistent-data \
    lvm2
    1
    2
    3
    $ yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
    1
    $ yum install docker-ce-<VERSION> docker-ce-cli-<VERSION> containerd.io
    1
    $ systemctl start docker

2. K8s安装

2.1 Master部署

  1. 安装kubeadm

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
    [kubernetes]
    name=Kubernetes
    baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    enabled=1
    gpgcheck=0
    repo_gpgcheck=0
    gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
    http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    EOF

    $ setenforce 0
    $ yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
    $ systemctl enable kubelet && systemctl start kubelet
  2. 准备

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ systemctl enable docker.service
    $ swapoff -a
    # 开放端口6443 10250
    $ systemctl status firewalld
    $ firewall-cmd --query-port=6443/tcp
    $ firewall-cmd --add-port=6443/tcp --permanent
    $ firewall-cmd --add-port=10250/tcp --permanent
    $ firewall-cmd --reload

    #detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
  1. 创建kubeadm.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    apiVersion: kubeadm.k8s.io/v1beta2
    kind: ClusterConfiguration
    controllerManager:
    extraArgs:
    # 将来部署的 kube-controller-manager 能够使用自定义资源(Custom Metrics)进行自动水平扩展
    horizontal-pod-autoscaler-use-rest-clients: "true"
    horizontal-pod-autoscaler-sync-period: "10s"
    node-monitor-grace-period: "10s"
    apiServer:
    extraArgs:
    runtime-config: "api/all=true"
    # kubeadm 帮我们部署的 Kubernetes 版本号
    kubernetesVersion: "stable-1.18"
  2. 然后执行

    1
    $ kubeadm init --config kubeadm.yaml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    failed to pull image k8s.gcr.io/...

    k8s.gcr.io/kube-apiserver:v1.18.2
    k8s.gcr.io/kube-controller-manager:v1.18.2
    k8s.gcr.io/kube-scheduler:v1.18.2
    k8s.gcr.io/kube-proxy:v1.18.2
    k8s.gcr.io/pause:3.2
    k8s.gcr.io/etcd:3.4.3-0
    k8s.gcr.io/coredns
    // 利用registry.aliyuncs.com/google_containers镜像,并docker tag
    1
    2
    3
    /etc/kubernetes/manifests/kube-controller-manager.yaml already exists

    // --ignore-preflight-errors=all
    1
    2
    3
    4
    5
    6
    7
    8
    This error is likely caused by:
    - The kubelet is not running
    - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)

    // journalctl -xeu kubelet 查看日志
    E0420 node "bogon" not found
    // 仔细发现
    192.168.52.128:6443: connect: connection refused'

    6443是kube-api的端口,所以是kube-api有问题

    kubeadm的kube-api是镜像部署,

    1
    $ vim /etc/kubernetes/manifests/kube-apiserver.yaml

    image:k8s.gcr.io/kube-apiserver:v1.18.2 镜像是存在的

    1
    2
    $ kubeadm reset 
    # 之后重试,成功
  3. 记录

    1
    2
    $ kubeadm join 192.168.52.128:6443 --token xqlsfe.ull9nqor5y1kr0hm \
    --discovery-token-ca-cert-hash sha256:0718e9bd9ec0c97ab058b20c981daf7f07205ab671d597f90d85b46256d29067
使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章