Kubernetes 搭建之 kubeadm 方式安装

Kubernetes About 5,148 words

修改镜像

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

安装组件

yum install -y kubelet kubeadm kubectl
systemctl enable kubelet

初始化

kubeadm init \
--apiserver-advertise-address=192.168.3.100 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.1 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=Swap \
--ignore-preflight-errors=NumCPU \
--ignore-preflight-errors=Mem

成功信息

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:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.3.100:6443 --token vbzq4m.tx56rncv3h8a4j1p \
    --discovery-token-ca-cert-hash sha256:6658c59aa91558c5b1486abfb8009181504f5197ac6599a7feca0923568fb1de

可能出现错误

查看日志

journalctl -xefu kubelet 

异常信息

[kubelet-check] It seems like the kubelet isn't running or healthy.
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.

查看kubelet服务运行状态

systemctl status kubelet

启动失败如果是因为cgroup问题,确认Docker配置中是否修改好了cgroupsystemd

修改/etc/docker/daemon.json文件。

{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

重启Docker

systemctl restart docker

成功后 master 节点执行

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

成功后 node 节点执行

token有效期为24小时。

kubeadm join 192.168.3.100:6443 --token vbzq4m.tx56rncv3h8a4j1p \
    --discovery-token-ca-cert-hash sha256:6658c59aa91558c5b1486abfb8009181504f5197ac6599a7feca0923568fb1de

查看节点状态

初始化后为NotReady状态。

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS     ROLES                  AGE     VERSION
k8s-master   NotReady   control-plane,master   9m10s   v1.23.1
k8s-node1    NotReady   <none>                 40s     v1.23.1
k8s-node2    NotReady   <none>                 18s     v1.23.1

配置网络插件

只需要在master执行即可。

直接URL执行。

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

如果raw.githubusercontent.com无法访问可以配置hosts

cat >> /etc/hosts << EOF
199.232.68.133 raw.githubusercontent.com
EOF

下载到本地执行。

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

执行apply

kubectl apply -f kube-flannel.yml

查看组件运行状态

kubectl get pods -n kube-system

输出:

[root@k8s-master ~]# kubectl get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-6d8c4cb4d-74zfn              1/1     Running   0          77m
coredns-6d8c4cb4d-mcfwd              1/1     Running   0          77m
etcd-k8s-master                      1/1     Running   0          77m
kube-apiserver-k8s-master            1/1     Running   0          77m
kube-controller-manager-k8s-master   1/1     Running   0          77m
kube-flannel-ds-6qcv9                1/1     Running   0          75m
kube-flannel-ds-fdcq8                1/1     Running   0          75m
kube-flannel-ds-m92wz                1/1     Running   0          75m
kube-proxy-8cfct                     1/1     Running   0          77m
kube-proxy-9zfzl                     1/1     Running   0          76m
kube-proxy-jzjq2                     1/1     Running   0          76m
kube-scheduler-k8s-master            1/1     Running   0          77m

查看各节点状态

kubectl get nodes

都为Ready状态:

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES                  AGE   VERSION
k8s-master   Ready    control-plane,master   84m   v1.23.1
k8s-node1    Ready    <none>                 83m   v1.23.1
k8s-node2    Ready    <none>                 83m   v1.23.1

测试安装

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort

查看状态

kubectl get pod,service

NginxPod上绑定的端口是30664

[root@k8s-master ~]# kubectl get pod,service
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-85b98978db-glq6g   1/1     Running   0          82m

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        86m
service/nginx        NodePort    10.110.44.60   <none>        80:30664/TCP   82m

物理机访问

使用两个node节点的IP地址都可以访问。

http://192.168.3.101:30664

http://192.168.3.102:30664

Views: 1,725 · Posted: 2022-03-05

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh