二、master节点操作(master节点)
2.1初始化master节点
官网参考:
https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
kubeadm init \ --apiserver-advertise-address=192.168.0.185 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.21.3 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 【若要重新初始化集群状态:kubeadm reset,然后再进行上述操作】
参数说明
kubeadm init \ --apiserver-advertise-address=10.0.0.116 \ #指定master监听的地址,修改为自己的master地址 --image-repository registry.aliyuncs.com/google_containers \ #指定为aliyun的下载源,最好用国内的 --kubernetes-version v1.18.0 \ #指定k8s版本,1.18.0版本比较稳定 --service-cidr=10.96.0.0/12 \ #设置集群内部的网络 --pod-network-cidr=10.244.0.0/16 #设置pod的网络 # service-cidr 和 pod-network-cidr 最好就用这个,不然需要修改后面的 kube-flannel.yaml 文件
记得保存好最后的 kubeadm join的信息node节点加入集群需要token!!!
不然要执行生成token
kubeadm token create --print-join-command #利用kubeadm命令重新生成token,node 节点加入集群需要token
2.2根据日志提示执行以下命令可使用kubectl管理工具
执行上方初始化master节点后程序会打印提示日志(就是设置环境变量)
kubeconfig官网解释
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config #命令的含义:Kubectl 是一个命令行接口,用于对 Kubernetes 集群运行命令。 #Kubectl 在 $HOME/.kube 目录中寻找一个名为 config 的文件。 #你可以通过设置环境变量 KUBECONFIG 或设置 --kubeconfig 参数指定其它 kubeconfig 文件 #当集群初始化成功后,要使 kubectl 适用于非 root 用户,则需要运行以下命令,这些命令也是 kubeadm init输出的一部分 #即创建.kube目录 -> 拷贝配置文件 -> 用户授权 #当然,如果你是 root 用户,则可以直接添加环境变量:执行export KUBECONFIG=/etc/kubernetes/admin.conf kubeectl cluster-info #查看一下是否成功,等待一会儿,同时可以发现像etc,controller,scheduler等组件都以pod的方式安装成功
三、node节点操作(所有node节点)
3.1kube join将node节点加入k8s集群
记得保存初始化master节点的最后打印信息【注意这边要自己的,下面我的只是一个参考】!!!
kubeadm join 192.168.0.185:6443 --token xwc4yz.g1zic1bv52wrwf0r --discovery-token-ca-cert-hash sha256:34c556c6a086d80b587c101f81b1602ba102feccc8dccd74a10fd25ab7cfa0ba #获取前面创建好的token令牌,所有node节点都执行
四、安装部署网络插件(master节点上)
必须安装网络组件否则k8s各个节点都是Noready状态
calico和flannel二选一(本次安装使用的是flannel网路插件)
二者不同可以查看官方文档
4.1calico网络插件
选择calico网络插件版本:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
calico网络插件:
https://docs.tigera.io/v3.9/manifests/tigera-operator.yaml
4.1.1部署方式
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml # 在k8s中安装calico
kubectl get pods --all-namespaces -w # 确认一下calico是否安装成功状态是否正常
4.2flannel网络插件
选择calico网络插件版本:
https://gitcode.net/mirrors/flannel-io/flannel
用wget下载并修改里面的Network字段与初始化master节点设置的一致
https://gitcode.net/mirrors/flannel-io/flannel/-/blob/v0.20.2/Documentation/kube-flannel.yml
flannel插件的yaml
将下面这代码放在kube-flannel.yam文件里,再kubectl apply -f发布一下就好了
4.2.1部署方式
wget https://gitcode.net/mirrors/flannel-io/flannel/-/blob/v0.20.2/Documentation/kube-flannel.yml #下载文件 vim kube-flannel.yml 底线命令模式下 输入/Network 输入i插入 将ip段修改和上文初始化master节点一样就可以了 也可以用下文我修改好的kube-flannel.yml kubectl apply -f kube-flannel.yml
下文为我修改好的kube-flannel.yml
--- kind: Namespace apiVersion: v1 metadata: name: kube-flannel labels: pod-security.kubernetes.io/enforce: privileged --- kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: flannel rules: - apiGroups: - "" resources: - pods verbs: - get - apiGroups: - "" resources: - nodes verbs: - get - list - watch - apiGroups: - "" resources: - nodes/status verbs: - patch - apiGroups: - "networking.k8s.io" resources: - clustercidrs verbs: - list - watch --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: flannel roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: flannel subjects: - kind: ServiceAccount name: flannel namespace: kube-flannel --- apiVersion: v1 kind: ServiceAccount metadata: name: flannel namespace: kube-flannel --- kind: ConfigMap apiVersion: v1 metadata: name: kube-flannel-cfg namespace: kube-flannel labels: tier: node app: flannel data: cni-conf.json: | { "name": "cbr0", "cniVersion": "0.3.1", "plugins": [ { "type": "flannel", "delegate": { "hairpinMode": true, "isDefaultGateway": true } }, { "type": "portmap", "capabilities": { "portMappings": true } } ] } net-conf.json: | { "Network": "10.244.0.0/16", ####初始化master节点时设置一样就行 "Backend": { "Type": "vxlan" } } --- apiVersion: apps/v1 kind: DaemonSet metadata: name: kube-flannel-ds namespace: kube-flannel labels: tier: node app: flannel spec: selector: matchLabels: app: flannel template: metadata: labels: tier: node app: flannel spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/os operator: In values: - linux hostNetwork: true priorityClassName: system-node-critical tolerations: - operator: Exists effect: NoSchedule serviceAccountName: flannel initContainers: - name: install-cni-plugin #image: flannelcni/flannel-cni-plugin:v1.1.2 #for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.2 command: - cp args: - -f - /flannel - /opt/cni/bin/flannel volumeMounts: - name: cni-plugin mountPath: /opt/cni/bin - name: install-cni #image: flannelcni/flannel:v0.20.2 #for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.2 command: - cp args: - -f - /etc/kube-flannel/cni-conf.json - /etc/cni/net.d/10-flannel.conflist volumeMounts: - name: cni mountPath: /etc/cni/net.d - name: flannel-cfg mountPath: /etc/kube-flannel/ containers: - name: kube-flannel #image: flannelcni/flannel:v0.20.2 #for ppc64le and mips64le (dockerhub limitations may apply) image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.2 command: - /opt/bin/flanneld args: - --ip-masq - --kube-subnet-mgr resources: requests: cpu: "100m" memory: "50Mi" limits: cpu: "100m" memory: "50Mi" securityContext: privileged: false capabilities: add: ["NET_ADMIN", "NET_RAW"] env: - name: POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: POD_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - name: EVENT_QUEUE_DEPTH value: "5000" volumeMounts: - name: run mountPath: /run/flannel - name: flannel-cfg mountPath: /etc/kube-flannel/ - name: xtables-lock mountPath: /run/xtables.lock volumes: - name: run hostPath: path: /run/flannel - name: cni-plugin hostPath: path: /opt/cni/bin - name: cni hostPath: path: /etc/cni/net.d - name: flannel-cfg configMap: name: kube-flannel-cfg - name: xtables-lock hostPath: path: /run/xtables.lock type: FileOrCreate
4.2.2如果发布完flannel容器状态不是Running状态的解决方案
确认状态
kubectl get pods -n kube-system # 查看所有名称空间kube-system下的pod
如果出现Running则表示正常,如果出现Init:0/1执行以下命令
sed -i -r "s#quay.io/coreos/flannel:.*-amd64#lizhenliang/flannel:v0.12.0-amd64#g" kube-flannel.yaml #修改 flannel 插件文件,这个版本比较低,高版本 k8s 尽量选择一些高一些的版本 flannel
五、检查各节点健康状态
kubectl get cs #查询master是否正常 #若状态为unhealthy vim /etc/kubernetes/manifests/kube-scheduler.yaml vim /etc/kubernetes/manifests/kube-controller-manager.yaml #将- --port=0注释掉 kubectl get pods -A #检查容器状态节点状态 kubectl get nodes #查询node节点是否ready
到此部署结束