GCDW技术栈 – kubernets 1.26.0运行环境搭建

GCDW运行在k8s上,本文在已经部署好docker运行环境基础上,介绍kubernets v1.26.0 的搭建过程。

上一步

GCDW技术栈- docker运行环境搭建

安装程序

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

安装成功能看到版本

[root@k8s-1 ~]# kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.4", GitCommit:"872a965c6c6526caa949f0c6ac028ef7aff3fb78", GitTreeState:"clean", BuildDate:"2022-11-09T13:35:06Z", GoVersion:"go1.19.3", Compiler:"gc", Platform:"linux/amd64"}
[root@k8s-1 ~]#

开机自启动

systemctl enable --now kubelet
systemctl start docker

生成初始化配置文件

kubeadm config print init-defaults > kubeadm.yaml

编辑配置文件

advertiseAddress

将IP写对

imageRepository

镜像服务器地址调整

name

节点名字调整

taints

注意里面的横线位置对齐,否则文档格式会校验错误。

  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master

完整的配置文件

[root@k8s-81 ~]# cat kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 10.0.2.81
  bindPort: 6443
nodeRegistration:
  criSocket: unix:///var/run/containerd/containerd.sock
  imagePullPolicy: IfNotPresent
  name: k8s-81
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: 1.26.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
scheduler: {}
[root@k8s-81 ~]#

建议提前下载镜像

本文使用的是aliyun的镜像

[root@k8s-83 docker]# kubeadm config images pull --image-repository=registry.aliyuncs.com/google_containers
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-apiserver:v1.26.1
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-controller-manager:v1.26.1
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-scheduler:v1.26.1
[config/images] Pulled registry.aliyuncs.com/google_containers/kube-proxy:v1.26.1
[config/images] Pulled registry.aliyuncs.com/google_containers/pause:3.9
[config/images] Pulled registry.aliyuncs.com/google_containers/etcd:3.5.6-0
[config/images] Pulled registry.aliyuncs.com/google_containers/coredns:v1.9.3
[root@k8s-83 docker]#

初始化

这里用init,有master和控制节点在一起。 如果是单纯扩容worker计算节点,用join命令而不是init。

[root@vm248 ~]# kubeadm init --config=kubeadm.yaml
[init] Using Kubernetes version: v1.26.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 vm248] and IPs [10.96.0.1 172.16.3.248]
[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 vm248] and IPs [172.16.3.248 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost vm248] and IPs [172.16.3.248 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 36.511564 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 vm248 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 vm248 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 172.16.3.248:6443 --token abcdef.0123456789abcdef \
        --discovery-token-ca-cert-hash sha256:60c04264e5c7dc356a1ed362b13b37da2d2bec7ff108c9fbb846e4594f927f9c
[root@vm248 ~]#

token的时效性和重新生成

查看

在控制平面节点上运行以下命令来获取令牌
[root@k8s-81 ~]# kubeadm token list
TOKEN                     TTL         EXPIRES                USAGES                   DESCRIPTION                                                EXTRA GROUPS
abcdef.0123456789abcdef   21h         2023-02-24T03:18:38Z   authentication,signing   <none>                                                     system:bootstrappers:kubeadm:default-node-token
[root@k8s-81 ~]#

注意其中最后的token是有时效性的,如果过期了,需要重新生成

[root@k8s-81 opt]# kubeadm token generate
wqcanr.t9lvabt314fso99o

生成join的命令

[root@k8s-81 opt]#  kubeadm token create wqcanr.t9lvabt314fso99o  --print-join-command --ttl=0
kubeadm join 10.0.2.81:6443 --token wqcanr.t9lvabt314fso99o --discovery-token-ca-cert-hash sha256:ba1128b14a989b26900a12f080cbfcfa8f63305bfdcbcbd15c548af5eda544df

k8s环境变量

root使用,需要执行如下环境变量

export KUBECONFIG=/etc/kubernetes/admin.conf

建议在用户主目录下,复制一份

mkdir ~/.kube
cp /etc/kubernetes/admin.conf ~/.kube/config

查看k8s环境

[root@k8s-81 ~]# kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   160m
[root@k8s-81 ~]#

[root@k8s-81 ~]# kubectl get nodes
NAME     STATUS     ROLES           AGE    VERSION
k8s-81   NotReady   control-plane   170m   v1.26.0
[root@k8s-81 ~]#

查看所有的pods
[root@k8s-81 ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE
kube-system   coredns-5bbd96d687-2w58n         0/1     Pending   0          163m
kube-system   coredns-5bbd96d687-7vksl         0/1     Pending   0          163m
kube-system   etcd-k8s-81                      1/1     Running   1          163m
kube-system   kube-apiserver-k8s-81            1/1     Running   1          163m
kube-system   kube-controller-manager-k8s-81   1/1     Running   1          163m
kube-system   kube-proxy-wrfgf                 1/1     Running   0          163m
kube-system   kube-scheduler-k8s-81            1/1     Running   1          163m
[root@k8s-81 ~]#

安装kube-flannel

下载地址如果不能拿到最新的,请自行找其它能用的。

wget https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml
[root@k8s-81 ~]# kubectl apply -f kube-flannel.yml
namespace/kube-flannel created
serviceaccount/flannel created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
[root@k8s-81 ~]#

修改kubernets配置

/etc/kubernetes/manifests/kube-controller-manager.yaml

增加2个启动参数

vim 
--allocate-node-cidrs=true
--cluster-cidr=10.244.0.0/16

重新部署

kubectl apply -f kube-flannel.yml

重启服务

systemctl restart kubelet

如上步骤【可能】有优化的余地。。。。

将本地节点master加入到worker节点里

默认master是不参与计算worker服务的。

[root@k8s-81 ~]# kubectl taint nodes --all node-role.kubernetes.io/master-
node/k8s-81 untainted

下一步

GCDW在k8s上的部署