近日经过kubeadm 安装 kubernetes v1.16.0,踩过很多坑,现记录下安装过程。node
安装环境:linux
三台虚拟机信息以下:docker
IP | 节点角色 | Hostname |
172.21.23.146 | master | master |
172.21.23.147 | worker | node1 |
172.21.23.148 | worker | node2 |
在全部节点上进行以下操做:centos
一、设置主机名api
hostnamectl set-hostname master echo "127.0.0.1 $(hostname)" >> /etc/hosts
设置其余两台worker节点主机名称时,可将 master 替换为正确的主机名node一、node2便可。安全
二、卸载docker旧版本服务器
yum remove -y docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine
三、设置 yum repository网络
yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
四、安装并启动 dockerapp
yum install -y docker-ce-18.09.7 docker-ce-cli-18.09.7 containerd.io systemctl enable docker systemctl start docker
五、安装 nfs-utilscurl
yum install -y nfs-utils
六、关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
七、关闭 SeLinux
setenforce 0 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
八、关闭 swap
swapoff -a yes | cp /etc/fstab /etc/fstab_bak cat /etc/fstab_bak |grep -v swap > /etc/fstab
九、配置内核参数
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
# 执行命令以生效 sysctl -p
十、配置国内Kubernetes源
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
十一、安装kubelet、kubeadm、kubectl
yum install -y kubelet-1.16.0 kubeadm-1.16.0 kubectl-1.16.0
十二、修改docker Cgroup Driver为systemd
sed -i "s#^ExecStart=/usr/bin/dockerd.*#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd#g" /usr/lib/systemd/system/docker.service
1三、设置 docker 镜像,提升 docker 镜像下载速度和稳定性, 若是您访问 https://hub.docker.io 速度很是稳定,亦能够跳过这个步骤
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
1四、重启 docker
systemctl daemon-reload
systemctl restart docker
1五、启动 kubelet
systemctl enable kubelet && systemctl start kubelet
在国内没有ke xue上网的条件下,须要经过国内镜像仓库去拉取镜像。我是先拉取镜像,修改镜像名称,最后安装kubernetes 的。
拉取镜像:
docker pull mirrorgooglecontainers/kube-apiserver-amd64:v1.16.0 docker pull mirrorgooglecontainers/kube-controller-manager-amd64:v1.16.0 docker pull mirrorgooglecontainers/etcd-amd64:3.3.15-0 docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.16.0 docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.16.0 docker pull mirrorgooglecontainers/pause:3.1 docker pull coredns/coredns:1.6.2
修改镜像名称:
docker tag mirrorgooglecontainers/kube-apiserver-amd64:v1.16.0 k8s.gcr.io/kube-apiserver:v1.16.0 docker tag mirrorgooglecontainers/kube-controller-manager-amd64:v1.16.0 k8s.gcr.io/kube-controller-manager:v1.16.0 docker tag mirrorgooglecontainers/kube-scheduler-amd64:v1.16.0 k8s.gcr.io/kube-scheduler:v1.16.0 docker tag mirrorgooglecontainers/kube-proxy-amd64:v1.16.0 k8s.gcr.io/kube-proxy:v1.16.0 docker tag mirrorgooglecontainers/etcd-amd64:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0 docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1 docker tag coredns/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
删除重复镜像:
docker rmi mirrorgooglecontainers/kube-apiserver-amd64:v1.16.0 docker rmi mirrorgooglecontainers/kube-controller-manager-amd64:v1.16.0 docker rmi mirrorgooglecontainers/etcd-amd64:3.3.15-0 docker rmi mirrorgooglecontainers/kube-scheduler-amd64:v1.16.0 docker rmi mirrorgooglecontainers/kube-proxy-amd64:v1.16.0 docker rmi mirrorgooglecontainers/pause:3.1 docker rmi coredns/coredns:1.6.2
一、编写了一个给 kubeadm 用的 YAML 文件(名叫:kubeadm.yaml):
apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration controllerManager: ExtraArgs: 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" kubernetesVersion: "v1.16.0"
二、执行部署指令:
kubeadm init --config kubeadm.yaml
就能够完成 Kubernetes Master 的部署了,这个过程只须要几分钟。部署完成后,kubeadm 会生成一行指令:
kubeadm join 172.21.23.146:6443 --token 9uko4n.jf7d60ijcl35kz7p \ --discovery-token-ca-cert-hash sha256:c9a97e52ad4704598cbe494d879f940cfc38364ee0acda6e6f98f44d1717230c
这个 kubeadm join 命令,就是用来给这个 Master 节点添加更多工做节点(Worker)的命令。咱们在后面部署 Worker 节点的时候立刻会用到它,因此找一个地方把这条命令记录下来。
此外,kubeadm 还会提示咱们第一次使用 Kubernetes 集群所须要的配置命令:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
而须要这些配置命令的缘由是:Kubernetes 集群默认须要加密方式访问。因此,这几条命令,就是将刚刚部署生成的 Kubernetes 集群的安全配置文件,保存到当前用户的.kube 目录下,
kubectl 默认会使用这个目录下的受权信息访问 Kubernetes 集群。
三、部署网络插件
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
等待大概2-3分钟左右,查看Pod状态
[root@master ~]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-5644d7b6d9-fxx4g 1/1 Running 0 32m coredns-5644d7b6d9-k76m7 1/1 Running 0 32m etcd-master 1/1 Running 0 31m kube-apiserver-master 1/1 Running 0 31m kube-controller-manager-master 1/1 Running 0 30m kube-proxy-v8d7k 1/1 Running 0 32m kube-scheduler-master 1/1 Running 0 30m weave-net-xb6xs 1/2 Running 0 7m37s
一、按照制做离线镜像的步骤制做k8s的镜像,这一步很重要,没有K8s的镜像,执行下面的kubeadm join命令后,节点一直是以下状态
二、执行部署 Master 节点时生成的 kubeadm join 指令:
kubeadm join 172.21.23.146:6443 --token 9uko4n.jf7d60ijcl35kz7p \ --discovery-token-ca-cert-hash sha256:c9a97e52ad4704598cbe494d879f940cfc38364ee0acda6e6f98f44d1717230c
三、若是忘记 kubeadm join命令,能够在master上执行下面一条指令获取
kubeadm token create --print-join-command