Ubuntu18.04安装Kubernetes 1.24
k8s 版本:v1.24
机器清单:
系统 |
规格 |
角色 |
ubuntu-18.04.6 |
4核4G磁盘80G |
Master |
ubuntu-18.04.6 |
4核4G磁盘80G |
Node |
没有这么高配置 2核2G也行。
系统镜像地址:
两个版本都行。
ubuntu-18.04.6-desktop 版本
https://mirrors.aliyun.com/ubuntu-releases/bionic/ubuntu-18.04.6-desktop-amd64.iso
ubuntu-18.04.6-live 版本
https://mirrors.aliyun.com/ubuntu-releases/bionic/ubuntu-18.04.6-live-server-amd64.iso
请配置好静态ip。
安装支持组件 sudo apt-get install network-manager
ubuntu-18.04.6 配置方法如下:
https://www.bbsmax.com/A/kmzLNpKl5G/
以下步骤在root权限下执行。
#设置密码
sudo passwd root
su - root
第一步 Master 和 Node 都需执行
timedatectl set-timezone Asia/Shanghai apt-get install ntpdate ntpdate ntp.aliyun.com # 系统参数修改, Ubuntu默认没有安装selinux的,直接执行命令即可。 setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config 1.1 这里需要看一下swap有没有关闭成功。 swapoff -a sed -i 's/.*swap.*/#&/' /etc/fstab free -m # swap 为 0 cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.ipv4.ip_forward = 1 EOF apt-get install iptables ufw disable sysctl --system
第二步 Master 和 Node 都需执行
apt-get update apt-get -y install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" apt-get -y update apt-get -y install docker-ce mkdir /etc/docker # Setup daemon. cat > /etc/docker/daemon.json <<EOF { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "registry-mirrors": ["https://e6vlzg9v.mirror.aliyuncs.com"] } EOF mkdir -p /etc/systemd/system/docker.service.d sed -i '/^disabled_plugins/d' /etc/containerd/config.toml cat <<EOF> /etc/crictl.yaml runtime-endpoint: unix:///run/containerd/containerd.sock image-endpoint: unix:///run/containerd/containerd.sock timeout: 10 debug: false EOF
# 安装crictl: https://blog.csdn.net/xixihahalelehehe/article/details/116591151
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.26.1/crictl-v1.26.1-linux-amd64.tar.gz
tar zxvf crictl-v1.26.1-linux-amd64.tar.gz -C /usr/local/bin
systemctl daemon-reload && service docker start && systemctl enable docker systemctl restart containerd chmod 777 /var/run/docker.sock # 验证 containerd 功能是否正常,会显示 IMAGE 等字样,如果没有,请执行 systemctl restart containerd 再次重启一下。 docker images
第三步 Master 和 Node 都需执行
apt-get update && apt-get install -y apt-transport-https sudo curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF
apt-get update apt-get install -y kubelet=1.24.0-00 kubeadm=1.24.0-00 kubectl=1.24.0-00 systemctl enable kubelet && systemctl start kubelet && systemctl status kubelet # journalctl -x kubelet 查看报错信息,kubeadm 工具比较喜欢更改 pause 的地址,看到pause 拉取错误了,tag命令替换成对应的地址。 ctr -n k8s.io i pull registry.aliyuncs.com/google_containers/pause:3.6 ctr -n k8s.io i tag registry.aliyuncs.com/google_containers/pause:3.6 registry.k8s.io/pause:3.6 ctr -n k8s.io i tag registry.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.6
第四步 Master执行
# 默认使用本机hostname,不设置,会与node重名,最终只能显示一个node.
# kubeadm初始化k8s集群
# 每台都拉取镜像包
crictl pull docker.io/calico/cni:v3.25.0
crictl pull docker.io/calico/kube-controllers:v3.25.0
crictl pull docker.io/calico/node:v3.25.0
crictl pull docker.io/library/nginx:latest
crictl pull k8s.gcr.io/pause:3.6
crictl pull registry.aliyuncs.com/google_containers/pause:3.6
crictl pull registry.k8s.io/pause:3.6
crictl pull registry.aliyuncs.com/google_containers/coredns:v1.8.6
crictl pull registry.aliyuncs.com/google_containers/etcd:3.5.3-0
crictl pull registry.aliyuncs.com/google_containers/kube-apiserver:v1.24.0
crictl pull registry.aliyuncs.com/google_containers/kube-controller-manager:v1.24.0
crictl pull registry.aliyuncs.com/google_containers/kube-proxy:v1.24.0
crictl pull registry.aliyuncs.com/google_containers/kube-scheduler:v1.24.0
crictl pull registry.aliyuncs.com/google_containers/pause:3.7
ctr -n k8s.io i tag registry.aliyuncs.com/google_containers/pause:3.6 k8s.gcr.io/pause:3.6
sudo kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.24.0 --ignore-preflight-errors=all -v5 sudo mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
第五步 Node执行
sudo hostnamectl set-hostname node # 再master上执行 sudo kubeadm token create --print-join-command # 粘贴执行输出的命令。 sudo kubeadm join xxx --token xxxxx --discovery-token-ca-cert-hash sha256:xxxx -v 5
第六步 Master执行
# 安装k8s网络插件 sudo curl https://docs.tigera.io/archive/v3.25/manifests/calico.yaml -O && kubectl apply -f calico.yaml
第七步Master执行
kubectl get node显示Ready时再执行下面语句。 kubectl taint node master node-role.kubernetes.io/master:NoSchedule- kubectl taint node master node-role.kubernetes.io/control-plane:NoSchedule- #创建一个tomcat应用并访问 kubectl create deployment tomcat --image=tomcat kubectl expose deployment tomcat --port=8080 --target-port=8080 --type=NodePort #确认 tomcat 部署成功。 kubectl get pods #确认端口 kubectl get service #浏览器访问应用。 http://xxxxx:xxx