1.k8s常用的資源
k8s yaml的主要組成
apiVersion: v1 api版本
kind: pod 資源類型
metadata: 屬性
spec: 詳細(xì)
k8s_pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: web
spec:
containers:
- name: nginx
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
1.構(gòu)建命令
kubectl create -f k8s_pod.yaml
2.查看狀態(tài)
kubectl get pod nginx -o wide
3.描述pod狀態(tài)
kubectl describe pod nginx
4.刪除pod
kubectl delete pod nginx 或者kubectl delete -f xxx.yaml
5.編輯pod的配置
kubectl edit pod nginx
這里需要注意的是構(gòu)建會(huì)失敗,因?yàn)閺墓?jié)點(diǎn)下載pod-infrastructure鏡像是從官網(wǎng)衡便,需要紅帽證書,所以我們用下載好的上傳到本地自己的鏡像倉(cāng)庫(kù)谁撼。
改變兩個(gè)從節(jié)點(diǎn)
vim /etc/kubernetes/kubelet
# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-i
mage=10.0.0.11:5000/pod-infrastructure:latest"
systemctl restart kubelet
pod資源:至少由兩個(gè)容器組成,pod基礎(chǔ)容器和業(yè)務(wù)容器組成
pod配置文件2:
apiVersion: v1
kind: Pod
metadata:
name: test
labels:
app: web
spec:
containers:
- name: nginx
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
- name: busybox
image: 10.0.0.11:5000/busybox:latest
command: ["sleep","10000"]
pod是k8s最小的資源單位
2.ReplicationController資源
rc:保證指定數(shù)量的pod始終存活,rc通過(guò)標(biāo)簽選擇器來(lái)關(guān)聯(lián)pod
創(chuàng)建一個(gè)rc.yaml文件
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 5
selector:
app: myweb
template:
metadata:
labels:
app: myweb
spec:
containers:
- name: myweb
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
rc的滾動(dòng)升級(jí) 新建一個(gè)nginx-rc1.15.yaml
升級(jí)
kubectl rolling-update nginx -f nginx-rc1.15.yaml --update-period=10s
回滾
kubectl rolling-update nginx2 -f nginx-rc.yaml --update-period=1s
3. service資源
service幫助pod暴露端口
創(chuàng)建一個(gè)service
apiVersion: v1
kind: Service
metadata:
name: myweb
spec:
type: NodePort #ClusterIP
ports:
- port: 80 #clusterIP
nodePort: 30000 #nodeport
targetPort: 80 #podport
selector:
app: myweb
#生成svc
kubectl create -f k8s_svc.yaml
#查看svc的兩種方法
kubectl get svc
kubectl get service
#查看所有資源類型
kubectl get all -o wide
#確保標(biāo)簽一樣避咆,修改為myweb
kubectl edit svc myweb
app: myweb
標(biāo)簽需要確保一樣,必須包竹,很重要Q嗫帷<!
#查看端口是否暴露成功
kubectl describe svc myweb
如何驗(yàn)證它的負(fù)載均衡呢苗缩?
方法一:
#修改副本數(shù)
kubectl scale rc nginx --replicas=2
方法二:
#進(jìn)入容器
kubectl exec -it pod_name /bin/bash
#k8s進(jìn)入容器的方法
[root@k8s-master svc]# kubectl exec -it nginx-5mf4r /bin/bash
root@nginx-5mf4r:/# echo '11111' >/usr/share/nginx/html/index.html
root@nginx-5mf4r:/# exit
[root@k8s-master svc]# kubectl exec -it nginx-ppjb3 /bin/bash
root@nginx-ppjb3:/# echo '2222' >/usr/share/nginx/html/index.html
root@nginx-ppjb3:/# exit
添加隨機(jī)端口
[root@k8s-master svc]# vim /etc/kubernetes/apiserver
KUBE_API_ARGS="--service-node-port-range=3000-50000"
[root@k8s-master svc]# systemctl restart kube-apiserver.service
service默認(rèn)使用iptables來(lái)實(shí)現(xiàn)負(fù)載均衡, k8s 1.8新版本中推薦使用lvs(四層負(fù)載均衡)
4.deployment資源
有rc在滾動(dòng)升級(jí)之后,會(huì)造成服務(wù)訪問(wèn)中斷,于是k8s引入了deployment資源
創(chuàng)建deployment
1饵蒂、編輯deploy的yaml文件
cat k8s_deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
resources:
limits:
cpu: 100m
requests:
cpu: 100m
2.編輯實(shí)現(xiàn)負(fù)載的yaml文件
cat k8s_svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-deployment
spec:
type: NodePort #ClusterIP
ports:
- port: 80 #clusterIP
nodePort: 30001 #nodeport
targetPort: 80 #podport
selector:
app: nginx
方法一:
deployment升級(jí)和回滾
命令行創(chuàng)建deployment
kubectl run nginx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
命令行升級(jí)版本
kubectl set image deploy nginx nginx=10.0.0.11:5000/nginx:1.15
查看deployment所有歷史版本
kubectl rollout history deployment nginx
deployment回滾到上一個(gè)版本
kubectl rollout undo deployment nginx
deployment回滾到指定版本
kubectl rollout undo deployment nginx --to-revision=2
方法二:
#修改配置文件中的此行改為nginx 1.15
[root@k8s-master deploy]# kubectl edit deployment nginx-deployment
- image: 10.0.0.11:5000/nginx:1.15
5.tomcat+mysql練習(xí)
在k8s中容器之間相互訪問(wèn),通過(guò)VIP地址!
1.上傳配置
[root@k8s-master tomcat_demo]# ll
total 16
-rw-r--r-- 1 root root 416 Jan 13 22:46 mysql-rc.yml
-rw-r--r-- 1 root root 145 Jan 13 22:29 mysql-svc.yml
-rw-r--r-- 1 root root 492 Jan 13 23:37 tomcat-rc.yml
-rw-r--r-- 1 root root 162 Jan 13 22:17 tomcat-svc.yml
2.創(chuàng)建
先創(chuàng)建mysql
kubectl create -f mysql-rc.yml
kubectl create -f mysql-svc.yml
kubectl get all -o wide查看mysql虛擬ip
vim tomcat-rc.yml
- name: MYSQL_SERVICE_HOST
value: '10.254.146.149'
更改ip
kubectl create -f tomcat-rc.yml
kubectl create -f tomcat-svc.yml
kubectl get all -o wide
啟動(dòng)成功后登陸