准备 安装 Docker 上一篇文章 安装 docker
修改主机名 1 hostnamectl set -hostname k8s-master
修改 /etc/hosts 1 192.168.140.28 api.k8s.local k8s-master
关闭 swap
注释 /etc/fstab
文件中 swap
分区。
添加内核参数 修改 /etc/sysctl.conf
1 2 3 4 5 fs.file-max = 1000000 net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1
添加 repo 源 1 2 3 4 5 6 7 8 9 10 cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF
阿里云公网 1 sed -i 's|packages.cloud.google.com|mirrors.aliyun.com/kubernetes|' /etc/yum.repos.d/kubernetes.repo
阿里云内网 1 sed -i 's|https://packages.cloud.google.com|http://mirrors.cloud.aliyuncs.com/kubernetes|' /etc/yum.repos.d/kubernetes.repo
安装 1 2 3 yum install -y kubeadm kubelet kubectl systemctl enable kubelet
初始化 master 节点 1 2 3 4 5 6 kubeadm init \ --kubernetes-version=1.18.2 \ --apiserver-advertise-address=192.168.140.28 \ --apiserver-bind-port 6443 \ --pod-network-cidr=10.244.0.0/16 \ --image-repository registry.aliyuncs.com/google_containers
当出现 Your Kubernetes control-plane has initialized successfully!
即安装成功,并且在下面有相关配置。
1 2 3 mkdir -p $HOME /.kube sudo cp -i /etc/kubernetes/admin.conf $HOME /.kube/config sudo chown $(id -u):$(id -g) $HOME /.kube/config
还有最后一条命令在添加 k8s node 节点时使用到
1 kubeadm join 192.168.140.28:6443 --token xxx --discovery-token-ca-cert-hash sha256:xxx
如果忘了保存,可以使用以下命令重新获取到
1 kubeadm token create --print -join-command
这时可以使用以下命令查看节点
1 2 3 kubectl get node kubectl get pod -A
1 2 NAME STATUS ROLES AGE VERSION k8s-master NotReady master 8m56s v1.18.2
这边看到状态为 NotReady
是因为未安装网络组件。
添加 node 节点 1 kubeadm join 192.168.140.28:6443 --token xxx --discovery-token-ca-cert-hash sha256:xxx
安装 Calico 网络 1 2 3 4 wget --unlink -qO calico.yaml https://docs.projectcalico.org/v3.14/manifests/calico.yaml sed -i -e "s|192.168.0.0/16|10.244.0.0/16|g" calico.yaml kubectl apply -f calico.yaml
完成这步之后会看到节点的状态变为 Ready
。
安装 Dashboard 1 2 wget --unlink -qO dashboard.yaml https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml kubectl apply -f dashboard.yaml
一般这样是无法通过外网访问,建议在测试环境下修改 kubernetes-dashboard
的 ClusterIP
为 NodePort
将端口暴露出去。
1 kubectl edit service kubernetes-dashboard -n kubernetes-dashboard
修改之后,查看 services
1 kubernetes-dashboard kubernetes-dashboard NodePort 10.106.39.19 <none> 443:31570/TCP 36m
之后访问 https://${ip}:31570
发现需要 token
,下面创建一个管理员用户。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 apiVersion: v1 kind: ServiceAccount metadata: name: admin-user namespace: kubernetes-dashboard --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: admin-user roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: admin-user namespace: kubernetes-dashboard
1 kubectl apply -f admin-user.yaml
最后查看 token
,找到 admin-user
复制 token
即可以管理员身份访问。
1 k describe secrets -n kubernetes-dashboard
其他 master 参与工作(单机部署) 1 kubectl taint nodes --all node-role.kubernetes.io/master-
命令补全 1 kubectl completion bash > /root/.kube/completion.bash.inc
如果使用 k
作为 kubectl
的别名,需要修改上面生成的文件,在文件末尾修改为
1 2 3 4 5 6 7 if [[ $(type -t compopt) = "builtin" ]]; then complete -o default -F __start_kubectl kubectl complete -o default -F __start_kubectl k else complete -o default -o nospace -F __start_kubectl kubectl complete -o default -o nospace -F __start_kubectl k fi
最后将下面这段加入 .bash_profile
中,以使用自动补全功能。
1 2 3 4 5 6 source /usr/share/bash-completion/bash_completionsource /root/.kube/completion.bash.incalias k='kubectl'