install docker
https://docs.docker.com/engine/install/
新增自動補全
yum -y install bash-completion
source /usr/share/bash-completion/bash_completion
install and configuration harbor
[root@Rocky-Harbor certs]# mkdir -pv /k8s/softwares/certs/{ca,server,client}
[root@Rocky-Harbor certs]# tar -zxvf harbor-offline-installer-v2.9.3.tgz
[root@Rocky-Harbor certs]# openssl genrsa -out ca/ca.key 4096
[root@Rocky-Harbor certs]# openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=foam.com" -key ca/ca.key -out ca/ca.crt
[root@Rocky-Harbor certs]# openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=foam.com" -key ca/ca.key -out ca/ca.crt
[root@Rocky-Harbor certs]# openssl genrsa -out server/harbor.foam.com.key 4096
[root@Rocky-Harbor certs]# openssl req -sha512 -new -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=harbor.foam.com" -key server/harbor.foam.com.key -out server/harbor.foam.com.csr
[root@Rocky-Harbor certs]# cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1=foam.com
DNS.2=foam
DNS.3=harbor.foam.com
EOF
[root@Rocky-Harbor certs]# openssl x509 -req -sha512 -days 3650 -extfile v3.ext -CA ca/ca.crt -CAkey ca/ca.key -CAcreateserial -in server/harbor.foam.com.csr -out server/harbor.foam.com.crt
[root@Rocky-Harbor certs]# openssl x509 -inform PEM -in server/harbor.foam.com.crt -out server/harbor.foam.com.cert
[root@Rocky-Harbor certs]# mkdir -pv /etc/docker/certs.d/harbor.foam.com
[root@Rocky-Harbor certs]# cp -r /k8s/softwares/certs/client/* /etc/docker/certs.d/harbor.foam.com/
[root@Rocky-Harbor certs]# echo ip harbor.foam.com >> /etc/hosts
[root@Rocky-Harbor harbor]# echo alias yy=\'egrep -v \"\^.*#\|\^\$\"\' >> /root/.bashrc
[root@Rocky-Harbor harbor]# source /root/.bashrc
# harbor configuration
hostname: harbor.foam.com
http:
port: 80
https:
port: 443
certificate: /k8s/softwares/certs/server/harbor.foam.com.crt
private_key: /k8s/softwares/certs/server/harbor.foam.com.key
harbor_admin_password: foam
[root@Rocky-Harbor harbor]# ./install
[root@Rocky-Harbor harbor]# docker login -u admin -p foam harbor.foam.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@Rocky-Harbor harbor]# docker logout harbor.foam.com
[root@Rocky-Harbor harbor]# scp /k8s/softwares/certs/client/* 192.168.100.15:/etc/docker/certs.d/harbor.foam.com
[root@Rocky-Harbor harbor]# scp /k8s/softwares/certs/client/* 192.168.100.7:/etc/docker/certs.d/harbor.foam.com
[root@Rocky-Harbor harbor]# scp /k8s/softwares/certs/client/* 192.168.100.8:/etc/docker/certs.d/harbor.foam.com
install kubernetes
# All Node and install docker
[root@Rocky-kubernetes-control ~]# swapoff -a && sysctl -w vm.swappiness=0
[root@Rocky-kubernetes-control ~]# sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
[root@Rocky-kubernetes-control ~]# ifconfig eth0 | grep ether | awk '{print $2}'
[root@Rocky-kubernetes-control ~]# cat > /etc/modules-load.d/k8s.conf << EOF
br_netfilter
EOF
[root@Rocky-kubernetes-control ~]# cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
[root@Rocky-kubernetes-control ~]# sysctl --system
[root@Rocky-kubernetes-control ~]# mkdir -pv /etc/docker/certs.d/harbor.foam.com
[root@Rocky-kubernetes-control ~]# systemctl enable --now docker
[root@Rocky-kubernetes-control ~]# sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
[root@Rocky-kubernetes-control ~]# grep ^SELINUX= /etc/selinux/config
[root@Rocky-kubernetes-control ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.15 kubernetes-control
192.168.100.7 kubernetes-node1
192.168.100.8 kubernetes-node2
192.168.100.25 harbor.foam.com
[root@Rocky-kubernetes-control ~]# docker login -u admin -p foam harbor.foam.com
[root@Rocky-kubernetes-control ~]# docker logout harbor.foam.com
# All Node install kubeadm kubelet kubectl
[root@Rocky-kubernetes-control ~]# 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
EOF
# View kubeadm version
[root@Rocky-kubernetes-control ~]# yum -y list kubeadm --showduplicates | sort -r
# install kubeadm-1.23.17-0
[root@Rocky-kubernetes-control ~]# yum -y install kubeadm-1.23.17-0 kubelet-1.23.17-0 kubectl-1.23.17-0
[root@Rocky-kubernetes-control ~]# kubeadm init --kubernetes-version=v1.23.17 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.100.0.0/16 --service-cidr=10.200.0.0/16 --service-dns-domain=foam.com
相關引數說明:
--kubernetes-version:
指定K8S master元件的版本號。
--image-repository:
指定下載k8s master元件的映象倉庫地址。
--pod-network-cidr:
指定Pod的網段地址。
--service-cidr:
指定SVC的網段
--service-dns-domain:
指定service的域名。若不指定,預設為"cluster.local"
# 輸出資訊
[init] Using Kubernetes version: v1.23.17
[preflight] Running pre-flight checks
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 25.0.3. Latest validated version: 20.10
[WARNING Hostname]: hostname "rocky-kubernetes-control" could not be reached
[WARNING Hostname]: hostname "rocky-kubernetes-control": lookup rocky-kubernetes-control on 192.168.100.2:53: no such host
[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
[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] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.foam.com rocky-kubernetes-control] and IPs [10.200.0.1 192.168.100.15]
[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 rocky-kubernetes-control] and IPs [192.168.100.15 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost rocky-kubernetes-control] and IPs [192.168.100.15 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
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 66.519758 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.23" in namespace kube-system with the configuration for the kubelets in the cluster
NOTE: The "kubelet-config-1.23" naming of the kubelet ConfigMap is deprecated. Once the UnversionedKubeletConfigMap feature gate graduates to Beta the default name will become just "kubelet-config". Kubeadm upgrade will handle this transition transparently.
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node rocky-kubernetes-control as control-plane by adding the labels: [node-role.kubernetes.io/master(deprecated) node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node rocky-kubernetes-control as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: bin8u5.8y2qs46aloky5x8h
[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 192.168.100.15:6443 --token bin8u5.8y2qs46aloky5x8h \
--discovery-token-ca-cert-hash sha256:061796f7364225699fc031e8df29a0659e18c716a5209a331efa3ae7b7c91b65
使用 kubeadm 初始化叢集時,可能會出現如下的輸出資訊
[init]
使用初始化的K8S版本。
[preflight]
主要是做安裝K8S叢集的前置工作,比如下載映象,這個時間取決於你的網速。
[certs]
生成證書檔案,預設儲存在"/etc/kubernetes/pki"目錄喲。
[kubeconfig]
生成K8S叢集的預設配置檔案,預設儲存在"/etc/kubernetes"目錄喲。
[kubelet-start]
啟動kubelet,
環境變數預設寫入:"/var/lib/kubelet/kubeadm-flags.env"
配置檔案預設寫入:"/var/lib/kubelet/config.yaml"
[control-plane]
使用靜態的目錄,預設的資源清單存放在:"/etc/kubernetes/manifests"。
此過程會建立靜態Pod,包括"kube-apiserver","kube-controller-manager"和"kube-scheduler"
[etcd]
建立etcd的靜態Pod,預設的資源清單存放在:""/etc/kubernetes/manifests"
[wait-control-plane]
等待kubelet從資源清單目錄"/etc/kubernetes/manifests"啟動靜態Pod。
[apiclient]
等待所有的master元件正常執行。
[upload-config]
建立名為"kubeadm-config"的ConfigMap在"kube-system"名稱空間中。
[kubelet]
建立名為"kubelet-config-1.22"的ConfigMap在"kube-system"名稱空間中,其中包含叢集中kubelet的配置
[upload-certs]
跳過此節點,詳情請參考”--upload-certs"
[mark-control-plane]
標記控制皮膚,包括打標籤和汙點,目的是為了標記master節點。
[bootstrap-token]
建立token口令,例如:"kbkgsa.fc97518diw8bdqid"。
如下圖所示,這個口令將來在加入叢集節點時很有用,而且對於RBAC控制也很有用處喲。
[kubelet-finalize]
更新kubelet的證書檔案資訊
[addons]
新增附加元件,例如:"CoreDNS"和"kube-proxy”
Configure all worker nodes to join the k8s cluster
[root@Rocky-kubernetes-node1 ~]# kubeadm join 192.168.100.15:6443 --token bin8u5.8y2qs46aloky5x8h \
--discovery-token-ca-cert-hash sha256:061796f7364225699fc031e8df29a0659e18c716a5209a331efa3ae7b7c91b65
[preflight] Running pre-flight checks
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 25.0.3. Latest validated version: 20.10
[WARNING Hostname]: hostname "rocky-kubernetes-node1" could not be reached
[WARNING Hostname]: hostname "rocky-kubernetes-node1": lookup rocky-kubernetes-node1 on 192.168.100.2:53: no such host
[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
[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.
# View existing nodes
[root@Rocky-kubernetes-control ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
rocky-kubernetes-control NotReady control-plane,master 11m v1.23.17
rocky-kubernetes-node1 NotReady <none> 104s v1.23.17
rocky-kubernetes-node2 NotReady <none> 104s v1.23.17
[root@Rocky-kubernetes-control ~]# kubectl apply -f kube-flannel.yml
[root@Rocky-kubernetes-control ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
rocky-kubernetes-control Ready control-plane,master 21m v1.23.17
rocky-kubernetes-node1 Ready <none> 12m v1.23.17
rocky-kubernetes-node2 Ready <none> 12m v1.23.17
# Check if the components are functioning properly
[root@Rocky-kubernetes-control ~]# kubectl get pods -A -o wide| grep kube-flannel
kube-flannel kube-flannel-ds-6vd59 1/1 Running 0 5m5s 192.168.100.7 rocky-kubernetes-node1 <none> <none>
kube-flannel kube-flannel-ds-c5fdn 1/1 Running 0 5m5s 192.168.100.8 rocky-kubernetes-node2 <none> <none>
kube-flannel kube-flannel-ds-dqjgv 1/1 Running 0 5m5s 192.168.100.15 rocky-kubernetes-control <none> <none>
測試網路連通性
[root@Rocky-kubernetes-control ~]# cat > foam-linux85-ds.yaml << EOF
kind: DaemonSet
apiVersion: apps/v1
metadata:
name: linux85-ds
spec:
selector:
matchLabels:
school: foam
class: linux85
template:
metadata:
labels:
school: foam
class: linux85
spec:
containers:
- image: alpine
stdin: true
name: mylinux
EOF
[root@Rocky-kubernetes-control ~]# kubectl apply -f oldboyedu-linux85-ds.yaml
daemonset.apps/linux85-ds created
[root@Rocky-kubernetes-control ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
linux85-ds-bchtf 1/1 Running 0 11s 10.100.2.3 rocky-kubernetes-node2 <none> <none>
linux85-ds-sz6mr 1/1 Running 0 57s 10.100.1.2 rocky-kubernetes-node1 <none> <none>
[root@Rocky-kubernetes-control ~]# kubectl exec linux85-ds-sz6mr -- ping -c 3 10.100.2.3
PING 10.100.2.3 (10.100.2.3): 56 data bytes
64 bytes from 10.100.2.3: seq=0 ttl=62 time=1.937 ms
64 bytes from 10.100.2.3: seq=1 ttl=62 time=1.310 ms
64 bytes from 10.100.2.3: seq=2 ttl=62 time=0.772 ms
--- 10.100.2.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.772/1.339/1.937 ms
[root@Rocky-kubernetes-control ~]# kubectl delete -f foam-linux85-ds.yaml
kubectl TAB
[root@Rocky-kubernetes-control ~]# yum install bash-completion
[root@Rocky-kubernetes-control ~]# source /usr/share/bash-completion/bash_completion
[root@Rocky-kubernetes-control ~]# type _init_completion
[root@Rocky-kubernetes-control ~]# kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
[root@Rocky-kubernetes-control ~]# sudo chmod a+r /etc/bash_completion.d/kubectl
[root@Rocky-kubernetes-control ~]# echo 'alias k=kubectl' >>~/.bashrc
[root@Rocky-kubernetes-control ~]# echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
[root@Rocky-kubernetes-control ~]# source ~/.bashrc
Kube-flannel file
---
kind: Namespace
apiVersion: v1
metadata:
name: kube-flannel
labels:
k8s-app: flannel
pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: flannel
name: flannel
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
- apiGroups:
- networking.k8s.io
resources:
- clustercidrs
verbs:
- list
- watch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: flannel
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: flannel
name: flannel
namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-flannel
labels:
tier: node
k8s-app: flannel
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-flannel
labels:
tier: node
app: flannel
k8s-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-plugin
image: docker.io/flannel/flannel-cni-plugin:v1.4.0-flannel1
command:
- cp
args:
- -f
- /flannel
- /opt/cni/bin/flannel
volumeMounts:
- name: cni-plugin
mountPath: /opt/cni/bin
- name: install-cni
image: docker.io/flannel/flannel:v0.24.3
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: docker.io/flannel/flannel:v0.24.3
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
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
- name: EVENT_QUEUE_DEPTH
value: "5000"
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
- name: xtables-lock
mountPath: /run/xtables.lock
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni-plugin
hostPath:
path: /opt/cni/bin
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
- name: xtables-lock
hostPath:
path: /run/xtables.lock
type: FileOrCreate