一、replicationcontroller (RC)
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
6、查看containerd镜像列表
[root@k8s-master ~]# 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
7、在node1和node2节点上引入tar包
[root@k8s-master ~]# scp /etc/docker/daemon.json root@192.168.8.178:/etc/docker/
[root@k8s-master ~]# scp ~/*.tar root@192.168.8.178:~/
[root@k8s-master ~]# scp mysql.tar root@192.168.8.178:~/
[root@k8s-master ~]# scp nginx.tar root@192.168.8.178:~/
[root@k8s-master ~]# scp haproxy.tar root@192.168.8.178:~/
[root@k8s-master ~]# scp centos.tar root@192.168.8.178:~/
[root@k8s-master ~]# scp /etc/docker/daemon.json root@192.168.8.168:/etc/docker/[root@k8s-master ~]# scp ~/*.tar root@192.168.8.168:~/
[root@k8s-master ~]# scp centos.tar root@192.168.8.168:~/
[root@k8s-master ~]# scp haproxy.tar root@192.168.8.168:~/
[root@k8s-master ~]# scp nginx.tar root@192.168.8.168:~/
[root@k8s-master ~]# scp mysql.tar root@192.168.8.168:~/[root@k8s-node2 ~]# systemctl daemon-reload
[root@k8s-node2 ~]# systemctl start docker[root@k8s-node1 ~]# systemctl daemon-reload
[root@k8s-node1 ~]# systemctl start docker[root@k8s-node2 ~]# ctr -n k8s.io images import mysql.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 haproxy.tar --platform=linux/amd64
[root@k8s-node2 ~]# ctr -n k8s.io images import centos.tar --platform=linux/amd64
[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
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 84s
[root@k8s-master ~]# kubectl describe pod test001
[root@k8s-master ~]# kubectl describe pod test001Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m26s default-scheduler Successfully assigned default/test001 to k8s-node1 Normal Pulled 2m25s kubelet Container image "docker.io/library/nginx:latest" already present on machine Normal Created 2m25s kubelet Created container test001 Normal Started 2m25s 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/[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 4s
10、添加两个容器
[root@k8s-master pods]# kubectl delete -f test0007.yaml
[root@k8s-master pods]# vim test0007.yamlapiVersion: 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 - name: test0007centos image: docker.io/library/centos:latest imagePullPolicy: Never command: - sleep - infinity
[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 9s
11、监控容器运行的5个切入点
postStart startup lived ready perStop
12、replicationcontroller (RC)
[root@k8s-master pods]# kubectl delete -f test0007.yaml
[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 create -f test0007.yaml
[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]# vim test0008.yaml
apiVersion: v1 kind: ReplicationController metadata: name: nginx0 spec: replicas: 3 selector: app: nginx0 # 模板 template: # pod 源数据 metadata: # pod名称 name: ngixn0 # pod标签 labels: app: nginx # pod详细信息 spec: containers: - name: nginx0 image: docker.io/library/nginx:l atest imagePullPolicy: Never ports: - name: containerport containerPort: 80
[root@k8s-master pods]# kubectl create -f test0008.yaml
replicationcontroller/nginx0 created
[root@k8s-master pods]# kubectl get poNAME READY STATUS RESTARTS AGE cluster-test1-54575cf56c-46mb4 1/1 Running 3 (51m ago) 3h59m nginx0-25xpx 1/1 Running 0 14s nginx0-49n2k 1/1 Running 0 14s nginx0-6lzvw 1/1 Running 0 14s
[root@k8s-master pods]# kubectl delete pod nginx0-49n2k //删除后会自动创建
pod "nginx0-49n2k" deleted
[root@k8s-master pods]# kubectl get poNAME READY STATUS RESTARTS AGE cluster-test1-54575cf56c-46mb4 1/1 Running 3 (52m ago) 4h nginx0-25xpx 1/1 Running 0 103s nginx0-6lzvw 1/1 Running 0 103s nginx0-gf22s 1/1 Running 0 3s [root@k8s-master pods]# kubectl get po -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES cluster-test1-54575cf56c-46mb4 1/1 Running 3 (54m ago) 4h2m 172.16.36.77 k8s-node1 <none> <none> nginx0-25xpx 1/1 Running 0 3m23s 172.16.36.81 k8s-node1 <none> <none> nginx0-6lzvw 1/1 Running 0 3m23s 172.16.169.141 k8s-node2 <none> <none> nginx0-gf22s 1/1 Running 0 103s 172.16.169.143 k8s-node2 <none> <none>
[root@k8s-master pods]# curl http://172.16.36.81:80
[root@k8s-master pods]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-master 159m 7% 1147Mi 66% k8s-node1 50m 2% 843Mi 49% k8s-node2 58m 2% 897Mi 52%
[root@k8s-master pods]# kubectl delete -f test0008.yaml //根据文件完全删除
[root@k8s-master pods]# kubectl delete replicationcontrollers nginx0 //删除
二、replicaSet
[root@k8s-master pods]# vim test0009.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx1
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx1
spec:
replicas: 3
selector:
matchLabels:
tier: nginx1
matchExpressions:
- key: tier
operator: In
values: [nginx1]
template:
metadata:
name: nginx1
labels:
app: guestbook
tier: nginx1
spec:
#restartPolicy: OnFailure
containers:
- name: nginx1
image: docker.io/library/nginx:latest
imagePullPolicy: Never
ports:
- name: nginxport
containerPort: 80
[root@k8s-master pods]# kubectl create -f test0009.yaml
[root@k8s-master pods]# kubectl get po
NAME READY STATUS RESTARTS AGE
cluster-test1-54575cf56c-46mb4 1/1 Running 5 (52s ago) 5h9m
nginx1-jg6lp 1/1 Running 0 35s
nginx1-r5hlp 1/1 Running 0 35s
nginx1-s26lm 1/1 Running 0 35s