k8s闆嗙兢鎼缓-鏂?浠ュ強k8s鐩稿叧鍛戒护璇︽儏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s闆嗙兢鎼缓-鏂?浠ュ強k8s鐩稿叧鍛戒护璇︽儏相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ges' title='ges'>ges   tty   describe   event   deploy   editor   鐩稿叧   yum   鍚嶇О   

淇敼涓绘満鍚?br/>hostnamectl set-hostname master00

hostnamectl set-hostname node1

hostnamectl set-hostname node2 鎵€鏈夐泦缇や富鏈洪兘鏀瑰ソ锛屼富鏈哄悕鑷畾涔?/p>

淇敼/etc/hosts鏂囦欢,鎵€鏈変富鏈洪兘淇敼
192.168.5.100 master00
192.168.5.101 node01
192.168.5.102 node02

swapoff -a 

sed -i 鈥榮/.*swap.*/#&/鈥?/etc/fstab

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl -p /etc/sysctl.conf

systemctl stop firewalld

systemctl disable firewalld

setenforce  0 

sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux 

sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

cat <<EOF >  /etc/yum.repos.d/kubernetes.repo 
[kubernetes]
name=kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
enable=1
EOF

yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum install docker-ce -y

systemctl enable docker

systemctl start docker

yum -y install kubelet kubeadm  kubectl kubernetes-cni

systemctl enable kubelet.service

systemctl start kubelet

kubeadm config print init-defaults > kubeadm.conf

** #娉細sed杩欎竴姝ユ搷浣滃彲鑷杩涘叆 kubeadm.conf涓慨鏀硅嚜瀹氫箟鐗堟湰锛屾垜鐨勬槸v1.18.2**
sed -i s/v1.14.0/v1.14.3/  kubeadm.conf

kubeadm config images pull --config kubeadm.conf

kubeadm init --kubernetes-version=v1.18.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.111.128

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml

瀹屼簡涔嬪悗璁颁綇涓嬮潰涓よ鍐呭锛屽洜涓簄ode鍔犲叆闆嗙兢闇€瑕佺敤鍒?kubeadm join 192.168.5.100:6443 --token j2tngs.y5fzfd9ph5ppdien     --discovery-token-ca-cert-hash sha256:b2a2b249a16aaf2f1784ab6206fb6abd279e8c70cf0b470c7fd004f1b8fdb481

鏌ョ湅闆嗙兢鐘舵€?kubectl get cs

NAME                 STATUS         MESSAGE             ERROR
controller-manager   Healthy      ok                  
scheduler                  Healthy      ok                  
etcd-0                       Healthy      {"health":"true"}   鎴愬姛

kubectl get pods --all-namespaces

NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-fb8b8dccf-khsgh            1/1     Running   0          2m2s
kube-system   coredns-fb8b8dccf-rrxgb             1/1     Running   0          2m2s
kube-system   etcd-master00                             1/1     Running   0          78s
kube-system   kube-apiserver-master00            1/1     Running   0          73s
kube-system   kube-controller-manager-master 00   1/1     Running   0          79s
kube-system   kube-flannel-ds-amd64-xpr74     1/1     Running   0          2m2s
kube-system   kube-proxy-dhvcr                         1/1     Running   0          2m2s
kube-system   kube-scheduler-master00            1/1     Running   0          57s

濡傛灉STATUS涓嶆槸Running 璇风瓑鍑犲垎閽熷啀鏌ョ湅锛岃繕鏄病鏈夛紝鏌ョ湅鎶ラ敊鏃ュ織 cat /var/log/messages锛岀劧鍚庢壘澶辫触鍘熷洜

master閮ㄧ讲瀹屼簡锛屼笅闈㈡槸node閮ㄧ讲

swapoff -a 

sed -i 鈥榮/.*swap.*/#&/鈥?/etc/fstab

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl -p /etc/sysctl.conf

systemctl stop firewalld

systemctl disable firewalld

setenforce  0 

sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux 

sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

cat <<EOF >  /etc/yum.repos.d/kubernetes.repo 
[kubernetes]
name=kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
enable=1
EOF

yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum install docker-ce -y

systemctl enable docker

systemctl start docker

yum -y install kubelet kubeadm  kubectl kubernetes-cni

systemctl enable kubelet.service

systemctl start kubelet

kubeadm config print init-defaults > kubeadm.conf

#娉細sed杩欎竴姝ユ搷浣滃彲鑷杩涘叆 kubeadm.conf涓慨鏀硅嚜瀹氫箟鐗堟湰锛屾垜鐨勬槸v1.18.2
sed -i s/v1.14.0/v1.14.3/  kubeadm.conf

kubeadm config images pull --config kubeadm.conf

鐒跺悗鍔犲叆闆嗙兢锛氶€氳繃鍒氬垰master鐨勮褰曞姞鍏?
kubeadm join 192.168.5.100:6443 --token j2tngs.y5fzfd9ph5ppdien     --discovery-token-ca-cert-hash sha256:b2a2b249a16aaf2f1784ab6206fb6abd279e8c70cf0b470c7fd004f1b8fdb481

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 鈥榢ubectl get nodes鈥?on the control-plane to see this node join the cluster  鎴愬姛鍔犲叆闆嗙兢

鐒跺悗鎵€鏈夌殑node渚濇鍔犲叆闆嗙兢

鏌ョ湅闆嗙兢鐘舵€?br/>kubectl get cs

鏌ョ湅闆嗙兢鑺傜偣
kubectl get nodes


k8s涓€浜涚浉鍏冲懡浠わ細
#鏌ョ湅鎵€鏈塶amespace鐨刾ods杩愯鎯呭喌
#kubectl get pods --all-namespaces
#鏌ョ湅鍏蜂綋pods锛岃寰楀悗杈硅窡namespace鍚嶅瓧鍝?br/>kubectl get pods kubernetes-dashboard-76479d66bb-nj8wr --namespace=kube-system
鏌ョ湅pods鍏蜂綋淇℃伅
kubectl get pods -o wide kubernetes-dashboard-76479d66bb-nj8wr --namespace=kube-system
鏌ョ湅闆嗙兢鍋ュ悍鐘舵€?br/>kubectl get cs
鑾峰彇鎵€鏈塪eployment
kubectl get deployment --all-namespaces
鍒楀嚭璇?namespace 涓殑鎵€鏈?pod 鍖呮嫭鏈垵濮嬪寲鐨?br/>kubectl get pods --include-uninitialized
鏌ョ湅deployment()
kubectl get deployment nginx-app
鏌ョ湅rc鍜宻ervers
kubectl get rc,services
鏌ョ湅pods缁撴瀯淇℃伅锛堥噸鐐癸紝閫氳繃杩欎釜鐪嬫棩蹇楀垎鏋愰敊璇級
瀵规帶鍒跺櫒鍜屾湇鍔★紝node鍚屾牱鏈夋晥
kubectl describe pods xxxxpodsname --namespace=xxxnamespace
鍏朵粬鎺у埗鍣ㄧ被浼煎惂锛屽氨鏄痥ubectl get 鎺у埗鍣?鎺у埗鍣ㄥ叿浣撳悕绉?br/>鏌ョ湅pod鏃ュ織
kubectl logs $POD_NAME
鏌ョ湅pod鍙橀噺
kubectl exec my-nginx-5j8ok -- printenv | grep SERVICE
闆嗙兢
kubectl get cs # 闆嗙兢鍋ュ悍鎯呭喌
kubectl cluster-info # 闆嗙兢鏍稿績缁勪欢杩愯鎯呭喌
kubectl get namespaces # 琛ㄧ┖闂村悕
kubectl version # 鐗堟湰
kubectl api-versions # API
kubectl get events # 鏌ョ湅浜嬩欢
kubectl get nodes //鑾峰彇鍏ㄩ儴鑺傜偣
kubectl delete node k8s2 //鍒犻櫎鑺傜偣
kubectl rollout status deploy nginx-test


鍒涘缓
kubectl create -f ./nginx.yaml # 鍒涘缓璧勬簮
kubectl create -f . # 鍒涘缓褰撳墠鐩綍涓嬬殑鎵€鏈墆aml璧勬簮
kubectl create -f ./nginx1.yaml -f ./mysql2.yaml # 浣跨敤澶氫釜鏂囦欢鍒涘缓璧勬簮
kubectl create -f ./dir # 浣跨敤鐩綍涓嬬殑鎵€鏈夋竻鍗曟枃浠舵潵鍒涘缓璧勬簮
kubectl create -f https://git.io/vPieo # 浣跨敤 url 鏉ュ垱寤鸿祫婧?br/>kubectl run -i --tty busybox --image=busybox ----鍒涘缓甯︽湁缁堢鐨刾od
kubectl run nginx --image=nginx # 鍚姩涓€涓?nginx 瀹炰緥
kubectl run mybusybox --image=busybox --replicas=5 ----鍚姩澶氫釜pod
kubectl explain pods,svc # 鑾峰彇 pod 鍜?svc 鐨勬枃妗?/p>


鏇存柊
kubectl rolling-update python-v1 -f python-v2.json # 婊氬姩鏇存柊 pod frontend-v1
kubectl rolling-update python-v1 python-v2 --image=image:v2 # 鏇存柊璧勬簮鍚嶇О骞舵洿鏂伴暅鍍?br/>kubectl rolling-update python --image=image:v2 # 鏇存柊 frontend pod 涓殑闀滃儚
kubectl rolling-update python-v1 python-v2 --rollback # 閫€鍑哄凡瀛樺湪鐨勮繘琛屼腑鐨勬粴鍔ㄦ洿鏂?br/>cat pod.json | kubectl replace -f - # 鍩轰簬 stdin 杈撳叆鐨?JSON 鏇挎崲 pod
寮哄埗鏇挎崲锛屽垹闄ゅ悗閲嶆柊鍒涘缓璧勬簮銆備細瀵艰嚧鏈嶅姟涓柇銆?br/>kubectl replace --force -f ./pod.json
涓?nginx RC 鍒涘缓鏈嶅姟锛屽惎鐢ㄦ湰鍦?80 绔彛杩炴帴鍒板鍣ㄤ笂鐨?8000 绔彛
kubectl expose rc nginx --port=80 --target-port=8000


鏇存柊鍗曞鍣?pod 鐨勯暅鍍忕増鏈紙tag锛夊埌 v4
kubectl get pod nginx-pod -o yaml | sed 鈥榮/(image: myimage):.*$/1:v4/鈥?| kubectl replace -f -
kubectl label pods nginx-pod new-label=awesome # 娣诲姞鏍囩
kubectl annotate pods nginx-pod icon-url=http://goo.gl/XXBTWq # 娣诲姞娉ㄨВ
kubectl autoscale deployment foo --min=2 --max=10 # 鑷姩鎵╁睍 deployment 鈥渇oo鈥?/p>


缂栬緫璧勬簮
kubectl edit svc/docker-registry # 缂栬緫鍚嶄负 docker-registry 鐨?service
KUBE_EDITOR="nano" kubectl edit svc/docker-registry # 浣跨敤鍏跺畠缂栬緫鍣?/p>


鍔ㄦ€佷几缂﹑od
kubectl scale --replicas=3 rs/foo # 灏唂oo鍓湰闆嗗彉鎴?涓?br/>kubectl scale --replicas=3 -f foo.yaml # 缂╂斁鈥渇oo鈥濅腑鎸囧畾鐨勮祫婧愩€?br/>kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # 灏哾eployment/mysql浠?涓彉鎴?涓?br/>kubectl scale --replicas=5 rc/foo rc/bar rc/baz # 鍙樻洿澶氫釜鎺у埗鍣ㄧ殑鏁伴噺
kubectl rollout status deploy deployment/mysql # 鏌ョ湅鍙樻洿杩涘害


鍒犻櫎
kubectl delete -f ./pod.json # 鍒犻櫎 pod.json 鏂囦欢涓畾涔夌殑绫诲瀷鍜屽悕绉扮殑 pod
kubectl delete pod,service baz foo # 鍒犻櫎鍚嶄负鈥渂az鈥濈殑 pod 鍜屽悕涓衡€渇oo鈥濈殑 service
kubectl delete pods,services -l name=myLabel # 鍒犻櫎鍏锋湁 name=myLabel 鏍囩鐨?pod 鍜?serivce
kubectl delete pods,services -l name=myLabel --include-uninitialized # 鍒犻櫎鍏锋湁 name=myLabel 鏍囩鐨?pod 鍜?service锛屽寘鎷皻鏈垵濮嬪寲鐨?br/>kubectl -n my-ns delete po,svc --all # 鍒犻櫎 my-ns namespace涓嬬殑鎵€鏈?pod 鍜?serivce锛屽寘鎷皻鏈垵濮嬪寲鐨?br/>kubectl delete pods prometheus-7fcfcb9f89-qkkf7 --grace-period=0 --force 寮哄埗鍒犻櫎


浜や簰
kubectl logs nginx-pod # dump 杈撳嚭 pod 鐨勬棩蹇楋紙stdout锛?br/>kubectl logs nginx-pod -c my-container # dump 杈撳嚭 pod 涓鍣ㄧ殑鏃ュ織锛坰tdout锛宲od 涓湁澶氫釜瀹瑰櫒鐨勬儏鍐典笅浣跨敤锛?br/>kubectl logs -f nginx-pod # 娴佸紡杈撳嚭 pod 鐨勬棩蹇楋紙stdout锛?br/>kubectl logs -f nginx-pod -c my-container # 娴佸紡杈撳嚭 pod 涓鍣ㄧ殑鏃ュ織锛坰tdout锛宲od 涓湁澶氫釜瀹瑰櫒鐨勬儏鍐典笅浣跨敤锛?br/>kubectl run -i --tty busybox --image=busybox -- sh # 浜や簰寮?shell 鐨勬柟寮忚繍琛?pod
kubectl attach nginx-pod -i # 杩炴帴鍒拌繍琛屼腑鐨勫鍣?br/>kubectl port-forward nginx-pod 5000:6000 # 杞彂 pod 涓殑 6000 绔彛鍒版湰鍦扮殑 5000 绔彛
kubectl exec nginx-pod -- ls / # 鍦ㄥ凡瀛樺湪鐨勫鍣ㄤ腑鎵ц鍛戒护锛堝彧鏈変竴涓鍣ㄧ殑鎯呭喌涓嬶級
kubectl exec nginx-pod -c my-container -- ls / # 鍦ㄥ凡瀛樺湪鐨勫鍣ㄤ腑鎵ц鍛戒护锛坧od 涓湁澶氫釜瀹瑰櫒鐨勬儏鍐典笅锛?br/>kubectl top pod POD_NAME --containers # 鏄剧ず鎸囧畾 pod鍜屽鍣ㄧ殑鎸囨爣搴﹂噺


璋冨害閰嶇疆
$ kubectl cordon k8s-node # 鏍囪 my-node 涓嶅彲璋冨害
$ kubectl drain k8s-node # 娓呯┖ my-node 浠ュ緟缁存姢
$ kubectl uncordon k8s-node # 鏍囪 my-node 鍙皟搴?br/>$ kubectl top node k8s-node # 鏄剧ず my-node 鐨勬寚鏍囧害閲?br/>$ kubectl cluster-info dump # 灏嗗綋鍓嶉泦缇ょ姸鎬佽緭鍑哄埌 stdout
$ kubectl cluster-info dump --output-directory=/path/to/cluster-state # 灏嗗綋鍓嶉泦缇ょ姸鎬佽緭鍑哄埌 /path/to/cluster-state
#濡傛灉璇ラ敭鍜屽奖鍝嶇殑姹$偣锛坱aint锛夊凡瀛樺湪锛屽垯浣跨敤鎸囧畾鐨勫€兼浛鎹?br/>$ kubectl taint nodes foo dedicated=special-user:NoSchedule

以上是关于k8s闆嗙兢鎼缓-鏂?浠ュ強k8s鐩稿叧鍛戒护璇︽儏的主要内容,如果未能解决你的问题,请参考以下文章

骞跺彂鐩稿叧闂浠ュ強java鍩虹鐭ヨ瘑

銆怢inux銆慻it銆乺epo銆乻vn銆乤db浠ュ強鍏朵粬甯哥敤鍛戒护

閫氳繃docker鎼缓ELK闆嗙兢

Linux绯荤粺-鍛戒护琛屽揩鎹烽敭

Hyperf 鍙戝竷 杞婚噺绾ф湁鍚戞棤鐜浘浠诲姟缂栨帓搴?鍜?閫嗘尝鍏拌〃绀烘硶 瀛靛寲缁勪欢浠ュ強 v2.1.4 鐗堟湰

Java闈㈣瘯鍩虹