一、pod
1、更改镜像站
[root@k8s-master ~]# vim /etc/docker/daemon.json
{ "registry-mirrors": [ "https://do.nark.eu.org", "https://dc.j8.work", "https://docker.m.daocloud.io", "https://dockerproxy.com", "https://docker.mirrors.ustc.edu.cn", "https://docker.nju.edu.cn" ] }
2、加载启动docker服务
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl start docker
3、拉取常用镜像
[root@k8s-master ~]# docker pull centos
[root@k8s-master ~]# docker pull nginx
[root@k8s-master ~]# docker pull mysql:5.7.44[root@k8s-master ~]# docker pull haproxy
[root@k8s-master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE haproxy latest a782c02b8259 10 days ago 103MB nginx latest 39286ab8a5e1 4 weeks ago 188MB mysql 5.7.44 5107333e08a8 9 months ago 501MB centos latest 5d0da3dc9764 2 years ago 231MB
4、使用docker save命令打包镜像
[root@k8s-master ~]# docker save -o centos.tar centos:latest
[root@k8s-master ~]# docker save -o nginx.tar nginx:latest
[root@k8s-master ~]# docker save -o haproxy.tar haproxy:latest
[root@k8s-master ~]# docker save -o mysql.tar mysql:5.7.44
5、使用ctr指令将tar包导入到containerd镜像中
[root@k8s-master ~]# ctr -n k8s.io images import centos.tar --platform=linux/amd64
[root@k8s-master ~]# ctr -n k8s.io images import nginx.tar --platform=linux/amd64
[root@k8s-master ~]# ctr -n k8s.io images import haproxy.tar --platform=linux/amd64
[root@k8s-master ~]# ctr -n k8s.io images import mysql.tar --platform=linux/amd64
[root@k8s-master ~]# crictl images 查看containerd镜像列表
IMAGE TAG IMAGE ID SIZE docker.io/library/centos latest 5d0da3dc97646 239MB docker.io/library/haproxy latest a782c02b82595 106MB docker.io/library/mysql 5.7.44 5107333e08a87 520MB docker.io/library/nginx latest 39286ab8a5e14 192MB
6、将master上的tar包上传到node节点上
[root@k8s-master ~]# scp /etc/docker/daemon.json root@192.168.8.203:/etc/docker/
[root@k8s-master ~]# scp /etc/docker/daemon.json root@192.168.8.204:/etc/docker/
scp ~/*.tar root@192.168.8.203:~
scp ~/*.tar root@192.168.8.204:~
[root@k8s-master ~]# scp haproxy.tar root@192.168.8.203:/
[root@k8s-master ~]# scp haproxy.tar root@192.168.8.203:~/
[root@k8s-master ~]# scp haproxy.tar root@192.168.8.204:~/
[root@k8s-master ~]# scp nginx.tar root@192.168.8.204:~/
[root@k8s-master ~]# scp nginx.tar root@192.168.8.203:~/
[root@k8s-master ~]# scp mysql.tar root@192.168.8.203:~/
[root@k8s-master ~]# scp mysql.tar root@192.168.8.204:~/
[root@k8s-master ~]# scp centos.tar root@192.168.8.204:~/
[root@k8s-master ~]# scp centos.tar root@192.168.8.203:~/[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl restart docker
[root@k8s-node2 ~]# systemctl daemon-reload
[root@k8s-node2 ~]# systemctl restart docker
7、使用ctr指令将tar包导入到containerd镜像中
[root@k8s-node1 ~]# ctr -n k8s.io images import haproxy.tar --platform=linux/amd64
[root@k8s-node1 ~]# ctr -n k8s.io images import centos.tar --platform=linux/amd64
[root@k8s-node1 ~]# ctr -n k8s.io images import nginx.tar --platform=linux/amd64
[root@k8s-node1 ~]# ctr -n k8s.io images import mysql.tar --platform=linux/amd64[root@k8s-node1 ~]# crictl images IMAGE TAG IMAGE ID SIZE docker.io/library/centos latest 5d0da3dc97646 239MB docker.io/library/haproxy latest a782c02b82595 106MB docker.io/library/mysql 5.7.44 5107333e08a87 520MB docker.io/library/nginx latest 39286ab8a5e14 192MB
--platform=linux/amd64 指定版本
[root@k8s-node2 ~]# ctr -n k8s.io images import haproxy.tar --platform=linux/amd64
[root@k8s-node2 ~]# ctr -n k8s.io images import centos.tar --platform=linux/amd64
[root@k8s-node2 ~]# ctr -n k8s.io images import nginx.tar --platform=linux/amd64
[root@k8s-node2 ~]# ctr -n k8s.io images import mysql.tar --platform=linux/amd64[root@k8s-node2 ~]# crictl images IMAGE TAG IMAGE ID SIZE docker.io/library/centos latest 5d0da3dc97646 239MB docker.io/library/haproxy latest a782c02b82595 106MB docker.io/library/mysql 5.7.44 5107333e08a87 520MB docker.io/library/nginx latest 39286ab8a5e14 192MB
8、使用kubectl run 创建pod
[root@k8s-master ~]# kubectl run test001 --image docker.io/library/nginx:latest --image-pull-policy=IfNotPresent
[root@k8s-master ~]# kubectl get po NAME READY STATUS RESTARTS AGE test001 1/1 Running 0 8s
[root@k8s-master ~]# kubectl describe pod test001
Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m6s default-scheduler Successfully assigned default/test001 to k8s-node2 Normal Pulled 2m5s kubelet Container image "docker.io/library/nginx:latest" already present on machine Normal Created 2m5s kubelet Created container test001 Normal Started 2m5s kubelet Started container test001
9、使用配置文件创建pod
[root@k8s-master ~]# vim test0007.yaml
apiVersion: v1 kind: Pod metadata: name: test0007 labels: name: test0007 spec: containers: - name: test0007nginx image: docker.io/library/nginx:latest imagePullPolicy: IfNotPresent ports: - name: nginxport containerPort: 80
root@k8s-master ~]# mv test0007.yaml pods/
[root@k8s-master ~]# cd pods/创建pod
[root@k8s-master pods]# kubectl create -f test0007.yaml
[root@k8s-master pods]# kubectl get po
NAME READY STATUS RESTARTS AGE test0007 1/1 Running 0 106s
10、添加两个容器再pod中
[root@k8s-master pods]# vim test0007.yaml
- name: nginxport containerPort: 80 - name: test0007centos image: docker.io/library/centos:latest imagePullPolicy: Never command: - sleep - infinity
[root@k8s-master pods]# kubectl delete -f test0007.yaml
[root@k8s-master pods]# kubectl create -f test0007.yaml[root@k8s-master pods]# kubectl get po
NAME READY STATUS RESTARTS AGE test0007 2/2 Running 0 77s
11、监控容器运行的5个切入点
postStart
startup
lived
ready
perStop
[root@k8s-master pods]# vim test0007.yaml
apiVersion: v1 kind: Pod metadata: name: test0007 labels: name: test0007 spec: restartPolicy: OnFailure containers: - name: test0007nginx image: docker.io/library/nginx:latest imagePullPolicy: IfNotPresent ports: - name: nginxport containerPort: 80 startupProbe: tcpSocket: port: 80 initialDelaySeconds: 10 timeoutSeconds: 2 periodSeconds: 20 successThreshold: 1 failureThreshold: 2 readinessProbe: httpGet: port: 80 path: /index.html initialDelaySeconds: 10 timeoutSeconds: 2 periodSeconds: 20 successThreshold: 1 failureThreshold: 2 livenessProbe: tcpSocket: port: 80 initialDelaySeconds: 10 timeoutSeconds: 2 periodSeconds: 20 successThreshold: 1 failureThreshold: 2 - name: test0007centos image: docker.io/library/centos:latest imagePullPolicy: Never command: - sleep - infinity
[root@k8s-master pods]# kubectl delete -f test0007.yaml
[root@k8s-master pods]# kubectl create -f test0007.yaml
[root@k8s-master pods]# kubectl get poNAME READY STATUS RESTARTS AGE test0007 2/2 Running 0 52s
12、配置生命周期
[root@k8s-master pods]# vim test0007.yaml
41 lifecycle: 42 postStart: 43 exec: 44 command: 45 - sh 46 - -c 47 - mkdir /data 48 preStop: 49 exec: 50 command: 51 - sh 52 - -c 53 - pkill nginx;sleep 30;
[root@k8s-master pods]# kubectl delete -f test0007.yaml
[root@k8s-master pods]# kubectl create -f test0007.yaml[root@k8s-master pods]# kubectl get po NAME READY STATUS RESTARTS AGE test0007 2/2 Running 0 35s
二、RC 和 RS
1、定义 Replication Controller
[root@k8s-master pods]# vim test0008.yaml
1 apiVersion: v1 2 kind: ReplicationController 3 metadata: 4 name: nginx0 5 spec: 6 replicas: 3 7 selector: 8 app: nginx0 9 template: 10 metadata: 11 name: nginx0 12 labels: 13 app: nginx0 14 spec: 15 containers: 16 - name: nginx0 17 image: docker.io/library/nginx:latest 18 imagePullPolicy: Never 19 ports: 20 - name: containerport 21 containerPort: 80
[root@k8s-master pods]# kubectl create -f test0008.yaml
[root@k8s-master pods]# kubectl get po
NAME READY STATUS RESTARTS AGE nginx0-8pmtv 1/1 Running 0 17s nginx0-gpvxm 1/1 Running 0 17s nginx0-z66zz 1/1 Running 0 17s
删除其中一个会再次自动创建
[root@k8s-master pods]# kubectl delete pod nginx0-gpvxm
[root@k8s-master pods]# kubectl get po
NAME READY STATUS RESTARTS AGE nginx0-8kxfb 1/1 Running 0 6s nginx0-8pmtv 1/1 Running 0 96s nginx0-z66zz 1/1 Running 0 96s
[root@k8s-master pods]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx0-8kxfb 1/1 Running 0 3m20s 172.16.169.148 k8s-node2 <none> <none> nginx0-8pmtv 1/1 Running 0 4m50s 172.16.169.146 k8s-node2 <none> <none> nginx0-z66zz 1/1 Running 0 4m50s 172.16.36.84 k8s-node1 <none> <none>
[root@k8s-master pods]# curl 172.16.169.146
[root@k8s-master pods]# kubectl delete -f test0008.yaml
[root@k8s-master pods]# vim test0009.yaml
1 apiVersion: apps/v1
2 kind: ReplicaSet
3 metadata:
4 name: nginx1
5 spec:
6 replicas: 3
7 selector:
8 matchLabels:
9 tier: nginx1
10 matchExpressions:
11 - key: tier
12 operator: In
13 values: [nginx1]
14 templete:
15 metadata:
16 name: nginx1
17 labels:
18 app: guestbook
19 tier: nginx1
20 spec:
21 #restartPolicy: OnFailure
22 containers:
23 - name: nginx1
24 image: docker.io/library/nginx:latest
25 imagePullPolicy: Never
26 ports:
27 - name: nginxport
28 containerPort: 80