运维必备知识点
运维必备知识点
之前的电脑配置不太够,学习K8s就使用“minikube”本机单机部署k8s环境,下载minikube二进制文件启动就可以。现在电脑配置好一些,使用虚拟机部署k8s集群。
部署k8s集群网上方法有很多,大多数都是所有节点安装docker、kubeadm、kubelet,找了一个比较简单的方式,下载安装 kubekey,即可一条命令部署k8s集群。
使用VMware虚拟机 创建3个虚拟机服务,镜像是 CentOS-7-x86_64-DVD-2009.iso 。
192.168.90.128 master 192.168.90.130 note1 192.168.90.131 note2
1.下载安装kubekey。
下载地址:https://github.com/kubesphere/kubekey/releases。
可以下载安装包,直接解压即可得到 kubekey二进制文件“kk” 。可以看需要的版本下载,我用的是 1.2.1 。
把二进制文件“kk” 加入系统环境变量内 :
export KKZONE=cn mv kk /usr/local/bin/
2.在所有节点上安装相关依赖。
yum install -y socat conntrack ebtables ipset
3.所有节点关闭网关防火墙。
setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config systemctl stop firewalld 关闭swap分区 临时关闭:swapoff -a
4.所有节点时间同步。
yum install -y chrony systemctl enable --now chronyd timedatectl set-timezone Asia/Shanghai
5.部署单节点k8s(如要部署集群,跳过此步骤)。
部署单节点的k8s,直接执行:
kk create cluster
指定k8s版本:
kk create cluster --with-kubernetes v1.20.4
6.部署k8s集群。
先创建配置文件,生成一个 config-sample.yaml 文件:
kk create config
编辑config-sample.yaml,如下,配置节点的服务器名称、地址、密码等信息:
apiVersion: kubekey.kubesphere.io/v1alpha1 kind: Cluster metadata: name: sample spec: hosts: - {name: k8s-master, address: 192.168.90.128, internalAddress: 192.168.90.128, port: 22, user: root, password: 123456} - {name: k8s-node1, address: 192.168.90.130, internalAddress: 192.168.90.130, port: 22, user: root, password: 123456} - {name: k8s-node2, address: 192.168.90.131, internalAddress: 192.168.90.131, port: 22, user: root, password: 123456} roleGroups: etcd: - k8s-master master: - k8s-master worker: - k8s-node1 - k8s-node2 controlPlaneEndpoint: ##Internal loadbalancer for apiservers internalLoadbalancer: haproxy domain: lb.kubesphere.local address: "" port: 6443 kubernetes: version: v1.21.5 clusterName: cluster.local network: plugin: calico kubePodsCIDR: 10.233.64.0/18 kubeServiceCIDR: 10.233.0.0/18 registry: registryMirrors: [] insecureRegistries: [] addons: []
环境初始化,此为检测环境是否有问题,也可以跳过此步骤:
kk init os -f config-sample.yaml
执行部署命令, 会在三个节点进行安装应用:
kk create cluster -f config-sample.yaml
如果出现以下报错:
Error: Failed to download cluster config: Failed to unmarshal configuration: json: cannot unmarshal number into Go struct field HostCfg.spec.hosts.password of type string
修改配置文件 config-sample.yaml,把密码加上双引号即可:
- {name: k8s-master, address: 192.168.90.128, internalAddress: 192.168.90.128, port: 22, user: root, password: "123456"} - {name: k8s-node1, address: 192.168.90.130, internalAddress: 192.168.90.130, port: 22, user: root, password: "123456"} - {name: k8s-node2, address: 192.168.90.131, internalAddress: 192.168.90.131, port: 22, user: root, password: "123456"}
等待安装完成......
————————————————————————————————
安装完成后,可以看到三个节点的服务器名称变成了对应的节点名称,并且都在目录/usr/local/bin/内安装了helm 、 kubeadm 、kubectl 、 kubelet等k8s的应用:
[tang@k8s-master ~]$ whereis kubectl kubectl: /usr/local/bin/kubectl [tang@k8s-master ~]$ cd /usr/local/bin/ [tang@k8s-master bin]$ ls etcd etcdctl helm kubeadm kubectl kubelet kube-scripts [tang@k8s-master bin]$
[tang@k8s-node1 ~]$ whereis kubectl kubectl: /usr/local/bin/kubectl [tang@k8s-node1 ~]$ cd /usr/local/bin [tang@k8s-node1 bin]$ ls helm kubeadm kubectl kubelet kube-scripts [tang@k8s-node1 bin]$
[tang@k8s-node2 ~]$ whereis kubectl kubectl: /usr/local/bin/kubectl [tang@k8s-node2 ~]$ cd /usr/local/bin/ [tang@k8s-node2 bin]$ ls helm kubeadm kubectl kubelet kube-scripts [tang@k8s-node2 bin]
切换root用户,查看k8s节点:
[root@k8s-master ~]# kubectl get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME k8s-master Ready control-plane,master 6d23h v1.21.5 192.168.90.128 <none> CentOS Linux 7 (Core) 3.10.0-1160.el7.x86_64 docker://20.10.8 k8s-node1 Ready worker 6d23h v1.21.5 192.168.90.130 <none> CentOS Linux 7 (Core) 3.10.0-1160.el7.x86_64 docker://20.10.8 k8s-node2 Ready worker 6d23h v1.21.5 192.168.90.131 <none> CentOS Linux 7 (Core) 3.10.0-1160.el7.x86_64 docker://20.10.8 [root@k8s-master ~]#
目前基本的k8s环境已经部署完成,还没有部署任何服务,只有k8s应用的服务:
[root@k8s-master ~]# kubectl get pods No resources found in default namespace. [root@k8s-master ~] [root@k8s-master ~]# kubectl get pods -A NAMESPACE NAME READY STATUS RESTARTS AGE kube-system calico-kube-controllers-75ddb95444-p22dg 1/1 Running 3 7d kube-system calico-node-8z46l 1/1 Running 2 7d kube-system calico-node-k2ktb 1/1 Running 2 7d kube-system calico-node-p7qrs 1/1 Running 3 7d kube-system coredns-5495dd7c88-hkc76 1/1 Running 3 7d kube-system coredns-5495dd7c88-nqlmt 1/1 Running 3 7d kube-system haproxy-k8s-node1 1/1 Running 2 7d kube-system haproxy-k8s-node2 1/1 Running 2 7d kube-system kube-apiserver-k8s-master 1/1 Running 3 7d kube-system kube-controller-manager-k8s-master 1/1 Running 45 7d kube-system kube-proxy-tcsxr 1/1 Running 2 7d kube-system kube-proxy-w748v 1/1 Running 2 7d kube-system kube-proxy-xcvwf 1/1 Running 3 7d kube-system kube-scheduler-k8s-master 1/1 Running 44 7d kube-system nodelocaldns-jwrlk 1/1 Running 3 7d kube-system nodelocaldns-kt7c2 1/1 Running 2 7d kube-system nodelocaldns-lm6h9 1/1 Running 2 7d [root@k8s-master ~]
到此基本结束k8s集群部署,以后再部署应用等。
7.kubekey集群维护
① 添加节点:
kk add nodes -f config-sample.yaml
② 删除节点:
kk delete node <nodeName> -f config-sample.yaml
③ 删除集群:
kk delete cluster kk delete cluster [-f config-sample.yaml]
④ 集群升级:
kk upgrade [--with-kubernetes version] [--with-kubesphere version] kk upgrade [--with-kubernetes version] [--with-kubesphere version] [(-f | --file) path]
推荐阅读
>>>新手必备-Linux系统安装配置+Xshell远程连接
运维界升职加薪必备的云计算技术,你学了吗?
学完高级运维云计算课程之后,你可以:
跨越90%企业的招聘硬门槛
增加70%就业机会
拿下BAT全国TOP100大厂敲门砖
体系化得到运维技术硬实力
技术大佬年薪可达30w+