k8s1.20.0 部署
所有节点
vim /etc/hosts
10.40.65.184 master
10.40.65.107 node1
ssh-keygen
ssh-copy-id master
ssh-copy-id node1
yum install docker-ce -y
yum install socat conntrack-tools ebtables -y
打开kubernetes源
[kubernetes]
name=Loongnix server $releasever kubernetes
baseurl=http://pkg.loongnix.cn/loongnix-server/$releasever/cloud/$basearch/release/kubernetes/
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-LOONGNIX
module_hotfixes=1
yum install cri-tools kubeadm kubectl kubelet kubernetes-cni -y
systemctl enable docker.service && systemctl restart docker && systemctl status docker
systemctl enable kubelet.service && systemctl restart kubelet
docker 设置
查看docker状态:
[root@master opt]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-07-14 09:43:38 CST; 29s ago
Docs: https://docs.docker.com
Main PID: 4837 (dockerd)
Tasks: 10
Memory: 40.7M
CGroup: /system.slice/docker.service
└─4837 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
7月 14 09:43:37 master dockerd[4837]: time="2021-07-14T09:43:37.652737990+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
7月 14 09:43:37 master dockerd[4837]: time="2021-07-14T09:43:37.725904370+08:00" level=warning msg="Your kernel does not support swap memory limit"
7月 14 09:43:37 master dockerd[4837]: time="2021-07-14T09:43:37.726143470+08:00" level=info msg="Loading containers: start."
7月 14 09:43:38 master dockerd[4837]: time="2021-07-14T09:43:38.023472700+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.18.0.0/16. Daemon option --bip c>
7月 14 09:43:38 master dockerd[4837]: time="2021-07-14T09:43:38.173480930+08:00" level=info msg="Loading containers: done."
7月 14 09:43:38 master dockerd[4837]: time="2021-07-14T09:43:38.211472800+08:00" level=warning msg="Not using native diff for overlay2, this may cause degraded performance for building imag>
7月 14 09:43:38 master dockerd[4837]: time="2021-07-14T09:43:38.211825210+08:00" level=info msg="Docker daemon" commit=46229ca1d815cfd4b50eb377ac75ad8300e13a85 graphdriver(s)=overlay2 versi>
7月 14 09:43:38 master dockerd[4837]: time="2021-07-14T09:43:38.211967530+08:00" level=info msg="Daemon has completed initialization"
7月 14 09:43:38 master systemd[1]: Started Docker Application Container Engine.
7月 14 09:43:38 master dockerd[4837]: time="2021-07-14T09:43:38.253139040+08:00" level=info msg="API listen on /var/run/docker.sock"
daemon设置:
重启docker服务
设置k8s.conf
vim /etc/sysctl.d/k8s.conf
## 添加如下内容
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
## 执行命令生效
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
关闭防火墙
iptables设置:在终端执行以下命令清除防火墙规则并查看清除后的结果:iptables -F && iptables -X && iptables -Z && iptables -L && systemctl stop iptables && systemctl status iptables
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
关闭交换分区 在终端执行下面两条命令关闭交换分区:
关闭selinux
查看selinux状态:
关闭 SELINUX。 在终端执行下面两条命令关闭selinux分区:
master节点部署
获取镜像 获取calico和k8s镜像:
cr.loongnix.cn
calico:
docker pull cr.loongnix.cn/calico/node:3.18.0
docker pull cr.loongnix.cn/calico/cni:3.18.0
docker pull cr.loongnix.cn/calico/pod2daemon-flexvol:3.18.0
docker pull cr.loongnix.cn/calico/kube-controllers:3.18.0
k8s:
docker pull cr.loongnix.cn/kubernetes/kube-apiserver:v1.20.0
docker pull cr.loongnix.cn/kubernetes/kube-controller-manager:v1.20.0
docker pull cr.loongnix.cn/kubernetes/kube-scheduler:v1.20.0
docker pull cr.loongnix.cn/kubernetes/kube-proxy:v1.20.0
docker pull cr.loongnix.cn/kubernetes/coredns:1.7.0
docker pull cr.loongnix.cn/kubernetes/etcd:3.4.13-0
docker pull cr.loongnix.cn/kubernetes/pause:3.2
准备配置文件 需准备kubeadm和calico配置文件(注:如果有定制化需求,请自行修改kubeadm配置文件和calico配置文件)。 (1) kubeadm配置文件准备(master节点): 通过以下命令生成配置文件模板
kubeadm config print init-defaults > init_default.yaml
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 10.40.65.184
bindPort: 6443
nodeRegistration:
criSocket: /var/run/dockershim.sock
name: master
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: cr.loongnix.cn/kubernetes
kind: ClusterConfiguration
kubernetesVersion: v1.20.0
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
scheduler: {}
(2) 执行如下命令查看kubeadm配置后所需镜像版本 [root@master ~]# kubeadm config images list --config init_default.yaml cr.loongnix.cn/kubernetes/kube-apiserver:v1.20.0 cr.loongnix.cn/kubernetes/kube-controller-manager:v1.20.0 cr.loongnix.cn/kubernetes/kube-scheduler:v1.20.0 cr.loongnix.cn/kubernetes/kube-proxy:v1.20.0 cr.loongnix.cn/kubernetes/pause:3.2 cr.loongnix.cn/kubernetes/etcd:3.4.13-0 cr.loongnix.cn/kubernetes/coredns:1.7.0
calico配置文件准备: 通过以下命令获取官方calico配置文件
修改calico.yaml中对应配置,使配置文件中镜像名称与本地镜像一致vim calico.yaml
# It can be deleted if this is a fresh installation, or if you have already
# upgraded to use calico-ipam.
- name: upgrade-ipam
image: cr.loongnix.cn/calico/cni:3.18.0 (保持与私有仓库地址一致)
--
# This container installs the CNI binaries
# and CNI network config file on each node.
- name: install-cni
image: cr.loongnix.cn/calico/cni:3.18.0 (保持与私有仓库地址一致)
--
# Adds a Flex Volume Driver that creates a per-pod Unix Domain Socket to allow Dikastes
# to communicate with Felix over the Policy Sync API.
- name: flexvol-driver
image: cr.loongnix.cn/calico/pod2daemon-flexvol:3.18.0 (保持与私有仓库地址一致)
--
# container programs network policy and routes on each
# host.
- name: calico-node
image: cr.loongnix.cn/calico/node:3.18.0 (保持与私有仓库地址一致)
--
priorityClassName: system-cluster-critical
containers:
- name: calico-kube-controllers
image: cr.loongnix.cn/calico/kube-controllers:3.18.0 (保持与私有仓库地址一致)
初始化master,在终端执行命令,如下示:master执行
[root@master ~]# kubeadm init --config=init_default.yaml
[init] Using Kubernetes version: v1.20.0
[preflight] Running pre-flight checks
[WARNING FileExisting-tc]: tc not found in system path
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.3. Latest validated version: 19.03
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Using existing ca certificate authority
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master] and IPs [10.96.0.1 10.40.65.184]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master] and IPs [10.40.65.184 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master] and IPs [10.40.65.184 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 19.502802 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.20" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master as control-plane by adding the labels "node-role.kubernetes.io/master=''" and "node-role.kubernetes.io/control-plane='' (deprecated)"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: abcdef.0123456789abcdef
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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 10.40.65.184:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:90f1e6027bd14b08d75c90cc4f59ce82b4425c5cb9f3ff6294bc612063180254
初始化完成后在终端执行以下命令,拷贝对应的配置文件:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master01 NotReady master 16m v1.20.0
[root@master ~]# kubectl get pods --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-5944856bb9-kl6cf 0/1 Pending 0 3m56s <none> <none> <none> <none>
kube-system coredns-5944856bb9-ttn8v 0/1 Pending 0 3m56s <none> <none> <none> <none>
kube-system etcd-master 1/1 Running 0 3m54s 10.40.65.184 master <none> <none>
kube-system kube-apiserver-master 1/1 Running 0 3m54s 10.40.65.184 master <none> <none>
kube-system kube-controller-manager-master 1/1 Running 0 3m54s 10.40.65.184 master <none> <none>
kube-system kube-proxy-fbntc 1/1 Running 0 3m57s 10.40.65.184 master <none> <none>
kube-system kube-scheduler-master 1/1 Running 0 3m54s 10.40.65.184 master <none> <none>
node节点部署
在执行前拉取镜像:
docker pull cr.loongnix.cn/kubernetes/pause:3.2
docker pull cr.loongnix.cn/kubernetes/kube-proxy:v1.20.0
docker pull cr.loongnix.cn/calico/cni:3.18.0
docker pull cr.loongnix.cn/calico/pod2daemon-flexvol:3.18.0
docker pull cr.loongnix.cn/calico/node:3.18.0
docker pull cr.loongnix.cn/kubernetes/coredns:1.7.0
(1) 加入集群,在终端执行命令,如下示(注:以下token由3.2.1(1)生成):
kubeadm join 10.40.65.184:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:90f1e6027bd14b08d75c90cc4f59ce82b4425c5cb9f3ff6294bc612063180254
[root@node1 opt]# kubeadm join 10.40.65.184:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:90f1e6027bd14b08d75c90cc4f59ce82b4425c5cb9f3ff6294bc612063180254
[preflight] Running pre-flight checks
[WARNING FileExisting-tc]: tc not found in system path
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.3. Latest validated version: 19.03
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
kubeadm join 10.40.65.121:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:b2b4a5651f5dff7de9921726508b46e90a2a26c0dfb29605fc33431c0a7e0426
主节点验证是否加入:
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 139m v1.20.0
node1 NotReady <none> 73s v1.20.0
master上部署k8s网络组件calico操作
root@master ~]# kubectl create -f calico.yaml
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/kubecontrollersconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.apps/calico-node created
serviceaccount/calico-node created
deployment.apps/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
poddisruptionbudget.policy/calico-kube-controllers created
root@master ~]# kubectl get pods --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system calico-kube-controllers-5b974c9dc5-p5t98 1/1 Running 0 38s 192.168.219.65 master <none> <none>
kube-system calico-node-d9rk7 1/1 Running 0 37s 10.40.65.184 master <none> <none>
kube-system calico-node-xqpsk 0/1 Init:0/3 0 37s 10.40.65.107 node1 <none> <none>
kube-system coredns-5944856bb9-kl6cf 1/1 Running 0 140m 192.168.219.67 master <none> <none>
kube-system coredns-5944856bb9-ttn8v 1/1 Running 0 140m 192.168.219.66 master <none> <none>
kube-system etcd-master 1/1 Running 0 140m 10.40.65.184 master <none> <none>
kube-system kube-apiserver-master 1/1 Running 0 140m 10.40.65.184 master <none> <none>
kube-system kube-controller-manager-master 1/1 Running 0 140m 10.40.65.184 master <none> <none>
kube-system kube-proxy-fbntc 1/1 Running 0 140m 10.40.65.184 master <none> <none>
kube-system kube-proxy-gdfnp 0/1 ContainerCreating 0 2m31s 10.40.65.107 node1 <none> <none>
kube-system kube-scheduler-master 1/1 Running 0 140m 10.40.65.184 master <none> <none>
[root@master ~]# kubectl get pods --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system calico-kube-controllers-5b974c9dc5-p5t98 1/1 Running 0 151m 192.168.219.65 master <none> <none>
kube-system calico-node-d9rk7 1/1 Running 0 151m 10.40.65.184 master <none> <none>
kube-system calico-node-xqpsk 1/1 Running 0 151m 10.40.65.107 node1 <none> <none>
kube-system coredns-5944856bb9-kl6cf 1/1 Running 0 4h51m 192.168.219.67 master <none> <none>
kube-system coredns-5944856bb9-ttn8v 1/1 Running 0 4h51m 192.168.219.66 master <none> <none>
kube-system etcd-master 1/1 Running 0 4h51m 10.40.65.184 master <none> <none>
kube-system kube-apiserver-master 1/1 Running 0 4h51m 10.40.65.184 master <none> <none>
kube-system kube-controller-manager-master 1/1 Running 0 4h51m 10.40.65.184 master <none> <none>
kube-system kube-proxy-fbntc 1/1 Running 0 4h51m 10.40.65.184 master <none> <none>
kube-system kube-proxy-gdfnp 1/1 Running 0 153m 10.40.65.107 node1 <none> <none>
kube-system kube-scheduler-master 1/1 Running 0 4h51m 10.40.65.184 master <none> <none>
验证node上:
在Master上创建nginx pod
(1) 创建nginx.ymal文件,内容如下(可根据实际情况修改):
vim nginx.ymal
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: cr.loongnix.cn/library/nginx:1.19.9
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
[root@master pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-56b6b998d7-hs8ql 1/1 Running 0 23s
[root@master pod]# kubectl expose deployment nginx-app --port=88 --target-port=80 --type=NodePort
service/nginx-app exposed
[root@master pod]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h10m
nginx-app NodePort 10.101.106.108 <none> 88:30886/TCP 34s
[root@master pod]# curl 10.101.106.108:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>