※ 各章節(jié)重要知識點
k8s學(xué)習(xí) — 各章節(jié)重要知識點
1 搭建方案
1.1 minikube【推薦學(xué)習(xí)時使用】
推薦學(xué)習(xí)時使用,輕量化的k8s集群,可以在個人電腦上使用。
minikube 是一個工具, 能讓你在本地運行 Kubernetes。 minikube 在你的個人計算機(jī)(包括 Windows、macOS 和 Linux PC)上運行一個一體化(all-in-one)或多節(jié)點的本地 Kubernetes 集群,以便你來嘗試 Kubernetes 或者開展每天的開發(fā)工作。
官方安裝文檔
1.2 kubeadm【推薦使用】
你可以使用 kubeadm 工具來創(chuàng)建和管理 Kubernetes 集群。 該工具能夠執(zhí)行必要的動作并用一種用戶友好的方式啟動一個可用的、安全的集群。
安裝 kubeadm 展示了如何安裝 kubeadm 的過程。一旦安裝了 kubeadm, 你就可以使用它來創(chuàng)建一個集群。
服務(wù)器要求:3 臺服務(wù)器(虛擬機(jī))
- k8s-master:192.168.3.241
- k8s-node1:192.168.3.242
- k8s-node2:192.168.3.243
最低配置:2核、2G內(nèi)存、20G硬盤,最好能聯(lián)網(wǎng),不能聯(lián)網(wǎng)的話需要有提供對應(yīng)鏡像的私有倉庫。
軟件環(huán)境:
- 操作系統(tǒng):CentOS 7
- Docker:20+
- k8s:1.23.6
安裝步驟
1.2.0 提示(步驟調(diào)整)
這里將1.2.3 中 安裝的Docker的步驟先放到了1.2.1 安裝Docker(所有節(jié)點),這個不影響整體的步驟。
1.2.1 安裝Docker(所有節(jié)點)
安裝 Docker:Centos7安裝docker
1.2.2 初始操作(所有節(jié)點)
# 關(guān)閉防火墻
systemctl stop firewalld
# 禁用防火墻
systemctl disable firewalld
# 關(guān)閉selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
# 或
setenforce 0 # 臨時
# 關(guān)閉swap
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 或
swapoff -a # 臨時
# 關(guān)閉完swap后,一定要重啟一下虛擬機(jī)!?。?/span>
# 根據(jù)規(guī)劃重新設(shè)置各節(jié)點的主機(jī)名,設(shè)置完后記得重啟虛擬機(jī)!??!
hostnamectl set-hostname <hostname>(k8s-master、k8s-node1、k8s-node2)
# 在各個節(jié)點的hosts加入配置后的主機(jī)名
cat >> /etc/hosts << EOF
192.168.3.241 k8s-master
192.168.3.242 k8s-node1
192.168.3.243 k8s-node2
EOF
# 將橋接的IPv4流量傳遞到iptables的鏈
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
# 時間同步
yum install ntpdate -y
ntpdate time.windows.com
1.2.3 安裝基礎(chǔ)軟件 kubeadm、kubelet、kubectl(所有節(jié)點)
- 添加阿里云 yum 源
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
- 安裝 kubeadm、kubelet、kubectl
yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
systemctl enable kubelet
配置關(guān)閉 Docker 的 cgroups,修改 /etc/docker/daemon.json:
# 加入以下內(nèi)容
"exec-opts": ["native.cgroupdriver=systemd"]
完整配置:
{
"registry-mirrors": ["https://almtd3fa.mirror.aliyuncs.com","http://hub-mirror.c.163.com","http://mirrors.ustc.edu.cn"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
# 使配置生效
systemctl daemon-reload
# 重啟docker
systemctl restart docker
1.2.4 部署 Kubernetes Master(在 Master 節(jié)點下執(zhí)行)
kubeadm init \
--apiserver-advertise-address=192.168.3.241 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.6 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
可能耗時有一點長,看網(wǎng)絡(luò)情況,如下顯示則安裝成功:
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.3.241:6443 --token f9v1xg.4vqoarsfo0zfrhge \
--discovery-token-ca-cert-hash sha256:54795fb6e9ee1ae628549bf9ae2fad9245aa5b25f62dd3e6af8995cdc4e5b69f
注意最后有生成token的命令,這個在下一步會用到。
# 安裝成功后,復(fù)制如下配置并執(zhí)行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 查看節(jié)點情況
kubectl get nodes
# 顯示結(jié)果
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady control-plane,master 12m v1.23.6
1.2.5 加入 Kubernetes Node(分別在 k8s-node1 和 k8s-node2 執(zhí)行)
# 下方命令可以在 k8s master 控制臺初始化成功后復(fù)制 join 命令
kubeadm join 192.168.3.241:6443 --token f9v1xg.4vqoarsfo0zfrhge \
--discovery-token-ca-cert-hash sha256:54795fb6e9ee1ae628549bf9ae2fad9245aa5b25f62dd3e6af8995cdc4e5b69f
# 結(jié)果:
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.
在master
節(jié)點查看集群情況:
kubectl get nodes
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master NotReady control-plane,master 51m v1.23.6
k8s-node1 NotReady <none> 48m v1.23.6
k8s-node2 NotReady <none> 48m v1.23.6
重新生成token:
# 如果初始化的 token 不小心清空了,可以通過如下命令獲取或者重新申請
# 如果 token 已經(jīng)過期,就重新申請
kubeadm token create
# token 沒有過期可以通過如下命令獲取
kubeadm token list
# 獲取 --discovery-token-ca-cert-hash 值,得到值后需要在前面拼接上 sha256:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
1.2.6 部署 CNI 網(wǎng)絡(luò)插件
1.2.6.1 下載相關(guān)鏡像(所有節(jié)點)
- 創(chuàng)建相關(guān)目錄
# 在/opt/下創(chuàng)建k8s,即:/opt/k8s/
-
(可跳過)選擇 calico 配置文件(calico.yaml)下載地址
地址1:https://docs.projectcalico.org/manifests/calico.yaml可在瀏覽器上訪問地址1,如果地址1無法訪問,訪問:https://docs.projectcalico.org,會發(fā)現(xiàn)地址會跳轉(zhuǎn)到
https://docs.tigera.io,然后點擊Calico Open Source
,進(jìn)入:圖2所示頁面,繼續(xù)點擊 All versions 可以查看Calico的版本情況(圖3),根據(jù)需要選擇對應(yīng)版本。
地址2:https://docs.tigera.io/archive/{version}
/manifests/calico.yaml,注意要把{version}
替換為對應(yīng)的版本,例如:https://docs.projectcalico.org/v3.25/manifests/calico.yaml、https://docs.tigera.io/archive/v3.24/manifests/calico.yaml
- 下載 calico 配置文件(calico.yaml)
# 在/opt/k8s/路徑下面下載 calico 配置文件(calico.yaml),可能會網(wǎng)絡(luò)超時
wget https://docs.projectcalico.org/v3.25/manifests/calico.yaml --no-check-certificate
- (可跳過)修改下載的 calico.yaml
# 修改下載的 calico.yaml 文件中的 CALICO_IPV4POOL_CIDR 配置。
# 如果 CALICO_IPV4POOL_CIDR 是注釋掉的,也就是沒有啟用的,可以不修改。
# 如果要啟用,先把前面的注釋去掉,然后修改為與初始化時,--pod-network-cidr=10.244.0.0/16中的相同,即改為:
- name: CALICO_IPV4POOL_CIDR
value: "10.244.0.0/16"
# 修改 IP_AUTODETECTION_METHOD 下的網(wǎng)卡名稱,沒有該配置則不用修改
- 去除下載地址的
docker.io/
前綴
# 查看有docker.io/前綴的地址
grep image calico.yaml
[root@k8s-master k8s]# grep image calico.yaml
image: docker.io/calico/cni:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/cni:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/node:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/node:v3.25.0
imagePullPolicy: IfNotPresent
image: docker.io/calico/kube-controllers:v3.25.0
imagePullPolicy: IfNotPresent
# 刪除鏡像 docker.io/ 前綴,避免下載過慢導(dǎo)致失敗,命令如下:
sed -i 's#docker.io/##g' calico.yaml
- 在master、node1、node2節(jié)點下載好相關(guān)鏡像,以免影響構(gòu)建進(jìn)度或?qū)е聵?gòu)建失敗
# 如果構(gòu)建時下載鏡像比較慢,可使用docker單獨下載鏡像后重新構(gòu)建
docker pull calico/cni:v3.25.0
docker pull calico/node:v3.25.0
docker pull calico/kube-controllers:v3.25.0
1.2.6.2 根據(jù) calico.yaml 構(gòu)建應(yīng)用(在 master 節(jié)點上執(zhí)行)
# 在/opt/k8s/執(zhí)行構(gòu)建命令
kubectl apply -f calico.yaml
# 查看應(yīng)用信息
kubectl get po -n kube-system
[root@k8s-master k8s]# kubectl get po -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-cd8566cf-sn2fz 1/1 Running 0 3m37s
calico-node-7h4nl 1/1 Running 0 3m37s
calico-node-b5wkj 1/1 Running 0 3m37s
calico-node-kpmr8 1/1 Running 0 3m37s
coredns-6d8c4cb4d-rr77x 1/1 Running 0 26m
coredns-6d8c4cb4d-wt8pf 1/1 Running 0 26m
etcd-k8s-master 1/1 Running 1 (20m ago) 26m
kube-apiserver-k8s-master 1/1 Running 1 (20m ago) 26m
kube-controller-manager-k8s-master 1/1 Running 1 (20m ago) 26m
kube-proxy-msxz4 1/1 Running 2 (5m16s ago) 21m
kube-proxy-w96nz 1/1 Running 1 (20m ago) 26m
kube-proxy-zq98q 1/1 Running 2 (5m10s ago) 21m
kube-scheduler-k8s-master 1/1 Running 1 (20m ago) 26m
# 查看某個Pod的運行情況
kubectl describe po calico-node-7h4nl(Pod名稱) -n kube-system
# 查看集群信息
kubectl get nodes
[root@k8s-master k8s]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 27m v1.23.6
k8s-node1 Ready <none> 22m v1.23.6
k8s-node2 Ready <none> 21m v1.23.6
1.2.7 測試 kubernetes 集群(master節(jié)點)
# 部署nginx
kubectl create deployment nginx --image=nginx
[root@k8s-master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
# 暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
[root@k8s-master ~]# kubectl expose deployment nginx --port=80 --type=NodePort
service/nginx exposed
# 查看 pod 以及服務(wù)信息
kubectl get pod,svc
[root@k8s-master k8s]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-85b98978db-p5r5z 0/1 ContainerCreating 0 15s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29m
service/nginx NodePort 10.111.11.157 <none> 80:31906/TCP 8s
這里可以發(fā)現(xiàn)pod/nginx-85b98978db-p5r5z
的狀態(tài)是容器創(chuàng)建中,可通過如下命令進(jìn)一步查看原因(鏡像拉取中
):
kubectl describe pod nginx-85b98978db-p5r5z
# 在最下面的事件中,可以看到是在拉取nginx鏡像中
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 63s default-scheduler Successfully assigned default/nginx-85b98978db-p5r5z to k8s-node2
Normal Pulling 63s kubelet Pulling image "nginx"
可以稍等一會兒查看 pod 以及服務(wù)信息,因為k8s會有相關(guān)機(jī)制保證服務(wù)的運行,所以不用再重新下載nginx鏡像部署:
# 查看 pod 以及服務(wù)信息
kubectl get pod,svc
[root@k8s-master k8s]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-85b98978db-p5r5z 1/1 Running 0 29m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 58m
service/nginx NodePort 10.111.11.157 <none> 80:31906/TCP 29m
訪問nginx服務(wù)時,注意對外的端口不是80,而是31906,完整地址:192.168.3.241:31906,通過node1、node2的IP加端口也可以訪問到。
1.3 二進(jìn)制安裝
利用 k8s 官方 github 倉庫下載二進(jìn)制包安裝,安裝過程較復(fù)雜,但相對較為穩(wěn)定,推薦生產(chǎn)環(huán)境使用。
1.4 命令行工具
2 命令行工具 kubectl 相關(guān)命令
Kubernetes 提供 kubectl 是使用 Kubernetes API 與 Kubernetes 集群的 控制面 進(jìn)行通信的命令行工具。
這個工具叫做 kubectl。更多官方命令
2.1 在任意節(jié)點使用 kubectl
- 在master節(jié)點進(jìn)行拷貝操作
# 將 master 節(jié)點中 /etc/kubernetes/admin.conf 拷貝到需要運行的服務(wù)器的 /etc/kubernetes 目錄中
# 拷貝到k8s-node1
scp /etc/kubernetes/admin.conf root@k8s-node1:/etc/kubernetes
# 拷貝到k8s-node2
scp /etc/kubernetes/admin.conf root@k8s-node2:/etc/kubernetes
- 在node1、node2節(jié)點配置環(huán)境變量
# 在對應(yīng)的服務(wù)器上配置環(huán)境變量
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
# 刷新配置
source ~/.bash_profile
- node1節(jié)點完整操作
[root@k8s-node1 ~]# kubectl get pod,svc
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[root@k8s-node1 ~]# echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
[root@k8s-node1 ~]# source ~/.bash_profile
[root@k8s-node1 ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-85b98978db-gvkcv 1/1 Running 2 (8h ago) 11h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11h
service/nginx NodePort 10.104.72.59 <none> 80:32708/TCP 11h
2.2 資源操作 相關(guān)命令
命令一般都是 kubectl [動作 create delete] [資源 pod services deploy] [名稱 nginx mysql]
2.2.1 創(chuàng)建對象
- 創(chuàng)建資源
kubectl create -f ./my-manifest.yaml # 創(chuàng)建資源
kubectl create -f ./my1.yaml -f ./my2.yaml # 使用多個文件創(chuàng)建資源
kubectl create -f ./dir # 使用目錄下的所有清單文件來創(chuàng)建資源
kubectl create -f https://git.io/vPieo # 使用 url 來創(chuàng)建資源
下面的方式可用,但不推薦(書寫不方便):
# 從 stdin 輸入中創(chuàng)建多個 YAML 對象
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: busybox-sleep
spec:
containers:
- name: busybox
image: busybox
args:
- sleep
- "1000000"
---
apiVersion: v1
kind: Pod
metadata:
name: busybox-sleep-less
spec:
containers:
- name: busybox
image: busybox
args:
- sleep
- "1000"
EOF
# 創(chuàng)建包含幾個 key 的 Secret
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: $(echo "s33msi4" | base64)
username: $(echo "jane" | base64)
EOF
- 啟動資源實例
kubectl run nginx --image=nginx # 啟動一個 nginx 實例
- 獲取 pod 和 svc 的文檔
kubectl explain pods,svc # 獲取 pod 和 svc 的文檔
2.2.2 顯示和查找資源
# Get commands with basic output
kubectl get services # 列出所有 namespace 中的所有 service
kubectl get pods --all-namespaces # 列出所有 namespace 中的所有 pod
kubectl get pods -o wide # 列出所有 pod 并顯示詳細(xì)信息
kubectl get pods --include-uninitialized # 列出該 namespace 中的所有 pod 包括未初始化的
kubectl get deployment my-dep # 列出指定 deployment
# 查看所有的命名空間
kubectl get ns(namespace)
# 查看部署的應(yīng)用
kubectl get deploy
# 查看部署的nginx應(yīng)用
kubectl get deploy nginx
# 查看部署的nginx應(yīng)用的配置文件 yaml格式
kubectl get deploy nginx -o yaml
# 查看部署的服務(wù)
kubectl get services
# 使用詳細(xì)輸出來描述命令
kubectl describe nodes my-node
kubectl describe pods my-pod
kubectl get services --sort-by=.metadata.name # List Services Sorted by Name
# 根據(jù)重啟次數(shù)排序列出 pod
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
# 獲取所有具有 app=cassandra 的 pod 中的 version 標(biāo)簽
kubectl get pods --selector=app=cassandra rc -o \
jsonpath='{.items[*].metadata.labels.version}'
# 獲取所有節(jié)點的 ExternalIP
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# 列出屬于某個 PC 的 Pod 的名字
# “jq”命令用于轉(zhuǎn)換復(fù)雜的 jsonpath,參考 https://stedolan.github.io/jq/
sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?}
echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})
# 查看哪些節(jié)點已就緒
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
&& kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
# 列出當(dāng)前 Pod 中使用的 Secret
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq
2.2.3 更新資源
kubectl rolling-update frontend-v1 -f frontend-v2.json # 滾動更新 pod frontend-v1
kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 # 更新資源名稱并更新鏡像
kubectl rolling-update frontend --image=image:v2 # 更新 frontend pod 中的鏡像
kubectl rolling-update frontend-v1 frontend-v2 --rollback # 退出已存在的進(jìn)行中的滾動更新
cat pod.json | kubectl replace -f - # 基于 stdin 輸入的 JSON 替換 pod
# 強(qiáng)制替換,刪除后重新創(chuàng)建資源。會導(dǎo)致服務(wù)中斷。
kubectl replace --force -f ./pod.json
# 為 nginx RC 創(chuàng)建服務(wù),啟用本地 80 端口連接到容器上的 8000 端口
kubectl expose rc nginx --port=80 --target-port=8000
# 更新單容器 pod 的鏡像版本(tag)到 v4
kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
kubectl label pods my-pod new-label=awesome # 添加標(biāo)簽
kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # 添加注解
kubectl autoscale deployment foo --min=2 --max=10 # 自動擴(kuò)展 deployment “foo”
2.2.4 修補資源
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' # 部分更新節(jié)點
# 更新容器鏡像; spec.containers[*].name 是必須的,因為這是合并的關(guān)鍵字
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# 使用具有位置數(shù)組的 json 補丁更新容器鏡像
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
# 使用具有位置數(shù)組的 json 補丁禁用 deployment 的 livenessProbe
kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
2.2.5 編輯資源
kubectl edit svc/docker-registry # 編輯名為 docker-registry 的 service
KUBE_EDITOR="nano" kubectl edit svc/docker-registry # 使用其它編輯器
2.2.6 scale 資源
kubectl scale --replicas=3 rs/foo # Scale a replicaset named 'foo' to 3
kubectl scale --replicas=3 -f foo.yaml # Scale a resource specified in "foo.yaml" to 3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # If the deployment named mysql's current size is 2, scale mysql to 3
kubectl scale --replicas=5 rc/foo rc/bar rc/baz # Scale multiple replication controllers
2.2.7 刪除資源
kubectl delete -f ./pod.json # 刪除 pod.json 文件中定義的類型和名稱的 pod
kubectl delete pod,service baz foo # 刪除名為“baz”的 pod 和名為“foo”的 service
kubectl delete pods,services -l name=myLabel # 刪除具有 name=myLabel 標(biāo)簽的 pod 和 serivce
kubectl delete pods,services -l name=myLabel --include-uninitialized # 刪除具有 name=myLabel 標(biāo)簽的 pod 和 service,包括尚未初始化的
kubectl delete deploy nginx # 刪除部署的nginx應(yīng)用
kubectl delete svc nginx # 刪除部署的nginx服務(wù)
kubectl -n my-ns delete po,svc --all # 刪除 my-ns namespace 下的所有 pod 和 serivce,包括尚未初始化的
2.3 Pod 與集群 相關(guān)命令
2.3.1 與運行的 Pod 交互
kubectl logs my-pod # dump 輸出 pod 的日志(stdout)
kubectl logs my-pod -c my-container # dump 輸出 pod 中容器的日志(stdout,pod 中有多個容器的情況下使用)
kubectl logs -f my-pod # 流式輸出 pod 的日志(stdout)
kubectl logs -f my-pod -c my-container # 流式輸出 pod 中容器的日志(stdout,pod 中有多個容器的情況下使用)
kubectl run -i --tty busybox --image=busybox -- sh # 交互式 shell 的方式運行 pod
kubectl attach my-pod -i # 連接到運行中的容器
kubectl port-forward my-pod 5000:6000 # 轉(zhuǎn)發(fā) pod 中的 6000 端口到本地的 5000 端口
kubectl exec my-pod -- ls / # 在已存在的容器中執(zhí)行命令(只有一個容器的情況下)
kubectl exec my-pod -c my-container -- ls / # 在已存在的容器中執(zhí)行命令(pod 中有多個容器的情況下)
kubectl top pod POD_NAME --containers # 顯示指定 pod 和容器的指標(biāo)度量
2.3.2 與節(jié)點和集群交互
kubectl cordon my-node # 標(biāo)記 my-node 不可調(diào)度
kubectl drain my-node # 清空 my-node 以待維護(hù)
kubectl uncordon my-node # 標(biāo)記 my-node 可調(diào)度
kubectl top node my-node # 顯示 my-node 的指標(biāo)度量
kubectl cluster-info # 顯示 master 和服務(wù)的地址
kubectl cluster-info dump # 將當(dāng)前集群狀態(tài)輸出到 stdout
kubectl cluster-info dump --output-directory=/path/to/cluster-state # 將當(dāng)前集群狀態(tài)輸出到 /path/to/cluster-state
# 如果該鍵和影響的污點(taint)已存在,則使用指定的值替換
kubectl taint nodes foo dedicated=special-user:NoSchedule
2.4 資源類型與別名
- pods:
po
- deployments:
deploy
- services:
svc
- namespace:
ns
- nodes:
no
2.5 格式化輸出
- 輸出 json 格式:
-o json
- 僅打印資源名稱:
-o name
- 以純文本格式輸出所有信息:
-o wide
- 輸出 yaml 格式:
-o yaml
3 API 概述
官網(wǎng)文檔:https://kubernetes.io/zh-cn/docs/reference/using-api
REST API 是 Kubernetes 系統(tǒng)的重要部分,組件之間的所有操作和通信均由 API Server 處理的 REST AP I調(diào)用,大多數(shù)情況下, API 定義和實現(xiàn)都符合標(biāo)準(zhǔn)的 HTTP REST 格式,可以通過 kubectl 命令管理工具或其他命令行工具來執(zhí)行。
3.1 類型
3.1.1 Alpha(不推薦)
- 包含 alpha 名稱的版本(例如v1alpha1)。
- 該軟件可能包含錯誤。啟用一個功能可能會導(dǎo)致 bug。默認(rèn)情況下,功能可能會被禁用。
- 隨時可能會丟棄對該功能的支持,恕不另行通知。
- API 可能在以后的軟件版本中以不兼容的方式更改,恕不另行通知。
- 該軟件建議僅在短期測試集群中使用,因為錯誤的風(fēng)險增加和缺乏長期支持。
3.1.2 Beta(可用)
- 包含 beta 名稱的版本(例如 v2beta3)。
- 該軟件經(jīng)過很好的測試。啟用功能被認(rèn)為是安全的。默認(rèn)情況下功能是開啟的。
- 細(xì)節(jié)可能會改變,但功能在后續(xù)版本不會被刪除
- 對象的模式或語義在隨后的 beta 版本或 Stable 版本中可能以不兼容的方式發(fā)生變化。如果這種情況發(fā)生時,官方會提供遷移操作指南。這可能需要刪除、編輯和重新創(chuàng)建API對象。
- 該版本在后續(xù)可能會更改一些不兼容地方,所以建議用于非關(guān)鍵業(yè)務(wù),如果你有多個可以獨立升級的集群,你也可以放寬此限制。
- 大家使用過的 Beta 版本后,可以多給社區(qū)反饋,如果此版本在后續(xù)更新后將不會有太大變化。
3.1.3 Stable(推薦)
- 該版本名稱命名方式:vX 這里 X 是一個整數(shù)。
- Stable 版本的功能特性,將出現(xiàn)在后續(xù)發(fā)布的軟件版本中。
3.2 訪問控制
3.2.1 認(rèn)證
3.2.1 授權(quán)
3.3 廢棄 api 說明
查看已廢棄的api信息:https://kubernetes.io/zh-cn/docs/reference/using-api/deprecation-guide
4 k8s集群各節(jié)點修改IP
修改已搭建完成k8s集群的各節(jié)點IP。
- 修改各節(jié)點的IP地址
- 修改各節(jié)點
/etc/hosts
vim /etc/hosts
- k8s-master節(jié)點
在/opt/k8s/
創(chuàng)建1233.sh,內(nèi)容如下:
# master 節(jié)點
export old_ip1=192.168.3.241
export new_ip1=192.168.1.241
# node 節(jié)點
export old_ip2=192.168.3.242
export new_ip2=192.168.1.242
export old_ip3=192.168.3.243
export new_ip3=192.168.1.243
# 修改Ip
find /etc/kubernetes -type f | xargs sed -i "s/$old_ip1/$new_ip1/"
find /etc/kubernetes -type f | xargs sed -i "s/$old_ip2/$new_ip2/"
find /etc/kubernetes -type f | xargs sed -i "s/$old_ip3/$new_ip3/"
find /root/.kube/config -type f | xargs sed -i "s/$old_ip1/$new_ip1/"
echo "Ip change success!"
cd /root/.kube/cache/discovery
mv ${old_ip1}_6443 ${new_ip1}_6443
echo "Ip and Port change success!"
# 重新生成證書
cd /etc/kubernetes/pki
mv apiserver.key apiserver.key.bak
mv apiserver.crt apiserver.crt.bak
kubeadm init phase certs apiserver --apiserver-advertise-address $new_ip1
echo "regeneration new ca!"
# 重啟kubelet
systemctl restart kubelet
echo "kubelet restart success!"
添加執(zhí)行權(quán)限:
chmod +x 1233.sh
運行腳本:文章來源:http://www.zghlxwxcb.cn/news/detail-772748.html
./1233.sh
- k8s-master節(jié)點,編輯ConfigMap,將舊 IP 替換成新的 IP
kubectl -n kube-system edit cm kubeadm-config
kubectl -n kube-system edit cm kube-proxy
kubectl edit cm -n kube-system coredns
kubectl edit cm -n kube-public cluster-info
systemctl restart kubelet
- k8s-node1、k8s-node2節(jié)點,修改ip
vim /etc/kubernetes/kubelet.conf
vim /etc/kubernetes/admin.conf
systemctl restart kubelet
- 查看結(jié)果
kubectl get node -o wide
[root@k8s-master k8s]# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master Ready control-plane,master 17d v1.23.6 192.168.1.241 <none> CentOS Linux 7 (Core) 3.10.0-1160.105.1.el7.x86_64 docker://24.0.7
k8s-node1 Ready <none> 17d v1.23.6 192.168.1.242 <none> CentOS Linux 7 (Core) 3.10.0-1160.105.1.el7.x86_64 docker://24.0.7
k8s-node2 Ready <none> 17d v1.23.6 192.168.1.243 <none> CentOS Linux 7 (Core) 3.10.0-1160.105.1.el7.x86_64 docker://24.0.7
5 其他(提煉常用內(nèi)容)
命令一般都是 kubectl [動作 create delete] [資源 pod services deploy] [名稱 nginx mysql]文章來源地址http://www.zghlxwxcb.cn/news/detail-772748.html
2.4 資源類型與別名
- pods:
po
- deployments:
deploy
- services:
svc
- namespace:
ns
- nodes:
no
2.5 格式化輸出
- 輸出 json 格式:
-o json
- 僅打印資源名稱:
-o name
- 以純文本格式輸出所有信息:
-o wide
- 輸出 yaml 格式:
-o yaml
到了這里,關(guān)于k8s學(xué)習(xí) — (實踐)第二章 搭建k8s集群的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!