列出资源支持的字段
root@ubuntu0:~# kubectl explain --help
root@ubuntu0:~# kubectl explain pod
KIND: Pod
VERSION: v1DESCRIPTION:Pod is a collection of containers that can run on a host. This resource iscreated by clients and scheduled onto hosts.FIELDS:apiVersion <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata <Object>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec <Object>Specification of the desired behavior of the pod. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-statusstatus <Object>Most recently observed status of the pod. This data may not be up to date.Populated by the system. Read-only. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status查看pod下metadata字段的参数和用法
root@ubuntu0:~# kubectl explain pod.metadata文档的类型说明:<string>表示值是一个字符串类型,一般情况下双引号可以省略,特殊字符触发。<integer>表示值必须是一个整型,说白了就是整数。<Object>表示一个对象,说明有多个下级字段,这些字段都是同一个对象。<map[string]string>表示一个map类型,对应的是"KEY: VALUE"的格式,其中KEY的类型是字符串,且VALUE的类型是字符串。<[]Object> 表示一个数组对象,说明下级字段可以定义多个并列的关系,代表的是多个对象。<[]string>表示数组字符串,可以定义多个字符串,使用"-"来区分。也可以使用中括号("[]")来定义,命令和参数使用双引号引起来,参数使用逗号分割。-required-关键字,表示该字段必须定义。- rc副本控制器1.rc控制器概述
可以指定控制Pod副本数量始终存活。2.编写资源清单root@ubuntu0:~/manifests/ReplicationController# cat 01-rc-xiuxian.yaml
apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-rc-xiuxian
spec:#指定多少个pod存货replicas: 3#标签选择器,一般用于关联pod的标签,rc控制器是基于标签关联pod的,他和下面定义的labels, labels要包含标签选择器 的标签,要不然会报错selector:apps: v1school: oldboy#定义pod的模板template:spec:containers:- name: c1image: mysqlsb:v1command: - tail- -f- /etc/hostsmetadata:labels:apps: v1school: oldboyclass: linux94
root@ubuntu0:~/manifests/ReplicationController# kubectl apply -f 01-rc-xiuxian.yaml
replicationcontroller/oldboyedu-rc-xiuxian created
root@ubuntu0:~/manifests/ReplicationController# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
oldboyedu-rc-xiuxian-k8rws 1/1 Running 0 3m7s 10.100.1.10 ubuntu1 <none><none>
oldboyedu-rc-xiuxian-lnfzn 1/1 Running 0 3m7s 10.100.1.11 ubuntu1 <none><none>
oldboyedu-rc-xiuxian-xpcp2 1/1 Running 0 3m7s 10.100.2.4 ubuntu2 <none><none>3.测试删除Pod观察是否会重新创建3个新的pod3.1已经开始创建三个新的pod了
root@ubuntu0:~/manifests/ReplicationController# kubectl delete pods --all
pod "oldboyedu-rc-xiuxian-k8rws" deleted
pod "oldboyedu-rc-xiuxian-lnfzn" deleted
pod "oldboyedu-rc-xiuxian-xpcp2" deleted
^C
root@ubuntu0:~/manifests/ReplicationController# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
oldboyedu-rc-xiuxian-ghclj 1/1 Running 0 3s 10.100.2.5 ubuntu2 <none><none>
oldboyedu-rc-xiuxian-k8rws 1/1 Terminating 0 4m 10.100.1.10 ubuntu1 <none><none>
oldboyedu-rc-xiuxian-kkrv2 1/1 Running 0 3s 10.100.1.12 ubuntu1 <none><none>
oldboyedu-rc-xiuxian-lnfzn 1/1 Terminating 0 4m 10.100.1.11 ubuntu1 <none><none>
oldboyedu-rc-xiuxian-szt7l 1/1 Running 0 3s 10.100.2.6 ubuntu2 <none><none>
oldboyedu-rc-xiuxian-xpcp2 1/1 Terminating 0 4m 10.100.2.4 ubuntu2 <none><none>查看rc列表
root@ubuntu0:~/manifests/ReplicationController# kubectl get rc
NAME DESIRED CURRENT READY AGE
oldboyedu-rc-xiuxian 333 5m27s那如何删除rc的资源
root@ubuntu0:~/manifests/ReplicationController# kubectl delete rc --all
replicationcontroller "oldboyedu-rc-xiuxian" deleted
root@ubuntu0:~/manifests/ReplicationController# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
oldboyedu-rc-xiuxian-ghclj 1/1 Terminating 0 2m59s 10.100.2.5 ubuntu2 <none><none>
oldboyedu-rc-xiuxian-kkrv2 1/1 Terminating 0 2m59s 10.100.1.12 ubuntu1 <none><none>
oldboyedu-rc-xiuxian-szt7l 1/1 Terminating 0 2m59s 10.100.2.6 ubuntu2 <none><none>但是这个有一个弊端,指定pod去删除的话,他虽然会重启,但是ip地址会改变,那如何解决呢,k8s内置了一般svc的参数
root@ubuntu0:~/manifests/ReplicationController# kubectl api-resources |grep -w services
services svc v1 true Service
使用svc提供用户的请求代理
root@ubuntu0:~/manifests/ReplicationController# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
oldboyedu-rc-xiuxian-7hwgd 1/1 Running 0 14s 10.100.2.8 ubuntu2 <none><none>
oldboyedu-rc-xiuxian-ff8lk 1/1 Running 0 14s 10.100.1.13 ubuntu1 <none><none>
oldboyedu-rc-xiuxian-fxdhv 1/1 Running 0 14s 10.100.2.7 ubuntu2 <none><none>root@ubuntu0:~/manifests/services# cat 01-svc-xiuxian.yaml
apiVersion: v1
kind: Service
metadata:name: svc-name
spec: #定义标签选择器关联后端podselector: apps: v1class: linux94#定义端口映射ports:# 定义Service对外暴露的端口- port: 88# 定义后端关联Pod的端口,相当于负载均衡器,访问88端口跳转到后端的80端口上 targetPort: 80
root@ubuntu0:~/manifests/ReplicationController# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 192.168.0.1 <none>443/TCP 3d7h <none>
svc-name ClusterIP 192.168.51.94 <none>88/TCP 110s apps=v1,class=linux94
root@ubuntu0:~/manifests/ReplicationController# kubectl describe svc svc-name
Name: svc-name
Namespace: default
Labels: <none>
Annotations: <none>
Selector: apps=v1,class=linux94
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 192.168.51.94
IPs: 192.168.51.94
Port: <unset>88/TCP
TargetPort: 80/TCP
Endpoints: 10.100.1.13:80,10.100.2.7:80,10.100.2.8:80 #访问88端口他就会自动的转到这三台机器的80端口
Session Affinity: None
Events: <none>
root@ubuntu0:~/manifests/ReplicationController#kubectl exec -it oldboyedu-rc-xiuxian-fpqf9 -- sh
/ # echo AAAAAAAAAAAAAAAAAAA > /usr/share/nginx/html/index.html
/ #
root@ubuntu0:~/manifests/ReplicationController#
root@ubuntu0:~/manifests/ReplicationController# kubectl exec -it oldboyedu-rc-xiuxian-m9bn4 -- sh
/ # echo BBBBBBBBBBBBBBBBBBBB > /usr/share/nginx/html/index.html
/ #
root@ubuntu0:~/manifests/ReplicationController#
root@ubuntu0:~/manifests/ReplicationController# kubectl exec -it oldboyedu-rc-xiuxian-sfwss -- sh
/ # echo CCCCCCCCCCCCCCCCCCC > /usr/share/nginx/html/index.html
/ #
root@ubuntu0:~/manifests/ReplicationController#
root@ubuntu0:~/manifests/ReplicationController# for i in `seq 10`; do curl 10.200.161.32:88;done # 访问10次,发现每次请求的服务器随机。
AAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBB
AAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCC
AAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAA
[root@master231 services]# 此时就算ip地址发生改变了,ip地址也会跟着变,他是基于标签关联的,只要标签不变,ip就不会变
svc的服务发现功能
root@ubuntu0:~/manifests/ReplicationController# kubectl describe svc svc-xiuxain-v1 | grep Endpoints
Endpoints: 10.100.1.11:80,10.100.2.25:80,10.100.2.26:80
root@ubuntu0:~/manifests/ReplicationController# kubectl delete pods --all
pod "oldboyedu-rc-xiuxian-qc258" deleted
pod "oldboyedu-rc-xiuxian-xdt4c" deleted
pod "oldboyedu-rc-xiuxian-xsnbg" deleted
root@ubuntu0:~/manifests/ReplicationController#kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
oldboyedu-rc-xiuxian-9m9bp 1/1 Running 0 4s 10.100.2.28 worker233 <none><none>apps=v1,class=linux94,school=oldboyedu
oldboyedu-rc-xiuxian-tq7k7 1/1 Running 0 4s 10.100.1.12 worker232 <none><none>apps=v1,class=linux94,school=oldboyedu
oldboyedu-rc-xiuxian-v7mcp 1/1 Running 0 4s 10.100.2.27 worker233 <none><none>apps=v1,class=linux94,school=oldboyedu
root@ubuntu0:~/manifests/ReplicationController# kubectl describe svc svc-xiuxain-v1 | grep Endpoints
Endpoints: 10.100.1.12:80,10.100.2.27:80,10.100.2.28:80可以在 这里面手动的更改负载均衡器的数量
root@ubuntu0:~/manifests/services# kubectl edit rc oldboyedu-rc-xiuxian # 此步骤修改svc对应的replicas副本数量。观察svc后端是否自动发现。
Edit cancelled, no changes made.
- 将WordPress和MySQL镜像推送到harbor仓库项目名称为: - oldboyedu-db- oldboyedu-wp- 使用rc资源部署MySQL镜像,要求如下:- 用户名: linux94 - 密码: oldboyedu- 数据库: wordpress- 管理员密码为空- 使用svc关联MySQL数据库 - 最后新建pod连接MySQL进行测试root@ubuntu0:~/manifests/ReplicationController# cat 02-rc-mysql.yaml
apiVersion: v1
kind: ReplicationController
metadata:name: mysql-test
spec:replicas: 1selector:apps: mysql80template:metadata:labels:apps: mysql80 spec: nodeName: worker233hostNetwork: truecontainers:- name: dbimage: mysqlsb:v1env:- name: MYSQL_ALLOW_EMPTY_PASSWORDvalue: "yes"- name: MYSQL_USERvalue: linux94- name: MYSQL_PASSWORDvalue: oldboyedu- name: MYSQL_DATABASEvalue: wordpressroot@ubuntu0:~/manifests/ReplicationController# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-test-fvlcz 1/1 Running 0 5s 192.168.23.98 ubuntu1 <none><none>
root@ubuntu0:~/manifests/ReplicationController# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
mysql-test 111 40s db mysqlsb:v1 apps=mysql80使用svc关联MySQL数据库 ,此时就关联上数据库了
root@ubuntu0:~/manifests/services# cat 02-mysql.yaml
apiVersion: v1
kind: Service
metadata:name: svc-name
spec: selector: apps: mysql80ports:- port: 3306targetPort: 3306root@ubuntu0:~/manifests/services# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none>443/TCP 7d6h
svc-name ClusterIP 192.168.212.112 <none>3306/TCP 3d23h
root@ubuntu0:~/manifests/services# kubectl describe svc
kubernetes svc-name
root@ubuntu0:~/manifests/services# kubectl describe svc svc-name
Name: svc-name
Namespace: default
Labels: <none>
Annotations: <none>
Selector: apps=mysql80
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 192.168.212.112
IPs: 192.168.212.112
Port: <unset>3306/TCP
TargetPort: 3306/TCP
Endpoints: 192.168.23.98:3306
Session Affinity: None
Events: <none>最后新建pod连接MySQL进行测试(临时测试)
root@ubuntu0:~/manifests/services# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-test-fvlcz 1/1 Running 0 43m 192.168.23.98 ubuntu1 <none><none>
root@ubuntu0:~/manifests/services# kubectl run -it mysql-test --image=harbor.oldboyedu.com/linux94/mysql:8.0.36-oracle -- mysql -h 192.168.23.98 -u root
If you don't see a command prompt, try pressing enter.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema || mysql || performance_schema || sys || wordpress |
+--------------------+
5 rows inset(0.00 sec)
测试完删掉即可
root@ubuntu0:~/manifests/services# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-test 1/1 Running 1(8s ago) 60s 10.100.1.17 ubuntu1 <none><none>
mysql-test-fvlcz 1/1 Running 0 45m 192.168.23.98 ubuntu1 <none><none>
root@ubuntu0:~/manifests/services# kubectl de
debug delete describe
root@ubuntu0:~/manifests/services# kubectl delete pods mysql-test
pod "mysql-test" deleted
root@ubuntu0:~/manifests/services#
2.编写资源清单
[root@master231 case-demo]# cat 06-devops-gitlab.yaml
apiVersion: v1
kind: Namespace
metadata:name: devops---apiVersion: v1
kind: ReplicationController
metadata:name: oldboyedu-gitlabnamespace: devops
spec:replicas: 1selector:apps: gitlabtemplate:spec:nodeName: worker233containers:- name: c1# image: gitlab/gitlab-ce:17.5.2-ce.0image: harbor.oldboyedu.com/oldboyedu-devops/gitlab-ce:17.5.2-ce.0# 配置宿主机的端口映射ports:# 定义容器的端口- containerPort: 80# 绑定到宿主机的端口hostPort: 8080metadata:labels:apps: gitlab3.创建资源
[root@master231 case-demo]# kubectl apply -f 06-devops-gitlab.yaml
namespace/devops created
replicationcontroller/oldboyedu-gitlab created
[root@master231 case-demo]# [root@master231 case-demo]# [root@master231 case-demo]# kubectl get pods -o wide -n devops
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
oldboyedu-gitlab-sw2n6 1/1 Running 0 6m1s 10.100.2.60 worker233 <none><none>[root@master231 case-demo]# [root@master231 case-demo]# kubectl -n devops exec -it oldboyedu-gitlab-sw2n6 -- bash
root@oldboyedu-gitlab-sw2n6:/# netstat -untal | egrep ":80"
tcp 000.0.0.0:80 0.0.0.0:* LISTEN
..
root@oldboyedu-gitlab-sw2n6:/# 4.查看默认的root密码
[root@master231 case-demo]# kubectl -n devops exec oldboyedu-gitlab-sw2n6 -- cat /etc/gitlab/initial_root_password# WARNING: This value is valid only in the following conditions# 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).# 2. Password hasn't been changed manually, either via UI or via command line.## If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.Password: Pm9uyDtMdoR1FEw4rGcKsjl55VQQ3iOGxrNFuz/Dj9o=# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.[root@master231 case-demo]# 5.windows修改root密码
http://10.0.0.233:8080/推荐阅读:https://docs.gitlab.com/ee/install/docker/installation.html#install-gitlab-by-using-docker-compose