使用kubeadm安装

参考 Kubernetes 官方文档:

前期准备

关闭防火墙

  1. systemctl disable firewalld && systemctl stop firewalld

docker和bridge-utils

要求节点上安装有 docker version 1.2+ 和 bridge-utils (用来操作linux bridge).

查看 docker 版本:

  1. $ docker --version
  2. Docker version 17.06.0-ce, build 02c1d87

bridge-utils可以通过apt安装:

  1. sudo apt-get install bridge-utils

注意事项

  1. 如果在使用 kubeadm init 命令是 提示以下内容:

    1. [preflight] Some fatal errors occurred:
    2. /proc/sys/net/bridge/bridge-nf-call-iptables contents are not set to 1

    执行:

    1. echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
    2. echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
  2. 禁用虚拟内存swap

    如果报错如下:

    1. [preflight] Some fatal errors occurred:
    2. running with swap on is not supported. Please disable swap

    这说明我们开启了linux的swap虚拟内存,这里要求关闭。

    参考文章: Ubuntu 16.04 禁用启用虚拟内存swap

    实测不重启电脑的方案无法生效,只好用需要重启的方案了。

    1. mount -n -o remount,rw /
    2. vi /etc/fstab
    3. # 在swap分区这行前加 # 禁用掉,保存退出
    4. reboot
    5. # 看看是否生效
    6. free -m

    实测发现,虽然当时生效了,但是过一段时间,虚拟内存又出现了。解决方式:通过磁盘工具将swap分区删除。

安装kubeadm

切记: 想办法搞定全局翻墙,不然kubeadm安装是比较麻烦的。

按照官方文档的指示,执行如下命令:

  1. apt-get update && apt-get install -y apt-transport-https
  2. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
  3. cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
  4. deb http://apt.kubernetes.io/ kubernetes-xenial main
  5. EOF
  6. apt-get update
  7. apt-get install -y kubelet kubeadm kubectl

安装k8s

同样: 想办法搞定全局翻墙。

  1. sudo kubeadm init

输出如下:

  1. [init] Using Kubernetes version: v1.9.2
  2. [init] Using Authorization modes: [Node RBAC]
  3. ......
  4. [addons] Applied essential addon: kube-dns
  5. [addons] Applied essential addon: kube-proxy
  6. Your Kubernetes master has initialized successfully!
  7. To start using your cluster, you need to run the following as a regular user:
  8. mkdir -p $HOME/.kube
  9. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  10. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  11. You should now deploy a pod network to the cluster.
  12. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  13. https://kubernetes.io/docs/concepts/cluster-administration/addons/
  14. You can now join any number of machines by running the following on each node
  15. as root:
  16. kubeadm join --token d05a21.dfad690e77acc878 192.168.1.244:6443 --discovery-token-ca-cert-hash sha256:263c07847848652711ecbe62b128d4c7e4a24418995a49c78f4ec3753cf111d4

为了使用普通用户,按照上面的提示执行:

  1. mkdir -p $HOME/.kube
  2. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. sudo chown $(id -u):$(id -g) $HOME/.kube/config

此时如果遇到执行命令时报如下错误:

  1. $ kubectl version
  2. Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T10:09:24Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
  3. The connection to the server localhost:8080 was refused - did you specify the right host or port?

需要修改/etc/kubernetes/manifests/kube-apiserver.yaml文件,修改下列参数:

  1. - --insecure-port=8080

默认是0不打开,修改为8080即可。