广州网站建设c2c,wordpress 4.2,wordpress 手机顶部悬浮,各网站文风前言
最近学习k8s#xff0c;跟着网上各种教程搭建了简单的版本#xff0c;一个master节点#xff0c;两个node节点#xff0c;这里记录下防止以后忘记。
具体步骤
准备环境
用Oracle VM VirtualBox虚拟机软件安装3台虚拟机#xff0c;一台master节点#xff0c;两台…前言
最近学习k8s跟着网上各种教程搭建了简单的版本一个master节点两个node节点这里记录下防止以后忘记。
具体步骤
准备环境
用Oracle VM VirtualBox虚拟机软件安装3台虚拟机一台master节点两台node节点系统使用centOS 7.9每台虚拟机都是2G内存2核CPU10G硬盘。网络连接使用桥接模式并将IP固定如下
master192.168.1.41node1192.168.1.42node1192.168.1.43
系统初始化全部节点执行
重新命名主机名并修改hosts文件 这步必需做后续node节点join到master的时候如果主机名相同是会报错的。
[rootlocalhost ~] cat /etc/hosts EOF192.168.1.41 k8s-master192.168.1.42 k8s-node1192.168.1.43 k8s-node2EOF修改一些内核参数 这步必需做后续会报错的。
[rootlocalhost ~] cat /etc/sysctl.conf EOFnet.bridge.bridge-nf-call-ip6tables 1net.bridge.bridge-nf-call-iptables 1net.ipv4.ip_nonlocal_bind 1net.ipv4.ip_forward 1vm.swappiness0EOF
[rootlocalhost ~] sysctl -p #让参数生效到内核里面关闭防火墙
[rootlocalhost ~] systemctl stop firewalld
[rootlocalhost ~] systemctl disable firewalld关闭 selinux
[rootlocalhost ~] sed -i s/enforcing/disabled/ /etc/selinux/config # 永久重启后生效关闭 swap
vi /etc/fstab
#注释掉下面的设置
/dev/mapper/centos-swap swap时间同步
[rootlocalhost ~] yum install ntpdate -y
[rootlocalhost ~] ntpdate time.windows.com安装 Docker全部节点执行 Kubernetes 默认 CRI 容器运行时 为 Docker注意这里选择的版本和后边的k8s是能匹配的。
安装指定版本docker
[rootlocalhost ~] wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
[rootlocalhost ~] yum -y install docker-ce-20.10.7-3.el7
[rootlocalhost ~] systemctl enable docker systemctl start docker
[rootlocalhost ~] docker --version配置docker 镜像加速器 没有daemon.json这个文件就创建一个
[rootlocalhost ~] cat /etc/docker/daemon.json EOF{ exec-opts: [native.cgroupdriversystemd], registry-mirrors: [https://kn0t2bca.mirror.aliyuncs.com]} EOFk8s镜像切换成国内源
[rootlocalhost ~] cat /etc/yum.repos.d/kubernetes.repo EOF[kubernetes]nameKubernetesbaseurlhttps://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64enabled1gpgcheck0repo_gpgcheck0gpgkeyhttps://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOF安装 kubeadm、 kubelet 和 kubectl全部节点执行
[rootlocalhost ~] yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
[rootlocalhost ~] systemctl enable kubelet #设置kubelet开机启动注意master服务此时无法启动因为没有配置文件 /var/lib/kubelet/config.yaml 这个文件在 kubeadm init 的[kubelet-start]这一步写进来的。且写完后kubeadm帮启动了无需自己启。
node节点也无法启动查看日志也是没有配置文件 /var/lib/kubelet/config.yaml 这个文件在 kubeadm join 的[kubelet-start]这一步写进来的。
部署K8s mastermaster节点执行
master节点初始化 注意敲打命令时\后不要有空格
[rootlocalhost ~] kubeadm init \--apiserver-advertise-address192.168.1.41 \ #集群通告地址(master 机器IP)--image-repository registry.aliyuncs.com/google_containers \ #由于默认拉取镜像地址k8s.gcr.io国内无法访问这里指定阿里云镜像仓库地址--kubernetes-version v1.23.6 \ #K8s版本与上面安装的一致--service-cidr10.96.0.0/12 \ #集群内部虚拟网络Pod统一访问入口--pod-network-cidr10.244.0.0/16 #Pod网络与下面部署的CNI网络组件yaml中保持一致初始化后日志的最后有一行如下这个是在node节点执行的默认token有效期为24小时当过期之后该token就不可用了这时就需要重新创建token。
[rootlocalhost ~] kubeadm join 192.168.1.41:6443 --token ih48s6.x1mjtokujdyt9ysu \--discovery-token-ca-cert-hash sha256:392750ed054b8288000d3969e8fdf47cc665c4bf8f8f025a255a457f5ec74814 使用 kubectl 工具创建必要文件
[rootlocalhost ~] mkdir -p $HOME/.kube
[rootlocalhost ~] sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[rootlocalhost ~] sudo chown $(id -u):$(id -g) $HOME/.kube/config部署容器网络CNI k8s的网络插件作用就是实现不同宿主机之间pod的通信需要下载kube-flannel.yaml 文件并修改定义Pod网络CALICO_IPV4POOL_CIDR与前面kubeadm init的–pod-network-cidr指定的一样这里给出这个文件
---
kind: Namespace
apiVersion: v1
metadata:name: kube-flannellabels:pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: flannel
rules:
- apiGroups:- resources:- podsverbs:- get
- apiGroups:- resources:- nodesverbs:- list- watch
- apiGroups:- resources:- nodes/statusverbs:- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: flannel
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: flannel
subjects:
- kind: ServiceAccountname: flannelnamespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:name: flannelnamespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:name: kube-flannel-cfgnamespace: kube-flannellabels:tier: nodeapp: 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,Backend: {Type: vxlan}}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:name: kube-flannel-dsnamespace: kube-flannellabels:tier: nodeapp: flannel
spec:selector:matchLabels:app: flanneltemplate:metadata:labels:tier: nodeapp: flannelspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/osoperator: Invalues:- linuxhostNetwork: truepriorityClassName: system-node-criticaltolerations:- operator: Existseffect: NoScheduleserviceAccountName: flannelinitContainers:- name: install-cni-plugin#image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0command:- cpargs:- -f- /flannel- /opt/cni/bin/flannelvolumeMounts:- name: cni-pluginmountPath: /opt/cni/bin- name: install-cni#image: flannelcni/flannel:v0.19.1 for ppc64le and mips64le (dockerhub limitations may apply)image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.1command:- cpargs:- -f- /etc/kube-flannel/cni-conf.json- /etc/cni/net.d/10-flannel.conflistvolumeMounts:- name: cnimountPath: /etc/cni/net.d- name: flannel-cfgmountPath: /etc/kube-flannel/containers:- name: kube-flannel#image: flannelcni/flannel:v0.19.1 for ppc64le and mips64le (dockerhub limitations may apply)image: docker.io/rancher/mirrored-flannelcni-flannel:v0.19.1command:- /opt/bin/flanneldargs:- --ip-masq- --kube-subnet-mgrresources:requests:cpu: 100mmemory: 50Milimits:cpu: 100mmemory: 50MisecurityContext:privileged: falsecapabilities:add: [NET_ADMIN, NET_RAW]env:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespace- name: EVENT_QUEUE_DEPTHvalue: 5000volumeMounts:- name: runmountPath: /run/flannel- name: flannel-cfgmountPath: /etc/kube-flannel/- name: xtables-lockmountPath: /run/xtables.lockvolumes:- name: runhostPath:path: /run/flannel- name: cni-pluginhostPath:path: /opt/cni/bin- name: cnihostPath:path: /etc/cni/net.d- name: flannel-cfgconfigMap:name: kube-flannel-cfg- name: xtables-lockhostPath:path: /run/xtables.locktype: FileOrCreate执行部署flannel
[rootlocalhost ~] kubectl apply -f kube-flannel.yaml查看节点状态
[rootlocalhost ~] kubectl get pods -n nodes#查看通信状态
[rootlocalhost ~] kubectl get pods -n kube-system -w部署K8s nodenode节点执行 在每个node节点上执行master节点初始化时生成的join语句如下
[rootlocalhost ~] kubeadm join 192.168.1.41:6443 --token ih48s6.x1mjtokujdyt9ysu \--discovery-token-ca-cert-hash sha256:392750ed054b8288000d3969e8fdf47cc665c4bf8f8f025a255a457f5ec74814 测试K8s 在 Kubernetes 集群中创建一个 pod 验证是否正常运行在master节点执行。
[rootlocalhost ~] kubectl create deployment nginx --imagenginx
[rootlocalhost ~] kubectl expose deployment nginx --port80 --typeNodePort
[rootlocalhost ~] kubectl get pod,svc执行kubectl get pod,svc后如下图中有端口号 浏览器访问地址 http://NodeIP:Port例如http://192.168.1.42:31220出现Nginx首页即安装搭建成功。
总结
安装过程会遇到各种问题 1.比如docker和k8s的版本不匹配因为我第一次安装的时候docker安装了最新版导致不匹配删除docker后重新安装指定版本就好。 2.kubectl init \报错后再次执行提示各种文件已经生产了删除相关文件后重新初始化就好。 3.像kubectl get nodes等命令只能在master上执行node节点执行会报错Unable to connect to the server: dial tcp: lookup localhost on 8.8.8.8:53: no such host