harbor+k8s deploy
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