搭建一台开发环境的kubernetes
Step 1 Set hostname and add entries in the hosts file
安装Ubuntu时应该已经有机器名了,比如我的机器名是u1(1.1.1.90)
要ping一下自己的机器名,如果返回127.0.0.1,请到 /etc/hosts修改成实际的IP地址
得到本机IP地址输入命令ip a
Step 2 Disable swap & add kernel settings
代码语言:bash复制sudo swapoff -a
sudo sed -i 's//swap.img/#/swap.img/' /etc/fsta
可以用free -m检查swap是否为0
代码语言:bash复制sudo tee /etc/modules-load.d/containerd.conf<<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
代码语言:bash复制sudo tee /etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
代码语言:bash复制sudo sysctl --system
Step 3 Install containerd run time
代码语言:bash复制sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
注意,必须从docker的源里安装最新版的containerd,ubuntu默认源的版本过低
代码语言:bash复制curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y containerd.io
代码语言:bash复制containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
注意:如果你在国内,需要用nano打开config.toml,把其中的k8s.gcr.io替换成registry.aliyuncs.com/google_containers
代码语言:bash复制sudo sed -i 's/registry.k8s.io/pause:3.6/registry.aliyuncs.com/google_containers/pause:3.9/' /etc/containerd/config.toml
代码语言:bash复制sudo systemctl restart containerd
sudo systemctl enable containerd
Step 4 Add apt repository for Kubernetes
如果你在国外
代码语言:bash复制curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
如果你在国内
代码语言:bash复制curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"
Step 5 Install Kubernetes components Kubectl, kubeadm & kubelet
代码语言:bash复制sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Step 6 Initialize Kubernetes cluster with Kubeadm command
注意把结尾的1.1.1.90替换成你的机器名或者IP,不能是127.0.0.1
代码语言:bash复制kubeadm version
sudo kubeadm init --kubernetes-version=v1.28.2 --image-repository registry.aliyuncs.com/google_containers --v=5 --control-plane-endpoint=1.1.1.90
代码语言:bash复制mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl cluster-info
kubectl get nodes
此时状态是NotReady
代码语言:bash复制curl -L https://projectcalico.docs.tigera.io/manifests/calico.yaml -O
kubectl apply -f calico.yaml
代码语言:bash复制kubectl get pods -n kube-system
至此安装尚未完成,有几个pod处于pending的情况,需要执行如下命令。
代码语言:bash复制kubectl taint nodes --all node-role.kubernetes.io/control-plane-
执行好,再运行kubectl get pods -n kube-system,有几个pod会处于ContainerCreating的状态
容器下载好会全部Running
Step 7 Test Kubernetes Installation
代码语言:bash复制kubectl create deployment nginx-app --image=nginx --replicas=2
kubectl get deployment nginx-app
kubectl expose deployment nginx-app --type=NodePort --port=80
kubectl get svc nginx-app
kubectl describe svc nginx-app
curl http://127.0.0.1:31246
注意:从kubectl describe svc nginx-app返回的信息里找到NodePort替换掉31246