k8s基础--安装
- 硬件环境的要求:
- 步骤
- 1、关闭防火墙、selinux、为每一台机器准备一个docker环境(步骤省)
- 2、 安装yum-utils软件包(提供yum-config-manager 实用程序),配置加速源(都需要安装源)
- 3、配置 Docker使用systemd作为默认Cgroup驱动
- 4、关闭swap分区(都要关闭)
- 5、重新命名主机,在所有主机上上添加如下命令,修改hosts文件(都需要添加)
- 6、每台机器上的/etc/hosts文件都需要修改
- 7、安装kubeadm,kubelet和kubectl
- 8、master主机执行部署Kubernetes Master
- 9、node节点执行,加入集合里面
- 10、 安装网络插件(在master节点执行)
- 注意
硬件环境的要求:
cpu:2c
memory:4G/2G
环境配置:
[root@master ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
步骤
1、关闭防火墙、selinux、为每一台机器准备一个docker环境(步骤省)
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
关闭selinux
setenforce 0 #临时关闭
#永久关闭
sed -i '/^SELINUX/ s/enforcing/disabled/' /etc/selinux/config
#查看
[root@master ~]# getenforce
Permissive
2、 安装yum-utils软件包(提供yum-config-manager 实用程序),配置加速源(都需要安装源)
加快下载速度
yum install -y yum-utils
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3、配置 Docker使用systemd作为默认Cgroup驱动
每台服务器上都要操作,master和node上都要操作
cat <<EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
#重启docker
systemctl restart docker
4、关闭swap分区(都要关闭)
因为k8s的特点是速度快的,他是运行在内存中来保持高速的特点,swap分区的速度慢,k8s不允许这样,这就是为什么k8s对环境有严格的要求
swapoff -a # 临时
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab #永久
5、重新命名主机,在所有主机上上添加如下命令,修改hosts文件(都需要添加)
hostnamectl set-hostname master
hostnamectl set-hostname node-1
hostnamectl set-hostname node-2
同步给node-1和node-2:
scp /etc/hosts node-1:/etc/
scp /etc/hosts node-2:/etc/
修改后重新登入刷新su
6、每台机器上的/etc/hosts文件都需要修改
名字与ip请与自己的配置相同
cat >> /etc/hosts << EOF
192.168.0.104 master
192.168.0.105 node-1
192.168.0.106 node-2
EOF
7、安装kubeadm,kubelet和kubectl
kubeadm --》k8s的管理程序--》在master上运行的--》建立整个k8s集群
kubelet --》在node节点上用来管理容器的--》管理docker,告诉docker程序去启动容器
master和node通信用的--》管理docker,告诉docker程序去启动容器
一个在集群中每个节点(node)上运行的代理。 它保证容器(containers)都 运行在 Pod 中。
kubectl --》在master上用来给node节点发号施令的程序,用来控制node节点的,告诉它们做什么事情的,是命令行操作的工具
集群里的每台服务器都需要做以下安装
a、添加kubernetes YUM软件源
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
b、安装kubeadm,kubelet和kubectl
yum install -y kubelet kubeadm kubectl
c、设置开机自启
systemctl enable --now kubelet
8、master主机执行部署Kubernetes Master
首先我们手动去拉取镜像
docker pull coredns/coredns:1.8.4
给它加一个标签成阿里源,因为k8s下载镜像只会去自己官网下载,现在我们使用阿里源,需要通过它的镜像来使用阿里源
docker tag coredns/coredns:1.8.4 registry.aliyuncs.com/google_containers/coredns:v1.8.4
初始化,多次初始化会报错,如端口被占用,文件已存在
kubeadm init \
#master的ip
--apiserver-advertise-address=192.168.0.104 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
输出结果
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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.0.104:6443 --token vrg5ik.i7axve20nzffd4yt \
--discovery-token-ca-cert-hash sha256:3b85f98c5b4b30256419aef17c1563e2f266d8f27ad5e768e656e21c52f971f2
按照提示操作
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
9、node节点执行,加入集合里面
[root@node-1 ~]# kubeadm join 192.168.0.104:6443 --token vrg5ik.i7axve20nzffd4yt \
--discovery-token-ca-cert-hash sha256:3b85f98c5b4b30256419aef17c1563e2f266d8f27ad5e768e656e21c52f971f2
提示有以下代码,加入成功
This node has joined the cluster
10、 安装网络插件(在master节点执行)
当我们查看状态kubectl get node
,master和node都是Notready状态,因为我们并没有安装网络插件,所以需要我们编辑一个yaml
文件,注意yaml文件格式严格,请前往官网复制
[root@master ~]# 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
部署flanne
l
[root@master ~]# kubectl apply -f kube-flannel.yml
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
查看进程
flanneld进程是一个容器里面的监控进程
[root@master ~]# ps aux|grep flannel
root 320372 1.8 1.4 1265920 26824 ? Ssl 11:12 0:00 /opt/bin/flanneld --ip-masq --kube-subnet-mgr
root 321071 0.0 0.0 12324 1088 pts/0 S+ 11:13 0:00 grep --color=auto flannel
查看状态:
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 172m v1.22.1
node-1 Ready <none> 169m v1.22.1
node-2 Ready <none> 168m v1.22.1
注意
1、部署flannel的时候,由于网络原因,节点并不会马上进入ready状态,过一段时间,再次查看node节点会是ready状态,大概2-3分钟时间,长一个小时也会存在,所以我们可以先在节点上查看日志来排除其他原因journalctl -f -u kubelet.service
2、任何问题我们首先要学会查看日志:
/var/log/messages
3、node节点在join k8s的时候就启动kubelet了
4、常用命令
1 # 获取节点和服务版本信息
2 kubectl get nodes
3 # 获取节点和服务版本信息,并查看附加信息
4 kubectl get nodes -o wide
10 # 获取指定名称空间的pod
11 kubectl get pod -n kube-system