0
点赞
收藏
分享

微信扫一扫

nvm安装详细教程(卸载旧的nodejs,安装nvm、node、npm、cnpm、yarn及环境变量配置)

北溟有渔夫 2023-11-19 阅读 43

目录

1 环境准备

2 所有节点安装docker

3 所有节点安装kubeadm,kubelet和kubectl

4 部署K8S集群

4.1 查看初始化需要的镜像

4.2 初始化kubeadm

4.3 设定kubectl

4.4 所有节点部署网络插件flannel


master(2C/4G,cpu核心数要求大于2) 192.168.30.105 docker、kubeadm、kubelet、kubectl、flannel

node01(2C/2G) 192.168.30.101 docker、kubeadm、kubelet、kubectl、flannel

node02(2C/2G) 192.168.30.102 docker、kubeadm、kubelet、kubectl、flannel

Harbor节点(hub.kgc.com) 192.168.30.109 docker、docker-compose、harbor-offline-v1.2.2

1、在所有节点上安装Docker和kubeadm

2、部署Kubernetes Master

3、部署容器网络插件

4、部署 Kubernetes Node,将节点加入Kubernetes集群中

5、部署 Dashboard Web 页面,可视化查看Kubernetes资源

6、部署 Harbor 私有仓库,存放镜像资源

1 环境准备

//所有节点,关闭防火墙规则,关闭selinux,关闭swap交换

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
swapoff -a #交换分区必须要关闭
sed -ri 's/.*swap.*/# /etc/fstab #永久关闭swap分区,&符号在sed命令中代表上次匹配的结果
#加载 ip_vs 模块
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

//修改主机名

hostnamectl set-hostname master01
hostnamectl set-hostname node01
hostnamectl set-hostname node02

//所有节点修改hosts文件

vim /etc/hosts
192.168.30.105 master01
192.168.30.101 node01
192.168.30.102 node02

//调整内核参数

cat > /etc/sysctl.d/kubernetes.conf << EOF
#开启网桥模式,可将网桥的流量传递给iptables链
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
#关闭ipv6协议
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOF

//生效参数

sysctl --system  

2 所有节点安装docker

yum install -y yum-utils device-mapper-persistent-data lvm2 
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io

mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "500m", "max-file": "3"
}
}
EOF

#使用Systemd管理的Cgroup来进行资源控制与管理,因为相对Cgroupfs而言,Systemd限制CPU、内存等资源更加简单和成熟稳定。 #日志使用json-file格式类型存储,大小为100M,保存在/var/log/containers目录下,方便ELK等日志系统收集和管理日志。

#使用Systemd管理的Cgroup来进行资源控制与管理,因为相对Cgroupfs而言,Systemd限制CPU、内存等资源更加简单和成熟稳定。 #日志使用json-file格式类型存储,大小为100M,保存在/var/log/containers目录下,方便ELK等日志系统收集和管理日志。

systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service

docker info | grep "Cgroup Driver"
Cgroup Driver: systemd

3 所有节点安装kubeadm,kubelet和kubectl

//定义kubernetes源

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum install -y kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15

//开机自启kubelet

systemctl enable kubelet.service

#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启

4 部署K8S集群

4.1 查看初始化需要的镜像

kubeadm config images list --kubernetes-version 1.20.15

//在 master 节点上传 v1.20.15.zip 压缩包至 /opt 目录

unzip v1.20.15.zip -d /opt/k8s
cd /opt/k8s/
for i in $(ls *.tar); do docker load -i $i; done

//复制镜像和脚本到 node 节点,并在 node 节点上执行脚本加载镜像文件

scp -r /opt/k8s root@node01:/opt
scp -r /opt/k8s root@node02:/opt

4.2 初始化kubeadm

方法一:

kubeadm config print init-defaults > /opt/kubeadm-config.yaml

cd /opt/
vim kubeadm-config.yaml
......
11 localAPIEndpoint:
12 advertiseAddress: 192.168.30.105 #指定master节点的IP地址
13 bindPort: 6443
......
32 imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers #指定拉取镜像的仓库,默认是k8s.gcr.io
33 kind: ClusterConfiguration
34 kubernetesVersion: v1.20.15 #指定kubernetes版本号
35 networking:
36 dnsDomain: cluster.local
37 podSubnet: "10.244.0.0/16" #指定pod网段,10.244.0.0/16用于匹配flannel默认网段
38 serviceSubnet: 10.96.0.0/16 #指定service网段
39 scheduler: {}

#末尾再添加以下内容

apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs #把默认的kube-proxy调度方式改为ipvs模式

//在线拉取镜像

kubeadm config images pull --config /opt/kubeadm-config.yaml

//初始化 master

kubeadm init --config=/opt/kubeadm-config.yaml --upload-certs | tee kubeadm-init.log
#--upload-certs 参数可以在后续执行加入节点时自动分发证书文件
#tee kubeadm-init.log 用以输出日志

//查看 kubeadm-init 日志

less kubeadm-init.log

//kubernetes配置文件目录

ls /etc/kubernetes/

//存放ca等证书和密码的目录

ls /etc/kubernetes/pki

方法二:

kubeadm init \
--apiserver-advertise-address=192.168.80.10 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.20.15 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \

--token-ttl=0

方法二初始化后需要修改 kube-proxy 的 configmap,开启 ipvs

kubectl edit cm kube-proxy -n=kube-system
修改mode: ipvs



提示:
......
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

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.80.10:6443 --token rc0kfs.a1sfe3gl4dvopck5 \
--discovery-token-ca-cert-hash sha256:864fe553c812df2af262b406b707db68b0fd450dc08b34efb73dd5a4771d37a2

4.3 设定kubectl

kubectl需经由API server认证及授权后方能执行相应的管理操作,kubeadm 部署的集群为其生成了一个具有管理员权限的认证配置文件 /etc/kubernetes/admin.conf,它可由 kubectl 通过默认的 “$HOME/.kube/config” 的路径进行加载。

mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

//如果 kubectl get cs 发现集群不健康,更改以下两个文件

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

修改如下内容

把--bind-address=127.0.0.1变成--bind-address=192.168.80.10 #修改成k8s的控制节点master01的ip
把httpGet:字段下的hosts由127.0.0.1变成192.168.80.10(有两处)
#- --port=0 # 搜索port=0,把这一行注释掉

systemctl restart kubelet

4.4 所有节点部署网络插件flannel

方法一: //所有节点上传 flannel 镜像 flannel.tar 和网络插件 cni-plugins-linux-amd64-v0.8.6.tgz 到 /opt 目录,master节点上传 kube-flannel.yml 文件

cd /opt
docker load < flannel.tar
docker load < flannel-cni-plugin.tar
mv /opt/cni /opt/cni_bak
mkdir -p /opt/cni/bin
tar zxvf cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin

//在 master 节点创建 flannel 资源

cd /opt
docker load < flannel.tar
docker load < flannel-cni-plugin.tar
mv /opt/cni /opt/cni_bak
mkdir -p /opt/cni/bin
tar zxvf cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin

kubectl apply -f kube-flannel.yml

方法二:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

//在 node 节点上执行 kubeadm join 命令加入群集

kubeadm join 192.168.30.105:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:2fbbd9a76750e2778f056e60e2aa3bc499a6491e745f09b453741a25c6881123

//在master节点查看节点状态

kubectl get nodes
NAME STATUS ROLES AGE VERSION
master01 Ready control-plane,master 55m v1.20.15
node01 Ready <none> 10m v1.20.15
node02 Ready <none> 11m v1.20.15


kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-54d67798b7-2tfmh 1/1 Running 0 55m
coredns-54d67798b7-pbxsd 1/1 Running 0 55m
etcd-master01 1/1 Running 0 55m
kube-apiserver-master01 1/1 Running 0 55m
kube-controller-manager-master01 1/1 Running 0 47m
kube-proxy-kml8t 1/1 Running 0 55m
kube-proxy-sb966 1/1 Running 0 10m
kube-proxy-t6wfj 1/1 Running 0 11m
kube-scheduler-master01 1/1 Running 0 50m

//测试 pod 资源创建

kubectl create deployment nginx --image=nginx

kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6799fc88d8-nzdk7 1/1 Running 0 54s 10.244.1.2 node02 <none> <none>

//暴露端口提供服务

kubectl expose deployment nginx --port=80 --type=NodePort

kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 60m
nginx NodePort 10.96.38.255 <none> 80:32112/TCP 99s

//测试访问

curl http://node02:32112

//扩展3个副本

kubectl scale deployment nginx --replicas=3
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6799fc88d8-nzdk7 1/1 Running 0 6m 10.244.1.2 node02 <none> <none>
nginx-6799fc88d8-q4vdt 1/1 Running 0 33s 10.244.2.3 node01 <none> <none>
nginx-6799fc88d8-zw949 1/1 Running 0 33s 10.244.2.2 node01 <none> <none>

举报

相关推荐

0 条评论