1. 使用kubeadm方式安装
1.1 环境准备介绍
角色 | IP地址 | 操作系统 | 配置 | kubernetes | docker |
---|---|---|---|---|---|
master | 192.168.199.100 | centos7.x | 2核/2G/50G | 1.18.0 | 18.06.3 |
node1 | 192.168.199.101 | centos7.x | 2核/2G/50G | 1.18.0 | 18.06.3 |
node2 | 192.168.199.102 | centos7.x | 2核/2G/50G | 1.18.0 | 18.06.3 |
1.2 准备工作
1.2.1 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
1.2.2 设置主机名
hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2
1.2.3 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config #永久
setenforce 0 #临时
getenforce #查看当前selinux 状态
1.2.4 关闭swap分区
sed -ri 's/.*swap.*/#&/' /etc/fstab #注释swap行
swapoff -a #临时关闭 重启无效
lsblk #查看swap关闭情况
1.2.5 将桥接的IPv4流量传递到iptables的链
cat > /etc/modules-load.d/k8s.conf << EOF
br_netfilter
EOF
modprobe br_netfilter
lsmod | grep br_netfilter
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
# 生效
sysctl --system
1.2.6 k8s-master执行: 推荐dns解析
cat >> /etc/hosts << EOF
192.168.199.100 k8s-master k8s-api.local.cn
192.168.199.101 k8s-node1
192.168.199.102 k8s-node2
EOF
2. 每个节点安装Docker、kubeadm、kubelet和kubectl
2.1 安装docker 18.0.6.3
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.3.ce-3.el7
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <
apt-get update
apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install docker-ce docker-ce-cli containerd.io
2.2 安装kubeadm、kubelet和kubectl
2.2.1 跟换国内源
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
2.2.2 安装kubeadm、kubelet和kubectl
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat </etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
2.2.3 部署k8s的Master节点
kubeadm init \
--apiserver-advertise-address=192.168.199.100 \
--image-repository registry.aliyuncs.com/google_containers \
--control-plane-endpoint k8s-api.local.cn \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16 \
--token-ttl 0
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join k8s-api.local.cn:6443 --token 2l64r1.7kf53k1aux25kbdl \
--discovery-token-ca-cert-hash sha256:75a43008d91f5fbc23e0caedb6952fbb800fbe3204c7e3c386f9a6aa1031b805 \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join k8s-api.local.cn:6443 --token 2l64r1.7kf53k1aux25kbdl \
--discovery-token-ca-cert-hash sha256:75a43008d91f5fbc23e0caedb6952fbb800fbe3204c7e3c386f9a6aa1031b805
有效期默认24H 复制到node节点执行
kubeadm token create --print-join-command 再次生成
kubeadm token create --ttl 0 --print-join-command 永久有效
kubectl get nodes 查看加入信息
useradd -m -s /bin/bash ilinux
echo -e 'test\ntest' |passwd ilinux
echo 'ilinux ALL=(ALL:ALL) NOPASSWD: ALL'>/etc/sudoers.d/ilinux
su - ilinux
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
useradd ik8s
su - ik8s
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3. 安装 Pod 网络插件(CNI)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[root@hk ~]# 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-rc1
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-rc1
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
4.部署k8s的node节点
kubeadm join k8s-api.local.cn:6443 --token 2l64r1.7kf53k1aux25kbdl \
--discovery-token-ca-cert-hash sha256:75a43008d91f5fbc23e0caedb6952fbb800fbe3204c7e3c386f9a6aa1031b805
测试
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
访问地址:http://NodeIP:Port
5.部署k8s的另一个master节点
#将k8s中三个证书上传k8s的secrets中
root@k8s-master:~# kubeadm init phase upload-certs --upload-certs
[upload-certs] Storing the certificates in Secret "kubeadm-certs" in the "kube-system" Namespace
[upload-certs] Using certificate key:
8409b541f062724223bfca07a16fab92540164a96d606947fcbcf55584768dea
ilinux@k8s-master:~$ kubectl get secrets -n kube-system
kubeadm-certs Opaque 8 70s
kubeadm join k8s-api.local.cn:6443 --token 2l64r1.7kf53k1aux25kbdl \
--discovery-token-ca-cert-hash sha256:75a43008d91f5fbc23e0caedb6952fbb800fbe3204c7e3c386f9a6aa1031b805 \
--control-plane --certificate-key 8409b541f062724223bfca07a16fab92540164a96d606947fcbcf55584768dea
6. 修改k8s 集群从iptables为 ipvsadm
[root@k8s-master ~]# kubectl get configmap -n kube-system
NAME DATA AGE
coredns 1 43h
extension-apiserver-authentication 6 43h
kube-flannel-cfg 2 43h
kube-proxy 2 43h
kubeadm-config 2 43h
kubelet-config-1.18 1 43h
[root@k8s-master ~]# kubectl edit cm kube-proxy -n kube-system
搜索mode 修改如下
mode: "ipvs"
kubectl delete pods -l k8s-app=kube-proxy -n kube-system 杀掉kube-proxy 容器会自动创建新的
node节点
yum install ipvsadm -y
ipvsadm -Ln 查看
7.部署dashboard
7.1 可视化界面
kubernetes官方提供的可视化界面
https://github.com/kubernetes/dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml
kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
修改:type: ClusterIP 改为 type: NodePort
#查看随机分配的端口
kubectl get svc -A |grep kubernetes-dashboard
https://192.168.199.100:30248
7.2 创建用户登录
#创建用户登录
vim dash.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
kubelet apply -f dash.yaml
#获取访问令牌 复制进去登录
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
eyJhbGciOiJSUzI1NiIsImtpZCI6IjQzR084MXRUX210Z3ZxNV9RUVNyb0dHUkwwYXE5a2hZREE3YUQ2QnQzTkEifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWZxcGo2Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJmMjI4YTIxZi0zODc1LTRjZTMtYWNiNi0zN2ExZmQxM2FjY2MiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.ILNTzVQgY7VsVTZ60TKE_FEw8pQhwfEupWZzLwrsB6qG6Rg0kEihnor4eTfSvjDJUyCk1a2mCCG0RGNwcrjPlLVsf2pGzrxBp1kDByVcvObolEiNyfxTHs8lPJUtcJKuatyumj7IeyyzFibWRxh708yiK_6FMXSCtKRzBrU-jtfbSlkMglVQBVYtvdF165lsaybFVEM0zurcQVQ9aN8soebD-iHLZ_eFVjThzZFve-oWKXxskkT7g9zf8r580VHFSejp0zRoPVp2iS6krpldB6KchbNC2ZK3AKnossgIQT2afF0ngfOxDo1jLVvKZsEQAh_VCXKOnZGHikYIi_3epw
8.部署ingress-nginx
K8S 1.2.1以下版本
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.47.0/deploy/static/provider/baremetal/deploy.yaml
K8S 1.2.1以上版本
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.3/deploy/static/provider/cloud/deploy.yaml
# 检查安装的结果
kubectl get pod,svc -n ingress-nginx