Docker系列文-----一台全新Linux系统简易搭建k8s集群全过程 建议收藏!!(6)
- 一.新建一台全新的机器
- 二.关闭防火墙和Selinux等文件
- 三.卸载旧版本的docker以及相关文件:
- 四.安装yum-utils软件包并且配置aliyun的源
- 五.安装docker设置开机自启
- 六.配置Docker使用systemd作为默认Cgroup驱动
- 七.关闭swap分区(kubernetes关闭swap主要是为了性能考虑)
- 八.安装Kubeadm、kubelet、kubectl:
- 九.添加kubenetes YUM源
- 十.配置Kubernetes Master
- 十一.初始化(kubeadm init)
- 十二.按照输出提示 运行命令
- 十三.安装网络服务插件:(kube-flannel)
- 十四.部署flannel服务
- 十五.查看状态
- 十六.关于节点
- 十七.测试
一.新建一台全新的机器
省略了前面Linux系统的安装过程
查看一下系统的版本:
[root@gogogo ~]# cat /etc/centos-release
CentOS Linux release 8.3.2011
二.关闭防火墙和Selinux等文件
[root@localhost ~]# hostnamectl set-hostname 'gogogo'
[root@gogogo ~]# vim /etc/selinux/config
SELINUX=disabled
[root@gogogo ~]# setenforce 0
关闭防火墙
[root@gogogo ~]# systemctl stop firewalld
[root@gogogo ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
三.卸载旧版本的docker以及相关文件:
[root@gogogo ~]# yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
四.安装yum-utils软件包并且配置aliyun的源
yum install -y yum-utils
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
五.安装docker设置开机自启
yum install -y docker-ce docker-ce-cli containerd.io
[root@gogogo ~]# systemctl start docker
[root@gogogo ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
六.配置Docker使用systemd作为默认Cgroup驱动
[root@gogogo ~]# cat <<EOF > /etc/docker/daemon.json
> {
> "exec-opts": ["native.cgroupdriver=systemd"]
> }
> EOF
七.关闭swap分区(kubernetes关闭swap主要是为了性能考虑)
swapoff -a # 临时
[root@gogogo ~]# vi /etc/fstab
#/dev/mapper/cl-swap none swap defaults 0 0
八.安装Kubeadm、kubelet、kubectl:
kubeadm --》k8s的管理程序–》在master上运行的–》建立整个k8s集群
kubelet --》在node节点上用来管理容器的–》管理docker,告诉docker程序去启动容器
master和node通信用的–》管理docker,告诉docker程序去启动容器
一个在集群中每个节点(node)上运行的代理。 它保证容器(containers)都 运行在 Pod 中。
kubectl --》在master上用来给node节点发号施令的程序,用来控制node节点的,告诉它们做什么事情的,是命令行操作的工具
九.添加kubenetes YUM源
[root@gogogo ~]# 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
十.配置Kubernetes Master
直接初始化(kubeadm init)会报错
需要替换tag 为 阿里云的(init所要求的tag标签)
[root@gogogo ~]# docker pull coredns/coredns:1.8.4
[root@gogogo ~]# docker tag coredns/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4
十一.初始化(kubeadm init)
[root@gogogo ~]# kubeadm init \
> --apiserver-advertise-address=192.168.2.19 \
> --image-repository registry.aliyuncs.com/google_containers \
> --service-cidr=10.1.0.0/16 \
> --pod-network-cidr=10.244.0.0/16
192.168.2.19 是k8s Master的ip。
–service-cidr 是为服务VIP使用可选IP地址范围(默认值“10.96.0.0/12”)。
–pod-network-cidr string 指定pod网络的IP地址范围。如果设置,控制平面将自动为每个节点分配CIDR。
十二.按照输出提示 运行命令
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
kubeadm join 192.168.2.19:6443 --token q7swys.w4w2ivlmg2elcdnz \
--discovery-token-ca-cert-hash sha256:27ea78090a55e95250af517ab33bbe863b5e84bc2fcc430897b8a25dd913ac6e
执行:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
使用下述命令可以将节点加入集群:
kubeadm join 192.168.2.19:6443 --token q7swys.w4w2ivlmg2elcdnz \
--discovery-token-ca-cert-hash sha256:27ea78090a55e95250af517ab33bbe863b5e84bc2fcc430897b8a25dd913ac6e
注意:token的有效时间是24小时,过期了需要使用下述命令重新获取
[root@manager149 docker]# kubeadm token create --print-join-command
十三.安装网络服务插件:(kube-flannel)
Flannel是CoreOS团队针对Kubernetes设计的一个网络服务,它的功能是让集群中的不同节点创建的Docker容器都具有全集群唯一的虚拟IP地址,Flannel的设计目的就是为集群中的所有节点重新规划IP地址,从而使得不同节点上的容器能够获得“同属一个内网”且”不重复的”IP地址,并让属于不同节点上的容器能够直接通过内网IP通信,Flannel实质上是一种“覆盖网络(overlaynetwork)”,也就是将TCP数据包装在另一种网络包里面进行路由转发和通信,目前已经支持udp、vxlan、host-gw、aws-vpc、gce路由等数据转发方式。
[root@gogogo ~]# vi kube-flannel.yml
[root@gogogo ~]# cat kube-flannel.yml
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: psp.flannel.unprivileged
annotations:
seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
privileged: false
volumes:
- configMap
- secret
- emptyDir
- hostPath
allowedHostPaths:
- pathPrefix: "/etc/cni/net.d"
- pathPrefix: "/etc/kube-flannel"
- pathPrefix: "/run/flannel"
readOnlyRootFilesystem: false
# Users and groups
runAsUser:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAny
# Privilege Escalation
allowPrivilegeEscalation: false
defaultAllowPrivilegeEscalation: false
# Capabilities
allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
defaultAddCapabilities: []
requiredDropCapabilities: []
# Host namespaces
hostPID: false
hostIPC: false
hostNetwork: true
hostPorts:
- min: 0
max: 65535
# SELinux
seLinux:
# SELinux is unused in CaaSP
rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
rules:
- apiGroups: ['extensions']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
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-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
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",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds
namespace: kube-system
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
image: quay.io/coreos/flannel:v0.13.1-rc2
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: quay.io/coreos/flannel:v0.13.1-rc2
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
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
十四.部署flannel服务
[root@gogogo ~]# kubectl apply -f kube-flannel.yml
Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
十五.查看状态
[root@gogogo ~]# docker ps |grep flan
0be911fe55b2 dee1cac4dd20 "/opt/bin/flanneld -…" About a minute ago Up About a minute k8s_kube-flannel_kube-flannel-ds-s8hlp_kube-system_256a607c-b60c-4982-ab49-5140c7c5c509_0
358ad86ca882 registry.aliyuncs.com/google_containers/pause:3.5 "/pause" 5 minutes ago Up 5 minutes k8s_POD_kube-flannel-ds-s8hlp_kube-system_256a607c-b60c-4982-ab49-5140c7c5c509_0
[root@gogogo ~]# ps aux|grep flan
root 21795 0.1 1.9 1265920 36104 ? Ssl 16:17 0:00 /opt/bin/flanneld --ip-masq --kube-subnet-mgr
root 22892 0.0 0.0 12324 1112 pts/0 S+ 16:18 0:00 grep --color=auto flan
flannel网络组件一安装好,状态就变成ready了
[root@gogogo ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
gogogo Ready control-plane,master 14m v1.22.1
十六.关于节点
节点机node:- 下载 coredns 镜像 docker pull coredns/coredns:1.8.4
- 安装kubeadm,kubelet和kubectl yum install -y kubelet kubeadm kubectl
- 关闭交换分区 swapoff -a /vi /etc/fstab 注释掉最后一行
- kubeadm join 192.168.2.19:6443 --token q7swys.w4w2ivlmg2elcdnz \ –
discovery-token-ca-cert-hash sha256:27ea78090a55e95250af517ab33bbe863b5e84bc2fcc430897b8a25dd913ac6e
注意:手动启动kubelet后 不能再join了 join会报错
所以节点必须 直接join 自动去启动kubelet 不要手动启动kubelet
十七.测试
创建一个nginx的pod
[root@gogogo ~]# kubectl run sc-nginx --image=nginx --port=8080
pod/sc-nginx created
刚开始容器需要创建,等待2-3m即可创建好
[root@manager149 docker]# kubectl get pod
NAME READY STATUS RESTARTS AGE
sc-nginx 0/1 ContainerCreating 0 6s
[root@manager149 docker]# kubectl get pod
NAME READY STATUS RESTARTS AGE
sc-nginx 1/1 Running 0 120m
这时候我们一个简单的k8s集群就搭建成功了