Create a cloud-native application publishing base through rancher

Rancher 2.7.0 application

Zero, prerequisite

0.1 Configure kernel routing and forwarding

# vim /etc/sysctl.conf
# cat /etc/sysctl.conf
...
net.ipv4.ip_forward=1
# sysctl -p
net.ipv4.ip_forward = 1

0.2 docker-ce installation

Install docker-ce on all hosts

# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# yum -y install docker-ce
# systemctl enable --now docker

0.3 rancher deployment

# mkdir -p /opt/data/rancher_data
# docker run -d --privileged -p 80:80 -p 443:443  -v /opt/data/rancher_data:/var/lib/rancher --restart=always --name rancher-2-7-0 rancher/rancher:v2.7.0

1. Rancher initial configuration

Insert image description here

2. Create a cluster

Import an existing cluster. The cluster version imported this time is k8s 1.25.2.

Insert image description here

Select Universal Import to any kubernetes cluster

Insert image description here

Add a name to the imported cluster

Insert image description here

Add labels to imported clusters

Insert image description here

Insert image description here

Register the existing cluster to rancher

Insert image description here

After copying, execute relevant commands on the master node in the k8s cluster

[root@k8s-master01 ~]# curl --insecure -sfL https://192.168.10.163/v3/import/pg977t2z59g872brkj75qmxtkgkz8x5lrwwpdhkprvmcjhhr7tp6wm_c-m-p7wrqk4w.yaml | kubectl apply -f -

After execution, check whether 4 namespaces are added

[root@k8s-master01 ~]# kubectl get ns
NAME                          STATUS   AGE
calico-apiserver              Active   36h
calico-system                 Active   36h
cattle-fleet-system           Active   60s 新添加
cattle-impersonation-system   Active   77s 新添加
cattle-system                 Active   81s 新添加
default                       Active   36h
kube-node-lease               Active   36h
kube-public                   Active   36h
kube-system                   Active   36h
local                         Active   27s 新添加
tigera-operator               Active   36h

After all executions are completed, you can find that all k8s cluster nodes are registered to rancher.

Insert image description here

3. Application release

Select the cluster where the application is published

Insert image description here

Insert image description here

3.1 Deploy Nginx application

Before publishing the application, create a project/namespace

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

After the project/namespace is created, choose to create a workload (type Deployments)

Insert image description here

Insert image description here

Specify the namespace, application name, and number of copies for application publishing

Insert image description here

Specify pod label

Insert image description here

Specify the image and container type used by the application (standard container)

Insert image description here

Add a port for application publishing, that is, create a corresponding service

Insert image description here

Insert image description here

After all the above configurations are completed, click Create

Insert image description here

Observe the created application

Insert image description here

Insert image description here

Access created applications on a physical machine

Insert image description here

3.2 Deploy MySQL application

View existing storage classes in k8s cluster

Persistent dynamic storage classes need to be prepared in advance

[root@k8s-master01 ~]# kubectl get sc
NAME                   PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-client (default)   fuseim.pri/ifs   Delete          Immediate           false                  15h

View in rancher ui interface
Insert image description here

Before publishing mysql, create a project/namespace

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Create PVC

Insert image description here

Insert image description here

Insert image description here

Create service

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Be sure to pay attention to this selector. Create it now. When you create the MySQL application, its Pod label must be consistent with this.

Insert image description here

Insert image description here

Insert image description here

Check the created SVC

[root@k8s-master01 ~]# kubectl get svc -n mydata1
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
mysqldata1   ClusterIP   None         <none>        3306/TCP   49s

Parse the IP address corresponding to SVC

Since Headless SVC is used, SVC does not assign ClusterIP, so there is no result.

[root@k8s-master01 ~]# dig -t a mysqldata1.mydata1.svc.cluster.local @10.96.0.10

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> -t a mysqldata1.mydata1.svc.cluster.local @10.96.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 18405
;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;mysqldata1.mydata1.svc.cluster.local. IN A

;; AUTHORITY SECTION:
cluster.local.          30      IN      SOA     ns.dns.cluster.local. hostmaster.cluster.local. 1664333089 7200 1800 86400 30

;; Query time: 0 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: 三 9月 28 10:47:03 CST 2022
;; MSG SIZE  rcvd: 158

Create a Statefulset type application

Insert image description here

Select and fill in the corresponding name. The service name is a service created in advance.

Insert image description here

Select MySQL container image

Insert image description here

Add environment variables

Set the MySQL administrator password through environment variables and add it to the MySQL database

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

The Pod label must be consistent with the label used by the selector in the created SVC

Insert image description here

Since MySQL needs to store data persistently, it needs to select a storage. This time, the PVC created earlier is used.

Insert image description here

Insert image description here

Mount the PVC created previously to the /var/lib/mysql directory in the MySQL container

Insert image description here

Insert image description here

Observe application releases

Insert image description here

Insert image description here

Execute the command to observe whether the application is available

Insert image description here

Insert image description here

3.3 Deploy WordPress application

Create project/namespace

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Observe namespace creation status

[root@k8s-master01 ~]# kubectl get ns
NAME                          STATUS   AGE
calico-apiserver              Active   37h
calico-system                 Active   37h
cattle-fleet-system           Active   59m
cattle-impersonation-system   Active   59m
cattle-system                 Active   59m
default                       Active   37h
kube-node-lease               Active   37h
kube-public                   Active   37h
kube-system                   Active   37h
local                         Active   58m
mydata1                       Active   34m
nginxweb                      Active   50m
tigera-operator               Active   37h
webpp1                        Active   34s  新添加命名空间

Publish the application corresponding to the Deployment

Insert image description here

Select Deployment Controller Type

Insert image description here

Insert image description here

Insert image description here

Add the port used by the application. This time, use the node port to publish it.

Insert image description here

Insert image description here

Configure the environment variables required by the application and connect to the database through environment variables

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Parse database domain name for environment variable definition

[root@k8s-master01 ~]# dig -t a mysqldata1-0.mysqldata1.mydata1.svc.cluster.local. @10.96.0.10

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> -t a mysqldata1-0.mysqldata1.mydata1.svc.cluster.local. @10.96.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38690
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;mysqldata1-0.mysqldata1.mydata1.svc.cluster.local. IN A

;; ANSWER SECTION:
mysqldata1-0.mysqldata1.mydata1.svc.cluster.local. 30 IN A 10.224.69.203  解析出来Pod IP地址

;; Query time: 0 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: 三 9月 28 11:32:07 CST 2022
;; MSG SIZE  rcvd: 143

Insert image description here

Insert image description here

Observe application creation

Insert image description here

Insert image description here

Access published applications on physical machines

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

4. Delete the cluster

Insert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_47758895/article/details/130599457