人狠话不多,直接上命令。
###################################################
### OS Preparation, CentOS v7-2009
###################################################
yum install -y telnet vim net-tools open-vm-tools wget ntpdate yum-utils device-mapper-persistent-data lvm2
ntpdate time.windows.com
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
###################################################
### Prepare yum repos from aliyun.com
###################################################
cat >> /etc/yum.repos.d/docker-ce.repo <<EOF
[docker-repo]
name=Docker Repository
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7.9/x86_64/stable/
enabled=1
gpgcheck=0
EOF
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
EOF
yum clean all
yum makecache
########################################################################
### Install docker engine
########################################################################
yum install -y docker-ce docker-ce-cli containerd.io
systemctl enable docker
systemctl start docker
systemctl enable containerd
systemctl start containerd
tee /etc/docker/daemon.json <<EOF
{
"insecure-registries":["192.168.1.220:5000"],
"registry-mirrors":["https://zt68n01x.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl restart docker
#####################################################################
### Install kubelet, kubeadm, kubectl
#####################################################################
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet
#########################################################################################################
#####################################################################
### kubeadm init
#####################################################################
kubeadm init \
--image-repository=registry.aliyuncs.com/google_containers \
--kubernetes-version=v1.23.5 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
cat >> ~/.bash_profile << EOF
export KUBECONFIG=/etc/kubernetes/admin.conf
EOF
source ~/.bash_profile
######################################################################
### 安装完成后记录一下命令行回显 !!!
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.1.161:6443 --token i82ix2.2z87s31ilrmisfg1 \
--discovery-token-ca-cert-hash sha256:377961908d0efdf16e1dd81b0b916adc3acf02ba54d68bd784c822cd46a4838d
#######################################################################################################################
#####################################################################
### Install flannel network in master machine
### https://github.com/flannel-io/flannel
#####################################################################
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
### Then join the other node into k8s
kubeadm join 192.168.1.161:6443 --token i6feiw.fr7xwojmv9frvb5y \
--discovery-token-ca-cert-hash sha256:b2bbe11f6a6eb66ab71ee44dc74bdbbdc2c1a63f7281b366fda0661475be1d79
kubeadm join 192.168.1.161:6443 --token i6feiw.fr7xwojmv9frvb5y \
--discovery-token-ca-cert-hash sha256:b2bbe11f6a6eb66ab71ee44dc74bdbbdc2c1a63f7281b366fda0661475be1d79
#####################################################################
### Deploy kubernetes dashboard in master machine
### will pull image from docker.io, so expect to use mirror
### kubectl get pods --namespace=kubernetes-dashboard
### kubectl get service --namespace=kubernetes-dashboard
### mirror: registry.cn-hangzhou.aliyuncs.com/google_containers/dashboard:v2.5.1
#####################################################################
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml
# modify [type: ClusterIP] to NodePort
kubectl edit service kubernetes-dashboard --namespace=kubernetes-dashboard
如下:
spec:
clusterIP: 10.107.192.48
externalTrafficPolicy: Cluster
ports:
- nodePort: 30001
port: 443
protocol: TCP
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
sessionAffinity: None
type: NodePort #### this line。
#####################################################################
### Create dashboard admin-token on Master machine.
### kubectl create serviceaccount dashboard-admin -n kube-system
### kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
### kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
#####################################################################
cat >> /root/system/admin-token.yaml <<EOF
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: admin
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: admin
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin
namespace: kube-system
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
EOF
kubectl create -f admin-token.yaml
kubectl describe secret/$(kubectl get secret -n kube-system |grep admin|awk '{print $1}') -n kube-system
eyJhbGciOiJSUzI1NiIsImtpZCI6IjBJOFVzRkNJbzBQRnpZWkxCTmprd0Z3Zkk3TmNoZGtWZW9SNVctOGczU3MifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi10b2tlbi12Yno4aiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJhZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjBhNWFiZjJmLTE0ZjUtNDZjZi04OGNmLWU0M2M4ZDg5NjJhNiIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTphZG1pbiJ9.A2MIh4CKxxZaDctC35Ttl2N3qd1Kv8BbmPuJWLFn_-wwigJEmtztBELMEwAWqoyGsGYpL8LdM6V6E31pnhobALo7NvKOFhf-0o5v4NhsRf5egzefMdtSR01aEHVZX3fePFEV4kPB0OeYC2uzBONUvL5en08QiWHrGaf4cCG9RJZyOtFjqqWue7kdxgSXQRdCHoT7jBU3dXz0HVuQpynhmNgM3tWt5HjI9LLIUuvmUa1WbMFr-Zq1V47yYW_QRnha3_faLC3sqGZa98TyX0TukwaUAZanCl4amQOgThLmOA_NXTeQNEYQnwM1p3pUwPVkuz9jWVO-i9WoadKLKIYobQ
########################################################################################################
kubectl scale -n nginx3 deployment nginx3 --replicas=3
#####################################################################
### Deploy nginx ingress
#####################################################################
kubectl create deployment nginx1 --image=nginx:1.14.2
kubectl expose deployment nginx1 --port=80 --target-port=80 --type=NodePort
kubectl create namespace nginx3
kubectl create deployment nginx3 --namespace=nginx3 --image=nginx:1.14.2
kubectl expose deployment nginx3 --namespace=nginx3 --port=80 --target-port=80 --type=NodePort
kubectl create deployment nginx --image=tomcat
kubectl expose deployment tomcat --port=8080 --target-port=8080 --type=NodePort
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.3/deploy/static/provider/baremetal/deploy.yaml
有三个地址要更换
registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.3
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
####################################################################
### ingress rule 中需要补充一个ingressClassName,大概是从1.19开始的
### 后续使用 ingress-controller的nodeport端口进行访问
####################################################################
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: k8s-ingress
spec:
ingressClassName: nginx
rules:
- host: etc.k8s.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80
- host: test.k8s.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx2
port:
number: 80
############################################################
kubectl create secret tls rankez-secret --cert=rankez.crt --key=rankez.key
############################################################################################
### PV, PVC
#############
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0001
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: "/data/test"
server: 192.168.1.170
apiVersion: v1
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
annotations:
volume.beta.kubernetes.io/storage-class: "nfs" #此处必须beta
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1000Mi
apiVersion: v1
kind: Pod
metadata:
name: nfs-test
spec:
containers:
- name: my-busybox
image: 192.168.1.220:5000/busybox
volumeMounts:
- mountPath: "/data"
name: sample-volume
command: ["sleep", "10000000"]
imagePullPolicy: IfNotPresent
volumes:
- name: sample-volume
persistentVolumeClaim:
claimName: test-claim
############################################################################################
Retain 之后删除PV中的spec.claimRef段落。可以恢复继续使用。所以不能轻易删除PVC。更不能删除PVC/PV
需要 vi /etc/kubernetes/manifests/kube-apiserver.yaml
添加一行,但是无需重启,apiserver会自动重启。如果不行,则delete pod
--feature-gates=RemoveSelfLink=false
权限,provisioner,pvc,pod
############################################################################################
### ConfigMap
################################################################################################
kubectl create configmap my-config --from-literal=firstname=shengping
##################################################################################