环境准备
-
服务器都已安装docker并配置system模式,具体操作可以参阅我的另外一篇文章docker部署
- 两台机器的内网 IP master:1.1.1.1 worker 2.2.2.2
- 所有服务器最低配置2核2G 所有服务器内网连通
- 文章的 所有ip都更换为自己的真实服务器的内网Ip地址
1.1.1.1
- 安装docker
- 环境准备
hostnamectl set-hostname k8s-master -- 命名服务器名称 主服务器
hostnamectl set-hostname k8s-worker -- 子节点服务器
- 设置主从
cat >> /etc/hosts << EOF
1.1.1.1 k8s-master ---------------------------注意改 IP 变成你的 k8s-master 的 IP
2.2.2.2 k8s-worker ---------------------------注意改 IP 变成你的 k8s-worker 的 IP
EOF
- 关闭虚拟内存
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
- 更换源 使用国内的源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
- 配置groups 统一使用systemd
- 修改docker的cgroups
编辑 `/etc/docker/daemon.json` (没有该文件就新建一个),添加如下内容 { "exec-opts": ["native.cgroupdriver=systemd"] } # systemctl restart docker - 重启docker- 修改k8s的cgroups
执行代码 修改文件 echo "KUBELET_EXTRA_ARGS=--cgroup-driver=systemd" > /etc/sysconfig/kubelet- 检查是否修改成功
docker info|grep "Cgroup Driver"
- 安装k8s
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
-
重启服务器
-
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
sysctl --system
- 创建systemctl服务
systemctl enable kubelet\
- 初始化集群
kubeadm init --apiserver-advertise-address=1.1.1.1 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.22.4 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
- --apiserver-advertise-address 这里是你的内网地址
- -- image-repository 镜像地址 更换为国内的阿里云地址
- -- kubernetes-version 指定版本 非必选,不加此配置就更新为最新版的
- 初始化成功
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 1.1.1.1:6443 --token v2djav.rfpat4j8g1uf7uoy \
--discovery-token-ca-cert-hash
sha256:65d640b3798fde3c1e9ef8f1abbf26c01b512d5d28947a6c7bc921e3dcb8f88b
- mkdir -p HOME/.kube/config sudo chown (id -g) $HOME/.kube/config 这三个命令需要执行
- export KUBECONFIG=/etc/kubernetes/admin.conf 添加环境变量
- kubeadm join 1.1.1.1:6443 --token v2djav.rfpat4j8g1uf7uoy \ --discovery-token-ca-cert-hash sha256:65d640b3798fde3c1e9ef8f1abbf26c01b512d5d28947a6c7bc921e3dcb8f88b 这个命令是子节点加入主节点需要执行的命令
-
到此 主节点已经部署完成 可以添加相应的配置插件 打开网址kubernetes.io/docs/concep… 选择对应的插件即可,我这里因为要添加子节点,所以需要添加网络插件 我这里选择的是Flannel 打开主页会显示
For Kubernetes v1.17+
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml所以只需要执行这个命令即可配置子节点
- 按照主节点的方式 初始化部署,但是不需要去初始化集群。千万不要初始化集群 然后执行 主节点集群初始化后给出的代码 请参考自己的生成的实际的代码
kubeadm join 1.1.1.1:6443 --token v2djav.rfpat4j8g1uf7uoy \ --discovery-token-ca-cert-hash sha256:65d640b3798fde3c1e9ef8f1abbf26c01b512d5d28947a6c7bc921e3dcb8f88b执行完之后 会显示如下
This node has joined the cluster: * Certificate signing request was sent to apiserver and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the control-plane to see this node join the cluster.- 接下来就可以在主机上查看运行情况 运行 kubectl get nodes 命令 查看所有的节点情况
NAME STATUS ROLES AGE VERSION k8s-master Ready control-plane,master 19m v1.22.4 k8s-worker Ready <none> 12m v1.22.4继续执行
kubectl get pod -n kube-system查看所有的pod,结果如下:NAME READY STATUS RESTARTS AGE coredns-7f6cbbb7b8-4hzvm 1/1 Running 0 20m coredns-7f6cbbb7b8-hh5rm 1/1 Running 0 20m etcd-k8s-master 1/1 Running 2 20m kube-apiserver-k8s-master 1/1 Running 2 20m kube-controller-manager-k8s-master 1/1 Running 2 20m kube-flannel-ds-bz29s 1/1 Running 0 13m kube-flannel-ds-ddk84 1/1 Running 0 16m kube-proxy-sxcrt 1/1 Running 0 20m kube-proxy-x5fvc 1/1 Running 0 13m kube-scheduler-k8s-master 1/1 Running 2 20m所有的pod都是1/1的状态即为配置成功,如有不是的 可以多等一会,启动pod需要时间
##验证k8s集群配置信息
- 往机器中放入一个 Nginx:
docker pull nginx --docker获取nginx镜像
kubectl create deployment nginx --image=nginx -- 这个是创建pod
kubectl expose deployment nginx --port=80 --type=NodePort -- 这个是部署pod按照docker的nginx镜像部署pod
- 接下来验证配置是否成功
- 运行
kubectl get pods,svc命令,查看nginx的这个pod的状态,显示如下
NAME READY STATUS RESTARTS AGE pod/nginx-6799fc88d8-56852 1/1 Running 0 7m24s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 30m service/nginx NodePort 10.1.254.34 <none> 80:30999/TCP 7m15s- 显示使用了30999这个端口,这个端口是随机生成的,如果需要指定,可以自定查阅相关命令
kubectl expose deployment
- 运行
curl http://master主机的内网Ip:30999
curl http://worker主机的内网ip:30999
curl命令执行访问没问题的话 会显示如下内容
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- 如果master和worker都开通了外网30999通过ipv6地址加端口访问的到