参考:https://www.cnblogs.com/fengyc/p/12392977.html
1. Ubuntu 安装 K8s
# Docker
curl -fsSL https://get.docker.com | sudo sh
# 使用 aliyun 的 k8s 源安装 kubeadm 和相关命令行工具
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl
systemctl enable kubelet
# 修改 docker 的 cgroup driver 选项为 systemd,与 k8s 保持一致,并修改 registry-mirror 加速下载
vi /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://registry.docker-cn.com", "https://docker.mirrors.ustc.edu.cn"]
}
systemctl restart docker
# 使用阿里云上提供的 k8s 镜像(这里指定的网络与后续使用的网络插件的配置保持一致)
kubeadm init --image-repository registry.aliyuncs.com/google_containers
--service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
# 按照提示把 k8s 配置复制到用户目录下
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 安装 pod 网络插件 flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 查看
kubectl get nodes
kubectl get cs
# 允许 master 部署 pod
kubectl taint nodes --all node-role.kubernetes.io/master-
# 测试(略)
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=8080 --type=NodePort
kubectl get pods,svc
# 重置环境(清理所有容器)
kubeadm reset
2. kubectl 命令补全
apt install bash-completion
echo "source <(kubectl completion bash)" >> ~/.bashrc
vi ~/.bashrc # 取消注释
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
3. 加入 node
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet kubeadm kubectl
master 节点重新创建 token
kubeadm token list # 查看 token
kubeadm token create --print-join-command # 创建 token 并打印 join 命令
加入集群
kubeadm join 192.168.11.204:6443 --token <token>
--discovery-token-ca-cert-hash sha256:<证书sha256>