Kubernetes再探(kubeadm init)

5 篇文章 0 订阅

周末加班搞,一种热情支撑着我

目前我只有一个master虚拟机,没关系,先把这个搞好,以后有了再往里面join。
使用minikube时启动需要指定container runtime,就不用使用1.23的版本了。
使用kubeadm 也没有用1.23,就用默认最新版(现在时间 2022年9月18日)1.25.
先看下启动日志

[root@node01 ~]# kubeadm init --config=init.yaml
[init] Using Kubernetes version: v1.25.0
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master] and IPs [10.0.0.1 192.168.56.102]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master] and IPs [192.168.56.102 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master] and IPs [192.168.56.102 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 8.502369 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: abcdef.0123456789abcdef
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

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.56.102:6443 --token abcdef.0123456789abcdef \
        --discovery-token-ca-cert-hash sha256:74764d67d4bf92db70a3c80f41305196687b791fb58b1918b146ae0fbc0cc1c1

1、将containerd用作 container runtime,由于安装docker时已经安装了containerd,这里就不再次安装了,主要说一下要配置的地方。

# 生成配置文件,如果已经存在,就删除
containerd config default > /etc/containerd/config.toml

# 将 sandbox_image 镜像源设置为阿里云google_containers镜像源
[plugins."io.containerd.grpc.v1.cri"]
	sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.6"

# 配置镜像加速地址
  [plugins."io.containerd.tracing.processor.v1.otlp"]
    endpoint = "https://a8x1qfbv.mirror.aliyuncs.com"

# 配置 cgroup 驱动程序systemd
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
  SystemdCgroup = true

# 重启containerd 
systemctl restart containerd && systemctl enable containerd
# 查询
[root@node01 ~]# netstat -anput | grep containerd
tcp        0      0 127.0.0.1:39210         0.0.0.0:*               LISTEN      65104/containerd
tcp        0      0 10.0.2.15:40724         116.62.81.173:443       ESTABLISHED 65104/containerd

2、配置crictl默认的containerd unix端点

# 编辑
nano /etc/crictl.yaml

runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: true

3、配置Kubernets集群

#打印用于kubeadm init的默认初始化配置为文件
kubeadm config print init-defaults > init.yaml
#修改配置文件
advertiseAddress: 192.168.56.102 #修改为第一台主机的IP地址
name: master #主机的名称
imageRepository: registry.aliyuncs.com/google_containers
kubernetesVersion: 1.25.0 #安装的版本
serviceSubnet: 10.0.0.0/24 #分配的地址


# 启动master
kubeadm init --config=init.yaml
# 截取成功后的日志
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/

4、安装Kunbernets网络插件

#下载 Kubernetes API 数据存储的 Calico 网络
curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O
# nano calico.yaml
            - name: CALICO_IPV4POOL_CIDR
              value: "10.0.0.0/24"
#将其取消注释并修改为init.yaml 中定义的网段地址

# apply
kubectl apply -f calico.yaml

okay

# vala 成功了 containerd有两个namespace了
[root@node01 ~]# ctr namespace ls
NAME   LABELS
k8s.io
moby

# 要查询相关 信息
[root@node01 ~]# kubectl get node
NAME     STATUS   ROLES           AGE   VERSION
master   Ready    control-plane   59m   v1.25.0

# 一些其他命令
# k8s 的 clientVersion、serverVersion
kubectl version --output=yaml
# ctr plugin ls
# kubeadm reset,启动失败再来一次
# 查看kubelet的状态
systemctl status kubelet
# 查看kubelet 日志,有时候会失败
journalctl -xeu kubelet
journalctl -xefu kubelet
# 查看所有
kubectl get all --all-namespaces
# 最开始装的flannel,总是失败,后来装了Calico 
[root@node01 ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                       READY   STATUS              RESTARTS   AGE
kube-system   calico-kube-controllers-58dbc876ff-l6thf   0/1     ContainerCreating   0          49m
kube-system   calico-node-6v6kz                          1/1     Running             0          49m
kube-system   coredns-c676cc86f-kq7xm                    0/1     ContainerCreating   0          83m
kube-system   coredns-c676cc86f-qt75m                    0/1     ContainerCreating   0          83m
kube-system   etcd-master                                1/1     Running             0          83m
kube-system   kube-apiserver-master                      1/1     Running             0          83m
kube-system   kube-controller-manager-master             1/1     Running             0          83m
kube-system   kube-proxy-cml8r                           1/1     Running             0          83m
kube-system   kube-scheduler-master                      1/1     Running             0          83m

#装了很久,一直在ContainerCreating状态。
# 查看logs
kubectl -n kube-system describe po calico-kube-controllers-xxxx
# Failed to create pod sandbox: rpc error: code = Unknown desc = failed to setup network for sandbox "27593c24003617558f9ac998b3744aafdfa67fb6fe06e9d530ed03fe331063c8": plugin type="calico" failed (add): error getting ClusterInformation: Get "https://10.0.0.1:443/apis/crd.projectcalico.org/v1/clusterinformations/default": dial tcp 10.0.0.1:443: connect: invalid argument
解决:
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.244.0.0/16
  podSubnet: 10.244.0.0/16

[root@node01 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.244.0.1   <none>        443/TCP   19m

kubectl delete -f kube-flannel.yml

init 失败:

# init时报 kubelet 没启动
# journalctl -xeu kubelet  查看日志,发现这个
Error getting node" err="node \"node01\" not found

解决办法:照着上面说的,配置好containerd和init即可

主要参考了:
https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

https://blog.csdn.net/weixin_43645454/article/details/124952184

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值