k8s集群

文档

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

init cluster

需要关闭swap,修改containerd配置

swapoff -a
containerd config default > /etc/containerd/config.toml
vim /etc/containerd/config.toml

修改如下(https://45hrqeao.mirror.aliyuncs.com有可能需要登陆阿里云控制台,改成自己的):

67c67
<     sandbox_image = "registry.k8s.io/pause:3.8"
---
>     sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.10.1"
139c139
<             SystemdCgroup = false
---
>             SystemdCgroup = true
170a171,175
>
> [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
> endpoint = ["https://docker.1ms.run", "https://docker-0.unsee.tech", "https://registry-1.docker.io"]
> [plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry.k8s.io"]
> endpoint = ["https://45hrqeao.mirror.aliyuncs.com", "https://k8s.m.daocloud.io"]

其中,镜像源部分,参考https://comate.baidu.com/zh/page/r7unf9fwsn2
然后执行


sudo kubeadm init  --v=5 --image-repository registry.aliyuncs.com/google_containers

输出结果


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 10.157.101.163:6443 --token yjao5h.mvkmgrs4h36xdehy \
        --discovery-token-ca-cert-hash sha256:e8544584ea49315928a34a766cc9532ce2e57ee6bfbbfb9e4d83372366d59a41

配置网络插件

参考https://docs.tigera.io/calico/latest/getting-started/kubernetes/flannel/install-for-flannel#installing-with-the-etcd-datastore,

部署应用

文档:
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
镜像拉取失败的,查看原因:

JYTFY-D1-308-H100-D01-3|2025-11-21 06:28:04[like@ k8s]kubectl get pods
NAME                                   READY   STATUS             RESTARTS   AGE
kubernetes-bootcamp-658f6cbd58-r9kk4   0/1     ImagePullBackOff   0          115s
JYTFY-D1-308-H100-D01-3|2025-11-21 06:29:32[like@ k8s]kubectl describe pod kubernetes-bootcamp-658f6cbd58-r9kk4

可以换成docker.io的镜像

# 删除当前的 deployment
kubectl delete deployment kubernetes-bootcamp

# 使用 Docker Hub 上的镜像重新创建
kubectl create deployment kubernetes-bootcamp --image=jocatalin/kubernetes-bootcamp:v1

# 查看新的 Pod 状态
kubectl get pods

# 查看 Pod 详情
kubectl describe pod kubernetes-bootcamp-<新的pod-id>

把服务暴露在外面

kubectl proxy
再开一个终端
curl http://localhost:8001/version

export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME

curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/

进入pod bash

kubectl exec -ti $POD_NAME -- bash

Leave a Comment